NYoj A*B Problem
来源:互联网 发布:2016mac游戏排行榜 编辑:程序博客网 时间:2024/05/16 11:00
A*B Problem
时间限制:1000 ms | 内存限制:65535 KB
难度:2
- 描述
设计一个程序求出A*B,然后将其结果每一位相加得到C,如果C的位数大于等于2,继续将C的各位数相加,直到结果是个一位数k。
例如:
6*8=48;
4+8=12;
1+2=3;
输出3即可。
- 输入
- 第一行输入一个数N(0<N<=1000000),表示N组测试数据。
随后的N行每行给出两个非负整数m,n(0<=m,n<=10^12)。 - 输出
- 对于每一行数据,输出k。
- 样例输入
36 81234567 67454 1232
- 样例输出
345
代码如下:
#include<stdio.h>
#include<string.h>const int MAX=15;
int Mult(char *s1,char *s2)
{
int a[MAX],b[MAX],ans[MAX*2];
int len1=strlen(s1);
int len2=strlen(s2);
memset(ans,0,sizeof(ans));
for(int i=0;i<len1;++i)
a[len1-i-1]=s1[i]-'0';
for(int i=0;i<len2;++i)
b[len2-i-1]=s2[i]-'0';
for(int i=0;i<len1;++i)
for(int j=0;j<len2;++j)
ans[i+j]+=a[i]*b[j];
int sum=0;
for(int i=0;i<len1+len2;++i)
{
if(ans[i]>9)
{
ans[i+1]+=ans[i]/10;
ans[i]%=10;
}
sum+=ans[i];
}
return sum;
}
int main()
{
int T;
char s1[MAX],s2[MAX];
scanf("%d",&T);
while(T--)
{
scanf("%s%s",&s1,&s2);
int sum=Mult(s1,s2);
while(sum>9)
{
int t=sum;
sum=0;
while(t)
{
sum+=t%10;
t/=10;
}
}
printf("%d\n",sum);
}
return 0;
}
0 0
- NYOJ - A*B Problem
- NYOJ - A-B Problem
- NYoj A*B Problem
- NYOJ-A^B Problem
- NYOJ-A/B Problem
- NYOJ A-B Problem
- NYOJ A-B Problem
- NYOJ---A+B Problem II
- NYOJ 524 A-B Problem
- NYOJ - A+B Problem II
- NYOJ 485 A*B Problem
- NYOJ.524.A-B Problem
- nyoj-485-A*B Problem
- NYOJ 1 A+B Problem
- nyoj-473-A^B Problem
- nyoj 485 A*B Problem
- nyoj 524 A-B Problem
- NYOJ 485 A*B Problem
- 《锋利的jQuery》学习笔记
- 《深入浅出 Java Concurrency》目录
- HDU1241:Oil Deposits(DFS)
- ubuntu下安装 LiveSuit on Linux (64bit
- ubuntu(lubuntu)中安装搜狗输入法
- NYoj A*B Problem
- 《Android应用开发与系统改造实战》知识总结
- 【设计模式】IOC模式
- 你必须非常努力,才能看起来毫不费力
- BFPRT算法
- SeaJS简介一:由来,特点以及优势
- 汤姆猫
- MagicRecord 使用方法
- UVa 539 The Settlers of Catan