#include<iostream>#include<string.h>using namespace std;//PAT乙级真题D进制的A+B (20)voidDradixTransfrom(longlongsum,intD){if(sum/D==0)cout<<sum;else{DradixTransfrom(sum/D,D);cout<<sum%D;}}intmain(){longlongA,B;intD;cin>>A>>B>>D;DradixTransfrom(A+B,D);return0;}
#include<stdio.h> int main (){//the shorter,the better. int a,b,d,s[64]; for(;~scanf("%d%d%d",&a,&b,&d);){ for(a+=b,b=0;a!=0;s[b++]=a%d,a/=d); for(!b?(*s=0):--b;b>=0;printf(b?"%d":"%d\n",s[b]),--b); } }
//输入已经能保证不越界,直接调用类库的方法吧 import java.util.Scanner; /** * @ClassName Main * @Description TODO * @Author Wlison * @Date 2020/3/11 9:38 * @Version 1.0 **/ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int a = sc.nextInt(); int b = sc.nextInt(); int d = sc.nextInt(); System.out.println(Integer.toString(a+b,d)); } } }
import java.math.BigInteger; import java.util.Scanner; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); BigInteger A = new BigInteger(sc.nextInt()+""); BigInteger B = new BigInteger(sc.nextInt()+""); BigInteger D = new BigInteger(sc.nextInt()+""); A = A.add(B); StringBuilder r = new StringBuilder(); while(A.divide(D) != BigInteger.ZERO){ r.append(A.remainder(D)); A = A.divide(D); } r.append(A.remainder(D)); r = r.reverse(); System.out.println(r.toString()); } }
//写一个比较全的进制转换函数 #include <stdio.h> int buf[40]; //m进制数n转成十进制 int transform1(int n,int m) { int i =0; int t = 1; int ans = 0; while(n != 0) { ans += (n%10)*t; n /= 10; t *= m; } return ans; } //十进制数n转换成m进制 void transform2(int n,int m) { int i = 0; int j; if(n == 0) { printf("%d",0); } while(n != 0) { buf[i++] = n%m; n /= m; } for(j=i-1;j>=0;j--) { printf("%d",buf[j]); } printf("\n"); } //任何进制转化为m进制 ,思路:先转十进制,再转m进制 void transform(int n,int m) { int tmp; tmp = transform1(n,m); transform2(tmp,m); } int main() { int a,b,c; while(~scanf("%d %d %d",&a,&b,&c)) { transform2(a+b,c); } return 0; }
pat官网的测试点3是a加b的和为0的情况 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; vector<long long>arr; int main(){ long long a,b; int d; cin>>a>>b>>d; long long c=a+b; if(c==0) arr.push_back(c); else { while(c!=0){ arr.push_back(c%d); c/=d; } } for(int i=arr.size()-1;i>=0;i--){ cout<<arr[i]; } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int b=sc.nextInt(); int d=sc.nextInt(); int c=a+b; String str=""; while(c>0){ int temp=c%d; str=temp+str; c=c/d; } System.out.print(str); } }
#include<iostream> #include<vector> using namespace std; int main(){ int a, b, d; cin >> a >> b >> d; int m = a + b; vector<int> v1; while (m){ v1.push_back(m%d); m /= d; } for (int i = v1.size() - 1; i >= 0; i--) cout << v1[i]; cout << endl; system("pause"); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { long long int m,n,s; int b,i=0,j; char no[100000]; scanf("%lld %lld %d",&m,&n,&b); s=m+n; while(s) { no[i]=(s%b)+'0'; s=s/b; i++; } for(j=i-1;j>=0;j--) printf("%c",no[j]); return 0; }
package test; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; import java.util.Scanner; /** * @author lanxuewei * 输入两个非负10进制整数A和B(<=230 -1),输出A+B的D (1 < D <= 10)进制数。 * 输入描述: * 输入在一行中依次给出3个整数A、B和D。 * 输出描述: * 输出A+B的D进制数。 * 示例1 * 输入 * 123 456 8 * 输出 * 1103 * */ public class AAndB { //十进制数n转化为m进制 public List<Integer> transform1(int n, int m){ List<Integer> list = new ArrayList<Integer>(); if(n == 0){ list.add(0); } while(n != 0){ list.add(n%m); n /= m; } return list; } //逆序输出 public void invertedSequence(List<Integer> list){ ListIterator<Integer> li = null; //获得LIstIterator对象 for(li = list.listIterator(); li.hasNext(); li.next()){ //将游标定位到列表结尾 //不执行语句,循环只是让游标移动 } for(; li.hasPrevious(); ){ System.out.print(li.previous()); } } public static void main(String[] args) { //输入A B D Scanner in = new Scanner(System.in); int A = in.nextInt(); int B = in.nextInt(); int D = in.nextInt(); int result = A + B; AAndB test = new AAndB(); //调用进制转化以及输出函数 test.invertedSequence(test.transform1(result, D)); } }
}