1 条题解
-
0
C++ :
#include <bits/stdc++.h> using namespace std; const int N = 210; int a[N], b[N], c[N], n; void convert(int a[], string s) { int len = s.size(); for (int i = 0; i < len; i++) if (s[i] >= '0' && s[i] <= '9') a[len-i-1] = s[i] - '0'; else a[len-i-1] = s[i] - 'a' + 10; } int main() { cin >> n; string s1, s2; cin >> s1 >> s2; int len1 = s1.size(); int len2 = s2.size(); convert(a, s1); convert(b, s2); int len = max(len1, len2); int t = 0; for (int i = 0; i < len; i++) { t += a[i] + b[i]; c[i] = t % n; t /= n; } if (t > 0) c[len++] = t; for (int i = len - 1; i >= 0; i--) if (c[i] >= 10) cout << char(c[i] + 'a' - 10); else cout << c[i]; return 0; }
Pascal :
program gjdjf; var a,b,c:array[1..210]of integer; lena,lenb,lenc,i,s:longint; procedure datain; var i:longint; n1,n2:string; begin readln(s); readln(n1); lena:=length(n1); readln(n2); lenb:=length(n2); for i:=1 to lena do begin case n1[i] of '0'..'9':a[lena-i+1]:=ord(n1[i])-48; 'a'..'z':a[lena-i+1]:=ord(n1[i])-87; end; end; for i:=1 to lenb do begin case n2[i] of '0'..'9':b[lenb-i+1]:=ord(n2[i])-48; 'a'..'z':b[lenb-i+1]:=ord(n2[i])-87; end; end; end; procedure work; var x,i:longint; begin i:=1; x:=0; while (i<=lena)or(i<=lenb) do begin c[i]:=a[i]+b[i]+x; x:=c[i] div s; c[i]:=c[i] mod s; inc(i); end; if x>0 then begin lenc:=i; c[i]:=x; end else lenc:=i-1; end; procedure dataout; var i:longint; begin for i:=lenc downto 1 do begin case c[i] of 0..9:write(c[i]); 10..35:write(chr(c[i]+87)); end; end; end; begin datain; work; dataout; end.
- 1
信息
- ID
- 509
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者