UVA10106
题目链接:
使用两个char字符串来保存输入,然后进行对字符串进行反转,操作的时候就会方便一些。直接把计算的结果保存得另外一个int型数组中。具体看代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string.h> using namespace std; char number1[255]; char number2[255]; int answer[515]; void reverseNumber(char*); int main() { while(gets(number1)!=NULL&&gets(number2) != NULL) { memset(answer,0,sizeof(answer)); reverseNumber(number1); reverseNumber(number2); int length1=strlen(number1); int length2=strlen(number2); int answerLength=length1+length2; int i,j,carryBit=0; for(i=0;i<length1;i++) { for(j=0;j<length2;j++) { carryBit=answer[i+j]+(number1[i]-\'0\')*(number2[j]-\'0\'); answer[i+j]=carryBit%10; answer[i+j+1]+=carryBit/10; } } int flag=1; for(int index=answerLength;index>=1;index--) if(flag) { if(answer[index]) { cout<<answer[index]; flag=0; } } else cout<<answer[index]; cout<<answer[0]<<endl; } return 0; } /** reverse the string **/ void reverseNumber(char* number) { int length=strlen(number); for(int i=0,j=length-1;i<j;i++,j--) { char temp=number[i]; number[i]=number[j]; number[j]=temp; } }
版权声明:本文为bbsno1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。