浙江大学10年计算机上机题(含答案)(DOC)
一、A+B
题目描述:
给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。
输入:
输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。
输出:
请计算A+B的结果,并以正常形式输出,每组数据占一行。
样例输入:
-234,567,890 123,456,789
1,234 2,345,678
样例输出:
-111111101
2346912
答案:
给大家记住数据吧,如果你是用高进度计算的。
注意:你的结果是否有前缀0;0是否带负号;
999 1
10,000 -9
0 0
-0 -0
0 -0
1999 10
10,001 -10,000
#include
#include
#include
#include
using namespace std;
int main()
{
string strA,strB;
while(cin>>strA>>strB)
{
long numA = 0;
for(int i=0; i<=strA.length()-1; i++)
{
if(strA[i] <= '9' && strA[i] >= '0')
{
numA = numA*10 + strA[i] - '0';
}
}
long numB = 0;
for(int//去掉否则出现变量重复i=0; i<=strB.length()-1; i++) {
if(strB[i] <= '9' && strB[i] >= '0')
{
numB = numB*10 + strB[i] - '0';
}
}
if(strA[0] != '-' && strB[0] != '-')//++
cout< if(strA[0] != '-' && strB[0] == '-')//+- cout< if(strA[0] == '-' && strB[0] != '-')//-+ cout< if(strA[0] == '-' && strB[0] == '-')//-- cout<<0-(numA+numB)< } return 0; } 已AC 1.//可以用sscanf把字符串转换为int,然后直接加就可以了 2.#include 3.#include 4.#include 5. https://www.360docs.net/doc/bb16278462.html,ing namespace std; 7. 8.const int Maxn = 50; 9.int main() 10.{ 11. char str1[Maxn],str2[Maxn]; 12. char temp[Maxn]; 13. int a,b,i,r; 14. while(scanf("%s%s",str1,str2)!=EOF) 15. { 16. int len = strlen(str1); 17. memset(temp,0,sizeof(temp)); 18. r = 0;