MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
题目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=1230
题目描述:
纯粹的模拟题........
代码:
代码
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
string a, b;
int prim[40] = {1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113};
int x[30], y[30];
int main () {
while ( cin >> a >> b, a != "0" && b != "0" ) {
memset ( x, 0, sizeof(x) );
memset ( y, 0, sizeof(y) );
int j = 1, k = 1;
for ( int i = 0; i < a.size(); ++ i ) {
if ( a[i] != ',' ) {
x[j] = x[j] * 10 + a[i] - '0';
} else {
++ j;
}
}
for ( int i = 0; i < b.size(); ++ i ) {
if ( b[i] != ',' ) {
y[k] = y[k] * 10 + b[i] - '0';
} else {
++ k;
}
}
reverse ( x+1, x + j + 1 );
reverse ( y+1, y + k + 1 );
int f = 0;
int m = j;
int mm = k;
int t = 0;
if ( m > mm ) {
swap ( m, mm );
t = 1;
}
//cout << m << " " << mm << endl;
int z[30];
for ( int i = 1; i <= m; ++ i ) {
z[i] = x[i] + y[i] + f;
f = z[i] / prim[i];
z[i] %= prim[i];
}
z[m+1] = 0;
for ( int i = m + 1; i <= mm; ++ i ) {
int temp = 0;
if ( !t ) temp = y[i];
else temp = x[i];
z[i] = temp + f;
f = z[i] / prim[i];
z[i] %= prim[i];
}
if ( f ) {
z[++mm] = f;
}
for ( int i = mm; i > 0; -- i ) {
printf ( i == mm ? "%d" : ",%d",z[i] );
}
putchar ( 10 );
}
return 0;
}
#include <string>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
string a, b;
int prim[40] = {1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113};
int x[30], y[30];
int main () {
while ( cin >> a >> b, a != "0" && b != "0" ) {
memset ( x, 0, sizeof(x) );
memset ( y, 0, sizeof(y) );
int j = 1, k = 1;
for ( int i = 0; i < a.size(); ++ i ) {
if ( a[i] != ',' ) {
x[j] = x[j] * 10 + a[i] - '0';
} else {
++ j;
}
}
for ( int i = 0; i < b.size(); ++ i ) {
if ( b[i] != ',' ) {
y[k] = y[k] * 10 + b[i] - '0';
} else {
++ k;
}
}
reverse ( x+1, x + j + 1 );
reverse ( y+1, y + k + 1 );
int f = 0;
int m = j;
int mm = k;
int t = 0;
if ( m > mm ) {
swap ( m, mm );
t = 1;
}
//cout << m << " " << mm << endl;
int z[30];
for ( int i = 1; i <= m; ++ i ) {
z[i] = x[i] + y[i] + f;
f = z[i] / prim[i];
z[i] %= prim[i];
}
z[m+1] = 0;
for ( int i = m + 1; i <= mm; ++ i ) {
int temp = 0;
if ( !t ) temp = y[i];
else temp = x[i];
z[i] = temp + f;
f = z[i] / prim[i];
z[i] %= prim[i];
}
if ( f ) {
z[++mm] = f;
}
for ( int i = mm; i > 0; -- i ) {
printf ( i == mm ? "%d" : ",%d",z[i] );
}
putchar ( 10 );
}
return 0;
}