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+=courc=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(".");}}
}
}
?
?