2009年5月27日 星期三

ACM Q11417

import java.util.Scanner;
public class Q11417 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int count=0;
while(cin.hasNextInt()){
int N = cin.nextInt();
if(N==0){
break;
}
count++;
int G=0;
for(int i=1;i< N;i++)
for(int j=i+1;j<=N;j++)
{
G+=GCD(i,j);
}
System.out.println(G);
}
}
public static int GCD(int x, int y) {
int tmp;
while (x % y != 0) {
tmp = y;
y = x % y;
x = tmp;
}
return y;
}
}

2009年5月26日 星期二

ACM Q10696

import java.util.Scanner;
public class Q10696 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int count=0;
while(cin.hasNextInt()){
int N = cin.nextInt();
count++;
if(N==0 || count >=250000){
break;
}else{
if(N>=101 ){
int tmp = N-10;
System.out.println("f91("+N+") = "+tmp);
}else{
System.out.println("f91("+N+") = 91");
}
}
}
}
}

ACM Q11577

import java.util.Scanner;
public class Q11577 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int[] arrayd = new int[26];
int loop = Integer.parseInt(cin.nextLine().toString());


for(int j=1; j<=loop; j++)
{
int max=0;
for(int p =0; p<26; p++){
arrayd[p]=0;
}
String line = cin.nextLine();
char[] ca = line.toCharArray();

for(int i=0; i< ca.length; i++){
if(ca[i]<= 90 && ca[i]>=65){
int tmpval = (int)ca[i]-65;
arrayd[tmpval]++;
if(arrayd[tmpval] > max){
max = arrayd[tmpval];
}
}else if (ca[i]<= 122 && ca[i]>=97){
int tmpval = (int)ca[i]-97;
arrayd[tmpval]++;
if(arrayd[tmpval] > max){
max = arrayd[tmpval];
}
}
}


for(int p =0; p<26; p++){
if(arrayd[p]==max){
System.out.print((char)(p+97));
}
}

System.out.println();
}
}
}

2009年5月24日 星期日

ACM Q10038

import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class Q10038 {

public static void main(String args[]){
Scanner cin = new Scanner(System.in);
String inputLine;
while (cin.hasNextLine()){
int count =0;
int max =0;
ArrayList as = new ArrayList();

boolean flag = true;
inputLine = cin.nextLine();
String[] tokens = inputLine.split(" ");
count = Integer.parseInt(tokens[0]);
for (int i=1 ;i < tokens.length; i++) {
if(Integer.parseInt(tokens[i])> max){
max = Integer.parseInt(tokens[i]);
}

}
for (int i=1 ;i < tokens.length-1; i++) {
as.add(Math.abs(Integer.parseInt(tokens[i])-Integer.parseInt(tokens[i+1])));
}
Collections.sort(as);

for (int i=0 ;i < as.size(); i++) {
if(as.get(i)!=i+1){
System.out.println("Not jolly");
flag =false;
break;
}
}
if(flag ==true){
System.out.println("Jolly");
}


}

}
}

ACM Q494

import java.util.Scanner;

public class Q494 {

public static void main(String args[]){
Scanner cin = new Scanner(System.in);
String regex = "^[A-Za-z]+.*";
String delims = " [^A-Za-z]";
String inputLine;
while (cin.hasNextLine()){
int count =0;
inputLine = cin.nextLine();
String[] tokens = inputLine.split("[^A-Za-z]");

for (String s : tokens) {
if(s.matches(regex)){
count++;
}

}
System.out.println(count);
}

}

}

ACM Q10035

import java.util.Scanner;
public class Q10035 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
while(cin.hasNextInt()){
String x = cin.next();
String k = cin.next();
if(x.charAt(0)=='0' && k.charAt(0)=='0'){
break;
}
int Match_length=Math.max(x.length(), k.length());
char [] x1 = new char[Match_length];
char [] k1 = new char[Match_length];
int carry=0, count =0;
char[] x2 = x.toCharArray();
char[] k2 = k.toCharArray();
for(int i=0; i< x1.length; i++){
if(i< Match_length- x2.length){
x1[i] ='0';
}else{
x1[i] = x2[i+x2.length-Match_length];
}
}
for(int j=0; j< k1.length; j++){
if(j< Match_length- k2.length){
k1[j] ='0';
}else{
k1[j] = k2[j+k2.length-Match_length];
}
}
for(int p=Match_length-1; p>= 0; p--){
if(Integer.parseInt(Character.toString(x1[p]))+Integer.parseInt(Character.toString(k1[p]))+carry>=10){
carry = 1;
count ++;
}else{
carry =0;
}
}
if(count ==0){
System.out.println("No carry operation.");
}else if(count ==1){
System.out.println("1 carry operation.");
}else{
System.out.println(count+" carry operations.");
}
}

}
}

ACM Q10107

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;

public class Q10107 {
public static void main(String args[]) {

Scanner cin = new Scanner(System.in);
LinkedList al=new LinkedList();
int count=0;
int flag = 0;
while (cin.hasNext()) {
count++;
Iterator itr = al.iterator();
int tmp = cin.nextInt();
int insert_pos=0;
while(itr.hasNext())
{
if(tmp > (Integer) itr.next()){
insert_pos ++;
}else{
break;
}
}
al.add(insert_pos,tmp);

if(count%2==1){
System.out.println(al.get(flag));
}else{
System.out.println((al.get(flag) + al.get(flag+1))/2);
flag++;
}

}
}
}

ACM Q10062

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;

public class Q10062 {


  public static void main(String args[]){
    Scanner cin = new Scanner(System.in);


    String inputLine;
    while (cin.hasNextLine()){
      inputLine = cin.nextLine();

      Scanner tokenize = new Scanner(inputLine).useDelimiter("");
      final HashMap numbers = new HashMap();
      while(tokenize.hasNext()) {
        String tmp = tokenize.next();
        if(!numbers.containsKey(tmp)){
          numbers.put(tmp,1);
        }else{
          int value = numbers.get(tmp)+1;
          numbers.put(tmp,value);
        }
      }

      ArrayList as = new ArrayList(numbers.keySet());
      Collections.sort(as, Collections.reverseOrder());
      Collections.sort( as , new Comparator() {
          public int compare( Object o1 , Object o2 )
          {
          String e1 = o1.toString() ;
          String e2 = o2.toString() ;
          Integer first = (Integer)numbers.get(e1);
          Integer second = (Integer)numbers.get(e2);
          return first.compareTo( second );
          }
          });

      Iterator itr = as.iterator();
      while(itr.hasNext()){
        String s= itr.next();
        System.out.println((int)s.charAt(0)+" "+numbers.get(s));
      }
      if(cin.hasNextLine()){
        System.out.println("");
      }
    }

  }

}

ACM Q10970

import java.util.Scanner;
public class Q10970 {
  public static void main(String args[]) {

    Scanner cin = new Scanner(System.in);
    while(cin.hasNextInt()){
      int x = cin.nextInt();
      int k = cin.nextInt();
      System.out.println(x*k-1);
    }

  }
}