当前位置: 代码迷 >> 综合 >> Java - PAT - 1022. D进制的A+B (20)
  详细解决方案

Java - PAT - 1022. D进制的A+B (20)

热度:39   发布时间:2023-10-09 21:10:04.0

题目链接:1022. D进制的A+B (20)


思路:

将十进制数转换成n进制数  ,1<n<=10  ,如果笔算就是把这个十进制数一直除,知道商是0,那么余数的倒序就是n进制数。这里的除即”/“去整,余数即”%“取余;


import java.util.Scanner;  
public class Main{  public static void main(String[] args){  Scanner sc = new Scanner(System.in);long a = sc.nextLong();long b = sc.nextLong();long d = sc.nextLong();long c  = a+b;StringBuilder str = (f(c,d));for(int i= str.length()-1 ;i>=0;i--){System.out.print(str.charAt(i));}System.out.println();}public static StringBuilder f(long a,long d){StringBuilder str = new StringBuilder();while(a/d!=0){str.append(a%d);a = a/d;}str.append(a%d);return str;}
}


  相关解决方案