| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
discuss 里这2个人的理解不一样,但是都ac了#include <iostream> #include <string> using namespace std; string strplus(string left, string right) { int large = left.size()>right.size() ? left.size() : right.size(); int small = left.size()<right.size() ? left.size() : right.size(); int flag = 0; if(left.size()>right.size()) flag=1; string ret=""; int carry=0; for(int i=0; i<small; i++) { int l=left[i]-'0'; int r=right[i]-'0'; int sum = l+r+carry; if(sum>=10) carry=1; else carry = 0; int reted = sum % 10; char c = '0'+reted; ret += c; } for(i=small; i<large; i++) { if(flag) { int l= left[i]-'0'; int sum = l+carry; if(sum>=10) carry = 1; else carry = 0; char c = '0' + sum%10; ret += c; } else { int l= right[i]-'0'; int sum = l+carry; if(sum>=10) carry = 1; else carry = 0; char c = '0' + sum%10; ret += c; } } if(carry == 1) { char c = '1'; ret += c; } int len=0; for(i=0; i<ret.size();i++) { if(ret[i]=='0') { len++; } else { break; } } ret.erase(0,len); return ret; } int main() { int cases; cin>>cases; string left, right; while(cases--) { cin>>left>>right; string ret = strplus(left,right); cout<<ret<<endl; } return 0; } 和 #include <stdio.h> #include <string.h> int reverse(int n) { char str[100]; int i = 0; while(n) { str[i++] = n % 10 + '0'; n /= 10; } str[i] = 0; sscanf(str, "%d", &n); return n; } int main() { int T, a, b; scanf("%d", &T); while(T--) { scanf("%d%d", &a, &b); printf("%d\n", reverse(reverse(a) + reverse(b))); } return 0; } 比如 102 002 一个输出104, 一个输出302 我用java将这2个理解的都写的,但是都是wa,晕 Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator