PAT(最近)

1022 D进制的A+B - PAT (Basic Level) Practice (中文)

加减位置调换

本来以为就是简单的 十进制转换为一个长的字符串 没想到在那个拼接字符串的时候 只需要简单的 加减位置调换就可以 避免使用麻烦的翻转函数

import java.util.Scanner;
public class twenty_two {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int d = scanner.nextInt();
        int c=a+b;
        String ll="";
        while(c>0){
            int cour=c%d;
            ll=cour+ll;  
// 方法一 真的是 想出来这个方法的真的是天才 
//因为字符串可以与整数通过相加而拼接 所以我只需要 
//调整cour这个余数加在ll的前面即可 
//可能是因为整数加多了下意识的就是ll+=cour
            c=c/d;
        }
//下面则是我的方法 再次引入一个翻转函数StringBuffer 总而言之不如他的
        StringBuffer re=new StringBuffer(ll);
        System.out.println(re.reverse());
}
}

1037 在霍格沃茨找零钱 - PAT (Basic Level) Practice (中文)

“.”不可以直接用 . 分割

它的意思是任意字符的意思  需要使用"\\." 

import java.util.Scanner;
public class thirty_seven {
public static void main(String[] args) {
    Scanner myin = new Scanner(System.in);
    String l=myin.nextLine();
    String PA[]=l.split(" ");
    //注意如果使用 "."分割时候必须加上"\\." 
    //因为单个 . 代表任意字符 不可行
    String P1[]=PA[0].split("\\.");
    String A2[]=PA[1].split("\\.");
    int a=0,b=0;
    a=Integer.parseInt(P1[0])*17*29+Integer.parseInt(P1[1])*29+Integer.parseInt(P1[2]);
    b=Integer.parseInt(A2[0])*17*29+Integer.parseInt(A2[1])*29+Integer.parseInt(A2[2]);    
    int ans=Math.abs(a-b);
    //System.out.println(ans);
    int cou[]= new int[3];
    cou[0]=ans/17/29;
    cou[1]=(ans-cou[0]*17*29)/29;
    cou[2]=ans-cou[0]*17*29-cou[1]*29;
     if(a>b){System.out.print("-");}
    for (int i = 0; i < 3; i++) {
       
        System.out.print(cou[i]);
        if(i!=2){System.out.print(".");}
    }
}
}

 

 

你可能感兴趣的:(java,算法,开发语言)