题目 1003 A+B 九度Online Judge
2015-03-25 17:00
267 查看
题目描述:
给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。
输入:
输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。
输出:
请计算A+B的结果,并以正常形式输出,每组数据占一行。
样例输入:
样例输出:
第一种方法(自己写的,说实话,OJ真的很坑爹,真机调试一点问题都没有的,到上面就是不行。也是醉了):
第二种方法:
给定两个整数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
第一种方法(自己写的,说实话,OJ真的很坑爹,真机调试一点问题都没有的,到上面就是不行。也是醉了):
#include<iostream> #include <string> using namespace std; int Num(char N) //Áé¸ÐÀ´×ÔµÚ1010Ìâ { if (N=='0') { return 0; } if (N=='1') { return 1; } if (N=='2') { return 2; } if (N=='3') { return 3; } if (N=='4') { return 4; } if (N=='5') { return 5; } if (N=='6') { return 6; } if (N=='7') { return 7; } if (N=='9') { return 9; } else return -1; } int main() { int i,j; string str1; string str2; long num1=0,num2=0; while (cin>>str1>>str2) { for (i=0;i<str1.length();i++) { if (str1[i]==','||str1[i]=='-') //²îµã¾ÍºöÂÔ¸ºÊýµÄʱºò£¡ÒòΪÊǶÔ×Ö·ûÊý×é½øÐвÙ×÷£¬ËùÒԵĿ¼ÂÇ·ûºÅ //if(str1[i]>='9'&&str1[i]<='0') { continue; // num1=num1*10+str1[i]-'0'; } else num1=num1*10+Num(str1[i]); } for (j=0;j<str2.length();j++) { //if (str1[j]>='9'&&str1[j]<='0') if (str1[j]==','||str1[j]=='-') { continue; // num2=num2*10+str2[j]-'0'; } else num2=num2*10+Num(str2[j]); } if (str1[0]!='-'&&str2[0]!='-') //ÒòΪÓиººÅµÄÔµ¹Ê£¬ËùÒÔÒªÅжÏһϠ{ cout<<num1+num2<<endl; } if (str1[0]=='-'&&str2[0]!='-') { cout<<num2-num1<<endl; } if (str1[0]!='-'&&str2[0]=='-') { cout<<num1-num2<<endl; } num1=0;num2=0; } return 0; }
第二种方法:
#include<iostream> #include<string> using namespace std; int main() { string str1,str2; while(cin>>str1>>str2) { long num1 = 0; int i ; for(i=0; i < str1.length(); i++) { if(str1[i] <= '9' && str1[i] >= '0') { num1 = num1*10 + str1[i] - '0'; } } long num2 = 0; for(i=0; i < str2.length(); i++) { if(str2[i] <= '9' && str2[i] >= '0') { num2 = num2*10 + str2[i] - '0'; } } //++ if(str1[0] != '-' && str2[0] != '-') { cout<<num1+num2<<endl; } //+- if(str1[0] != '-' && str2[0] == '-') { cout<<num1-num2<<endl; } //-+ if(str1[0] == '-' && str2[0] != '-') { cout<<num2-num1<<endl; } //-- if(str1[0] == '-' && str2[0] == '-') { cout<<0-(num1+num2)<<endl; } } return 0; } /************************************************************** Problem: 1003 User: Carvin Language: C++ Result: Accepted Time:0 ms Memory:1520 kb ****************************************************************/
相关文章推荐
- 题目1065 输出梯形 九度Online Judge
- 题目1018 统计学生成绩 九度OnlineJudge
- 题目 1032 ZOJ 九度OnlineJudge
- 题目 1010 A + B 九度Online Judge
- 题目 1063 整数和 九度Online Judge
- 题目 1438 求两个数的最小公倍数 九度Online Judge
- 题目 1026 又一版 A+B 九度Online Judge
- 题目 1045 百鸡问题 九度Online Judge
- 题目 1062 分段函数 九度Online Judge
- 题目 1049 字符串去特定字符 九度Online Judge
- 题目1064 反序数 九度Online Judge
- 【Peking University Online Judge】1003(Hangover)
- LeetCode Online Judge 题目C# 练习 - Length of Last Word
- LeetCode Online Judge 题目C# 练习 - N-Queens
- LeetCode Online Judge 题目C# 练习 - Unique Binary Search Trees II
- UVa Online Judge - Volume I 题目和解答索引
- 九度——题目1003:A+B
- 九度OJ题目解答1003
- LeetCode Online Judge 题目C# 练习 - Subsets
- LeetCode Online Judge 题目C# 练习 - Unique Paths