2018年7月30日 星期一

Java 明明愛明明

題目: 明明愛明明


import java.util.Scanner;

public class Palindrome{
    public static Scanner sc = new Scanner(System.in);
 
    public static void main(String args[]){
        while(sc.hasNext()){
            String str = sc.nextLine();
            int[] i = new int[26];
            for(char c : str.toCharArray()){
                if(c >= 'a' && c <= 'z'){
                    i[c-97] += 1;
                }
                else if(c >= 'A' && c <= 'Z'){
                    i[c-65] += 1;
                }
            }
            int o = 0;
            for(int j : i){
                if(j % 2 == 1){
                    o += 1;
                }
            }
            if(o > 1) System.out.println("no...");
            else System.out.println("yes !");
        }
    }
}

2018年7月29日 星期日

Java 數數愛明明

題目: 數數愛明明


import java.util.Scanner;

public class CountingTwo{
    public static Scanner sc = new Scanner(System.in);
 
 
    public static void main(String args[]){
        while(sc.hasNext()){
            Long n = sc.nextLong();
            Long f = ((1 + n)*n)/2;
            Long g = (f*(n+2))/3;
            System.out.println(f + " " + g);
        }
    }
}

參考網站

Java 明明愛數數

題目: 明明愛數數


import java.util.Scanner;

public class Counting{
    public static Scanner sc = new Scanner(System.in);
 
    public static void main(String args[]){
        while(sc.hasNext()){
            int n = sc.nextInt();
            int m = sc.nextInt();
            int total = n;
            int i,j=1;
            for(i = n+1; total <= m; i++){
                total += i;
                j++;
            }
            System.out.println( j );
        }
    }
}

Java 乘乘樂

題目: 乘乘樂


import java.util.Scanner;

public class Multi{
    public static Scanner sc = new Scanner(System.in);
 
    public static void main(String args[]){
        sc.nextInt();
        while(sc.hasNext()){
            Integer n = 1;
            for(char i : sc.next().toCharArray()){
                n *= Character.getNumericValue(i);
            }
            System.out.println(n);
        }
    }
}

Java You Cannot Pass?!

題目: You Cannot Pass?!


import java.util.Scanner;

public class PassScore{
    public static Scanner sc = new Scanner(System.in);
    public static int n,i,score;
 
    public static void main(String args[]){
        while(sc.hasNext()){
            n = sc.nextInt();
            score = 0;
   
            for(i = 1; i <= n; i++){
                score += sc.nextInt();
            }
   
            System.out.println((score > (59*n)) ? "no" : "yes" );
        }
    }
}

Java Print it all

題目: Print it all


import java.util.Scanner;

public class PrintAll{ 
    public static Scanner sc = new Scanner(System.in);
    public static int n,i;
 
    public static void main(String args[]){
        while(sc.hasNext()){
            n = sc.nextInt();
   
            for(i = 1; i < n; i++){
                if(i % 7 != 0){
                    System.out.print(i + " ");
                }
            }
            System.out.println();
        }
    }
}

2018年7月27日 星期五

Java 質數又來囉

題目: 質數又來囉

使用這篇文章判斷質數去修改

import java.util.Scanner;
import java.math.BigInteger;

public class Prime {
  
    public static boolean MillerRabin(BigInteger x, int d, int time, BigInteger n){
        BigInteger nDivOne = n.subtract( BigInteger.ONE );
        BigInteger TWO = BigInteger.valueOf(2);
        x = x.modPow(BigInteger.valueOf(d), n);
  
        if( !x.equals( BigInteger.ONE ) && !x.equals( nDivOne ) ){
   
            while( --time > 0 ){
                x = x.modPow(TWO, n);
                if( x.equals( nDivOne ) ){ return true; }
            }
            return false;
        }
        return true;
    }
 
    public static int d,s;
    public static boolean isPrime(int n){  
        d = n - 1;
        s = 0;
        while( d % 2 == 0 ){
            d /= 2;
            s++;
        }
  
        return MillerRabin(BigInteger.valueOf(2), d, s, BigInteger.valueOf(n)) &&
               MillerRabin(BigInteger.valueOf(7), d, s, BigInteger.valueOf(n)) &&
               MillerRabin(BigInteger.valueOf(61), d, s, BigInteger.valueOf(n));
    }

    public static Scanner sc = new Scanner(System.in);
    public static int a,b,total;
 
    public static void main(String[] args){
          
        while(sc.hasNext()){
            a = sc.nextInt();
            b = sc.nextInt();
            total = 0;
    
            do{
                if(a % 2 ==0 && a > 2 || a == 1){
                    a++;
                    continue;
                }
                switch( a ){
                    case 2: case 5: case 7: case 61:
                        total++;
                        a++;
                        break;
                    default:
                        switch( a % 10 ){
                            case 1: case 3: case 5: case 7: case 9:
                                total += isPrime(a) ? 1 : 0;
                                a+=2;
                                break;
                        }
                        break;
                }
            }while( a <= b );
   
            System.out.println(total);
        }
    }
}

Java 排序

題目: 排序


import java.util.Scanner;
import java.util.Arrays;

public class Sort{
    public static Scanner sc = new Scanner(System.in);
    public static int length,i;
    public static int[] a;
 
    public static void main(String args[]){
        while(sc.hasNext()){
            length = sc.nextInt();
   
            a = new int[length];
            for(i = 0; i < length; i++){
                a[i] = sc.nextInt();
            }
            Arrays.sort(a);
            for(i = 0; i < length; i++){
                System.out.print(a[i] + " ");
            }
            System.out.println();
        }
    }
}

Java 麥哲倫的陰謀

題目: 麥哲倫的陰謀


import java.util.Scanner;

public class GuessHat{
    public static Scanner sc = new Scanner(System.in);
    public static long n,m;
 
    public static void main(String args[]){
   
        while(sc.hasNext()){
            n = sc.nextLong();
            m = sc.nextLong();
   
            System.out.println((m != n) ? m+1 : m);
        }
    }
}

Java 提款卡密碼

題目: 提款卡密碼


import java.util.Scanner;
import java.lang.Math;

public class ATMCard{
    public static Scanner sc = new Scanner(System.in);
    public static String str;
    public static int length,a,b,i;
 
    public static void main(String args[]){
        while(sc.hasNext()){
            str = sc.nextLine();
   
            length = str.length();
            a = (int)(str.charAt(0));
            i = 0;
            while(++i < length){
                b = (int)(str.charAt(i));
                System.out.print(Math.abs(a - b));
                a = b;
            }
            System.out.println();
        }
    }
}

2018年7月26日 星期四

Java 完全平方和

題目: 完全平方和


import java.util.Scanner;
import java.lang.Math;

public class Square{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
  
        while(sc.hasNext()){
            int n = sc.nextInt();
   
            for(int i = 1; i <= n; i++){
                int a = sc.nextInt();
                int b = (int)Math.sqrt(sc.nextInt());
                int calc = 0;
    
                int s = (int)Math.sqrt(a);
                a = (a > 1) ? ((s*s < a) ? s + 1 : s) : 1;
    
                for(; a <= b; a++){
                    calc += (a*a);
                }
                System.out.println("Case " + i + ": " + calc);
            }
   
        }
    }
 
}

Java MOD3

題目: MOD3


import java.util.Scanner;

public class Mod3{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
  
        while(sc.hasNext()){
            int a = 0,b = 0,c = 0;
   
            for(int i = sc.nextInt(); i-- > 0;){
                switch (sc.nextInt() % 3){
                    case 0:
                        a++;
                        break;
                    case 1:
                        b++;
                        break;
                    case 2:
                        c++;
                        break;
                }
            }
   
            System.out.println(a + " " + b + " " + c);
        }
    }
}