2011年3月30日 星期三

Java 算一算每行有幾個字

Word的定義是連續的字元(A~Z,a~z)所組成的字。
ASCII 字元表

輸入說明:
asd....asd
This is a book.

輸出說明:
2
4

import java.util.*;

public class HowManyWorld {
   public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      String str;
      StringBuffer stb;
      int a,b;
      char c;
  
      while(sc.hasNext()){
         str = sc.nextLine();
         stb = new StringBuffer(str);
         a = 0;
         b = 0;
  
         for(int i = 0; i < str.length(); i++){
            c = stb.charAt(i);
            if( (c >= 65 && c <= 90) || (c >= 97 && c <= 122) ){
                b++;
                continue;
            }
            else if((b >= 1) && (i > 0 || c <= 64 || (c >= 91 && c <= 96) || c >= 123)){
                b = 0;
                a++;
            }
         }
         if( b >= 1 ){
            a++;
         }
         System.out.println(a);
      }
   }
}

2011年3月28日 星期一

Java 因數分解


import java.util.Scanner;

public class Factorization {
   public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      StringBuffer str;
      Boolean zero;
      long a ;
      int b, index;
      String ss = "S";

      while(sc.hasNext()){
        zero = false;
        str = new StringBuffer();
        a = sc.nextLong();
        b = (int)Math.sqrt(a) + 1;
   
        for(long i = 2; i <= b; i++){
            int c = 0;
            if(a == 2){
               str.append(a + " * ");
               break;
            }
            for(long j = i; j <= b; j *= (zero == true ? i : 1)){
                if( a % j == 0){
                   str.append(j);
                   str.append(" * ");
                   a /= j;
                   c++;
                   ss = Long.toString(j) + " * ";
                   zero = false;
                }
                else{
                   if( c >= 2){
                      str.delete(str.indexOf(Long.toString(j)),str.lastIndexOf("*")+2);
                      str.append(Long.toString(j) + "^" + Integer.toString(c) + " * ");
                   }
                   c = 0;
                   zero = true;
                }
            }
        }
        index = str.length();

        if(index == 0){
            str.append(a);
        }
        else{
            if(ss.equals(str.toString()) == true){
               str.append(" * " + a);
            }
            str.delete(index-3,index);
        }
        System.out.println(str);
      }
   }
}

2011年3月22日 星期二

Java 阿拉伯數字轉國字

最大顯示到拾億,也就是10位阿拉伯數字


import java.util.Scanner;

public class Chinese {
    static String [] chineseNumber = {"零","壹","貳","參","肆","伍","陸","柒","捌","玖"};
    static String [] chineseUnit = {"","拾","佰","仟","萬","拾萬","佰萬","仟萬","億","拾億"};
    static StringBuffer str;
    static Long money;
    static String input;
    static int index;
    static boolean naZero;
    static Scanner sc = new Scanner(System.in);
     
    public static void main(String args[]){
        while( sc.hasNext() ){
            str = new StringBuffer();
            money = sc.nextLong();
            input = money.toString();
            index = input.length();
            naZero = false;

            for( char a : input.toCharArray() ){
                index--;

                if( a > '0' ){
                    if( naZero ){
                        str.append(chineseNumber[0]);
                        naZero = false;
                    }
                    str.append(chineseNumber[(int)(a-'0')]+chineseUnit[index]);
                }
                else{
                    naZero = true;
                }
    
                String b = chineseUnit[index <= 7 ? 4 : 8];
                    int c = str.indexOf(b);
                    int d = str.lastIndexOf(b);
                    if( c == d ){
                        continue;
                    }
                    else{
                        str.deleteCharAt(c);
                    }
                }
   
                if( str.length() == 0 ){
                    str.append(chineseNumber[0]);
                }
   
                System.out.println(str);
        }
    }
}