解法一
#include<iostream>
using namespace std;
int main(){
int a,b;
while(scanf("%d %d",&a,&b)!=EOF){
int buf1[20],buf2[20];
int size1 = 0,size2 = 0;
while(a!=0){
buf1[size1++] = a % 10;
a /= 10;
}
while(b!=0){
buf2[size2++] = b % 10;
b /= 10;
}
int ans = 0;
for(int i = 0; i < size1; i++){
for(int j= 0 ;j < size2;j++){
ans += buf1[i] * buf2[j];
}
}
printf("%d\n",ans);
}
return 0;
}
解法二
#include<iostream>
using namespace std;
int main(){
char a[11],b[11];
while(scanf("%s %s",&a,&b)!=EOF){
int ans = 0;
for(int i = 0;a[i] != 0;i++){
for(int j =0;b[j]!=0;j++){
ans += (a[i]-'0') * (b[j]-'0');
}
}
printf("%d\n",ans);
}
return 0;
}
解法1 :直接开数组,保存各个取余结果即可,这里是m<10。
#include<iostream>
using namespace std;
int main(){
int a,b,c;
while(scanf("%d %d %d",&a,&b,&c)!=EOF){
if(a == 0) break;
int d = b + c;
int e = d;
int buf[11];
int i = 0;
while(e != 0){
buf[i++] = e % a;
e /= a;
}
for(int k = 0;k < i;k++){
printf("%d",buf[k]);
}
printf("\n");
}
return 0;
}
解法2:更新下解法1的错误地方,注意题目要求的不超过整数的定义。
#include<iostream>
using namespace std;
int main(){
long long b,c;
int m;
while(scanf("%d",&m)!=EOF){
if(m == 0) break;
scanf("%lld %lld",&b,&c);
b += c;
int buf[50];
int i = 0;
while(b != 0){
buf[i++] = b % m;
b /= m;
}
for(int k = i;k > 0;k--){
printf("%d",buf[k]);
}
printf("\n");
}
return 0;
}