题解 | #A+B#
A+B
https://www.nowcoder.com/practice/b183eac8dfba4de99d47c1ca4ce9571f
#include <stdio.h> #include<string.h> int main() { char temp; char a[100][20]={0}; int len=0,j=0; while(scanf("%c",&temp)!=EOF){ if(temp=='\n'||temp==' '){len++;j=0;} else{ if(temp==','){} else{a[len][j]=temp;j++;} } } int n; if(len%2==1){ n=(len+1)/2;} else{n=len/2;} for(int i=0;i<n;i++){ int len1=strlen(a[i*2]); int len2=strlen(a[i*2+1]); if(a[i*2][0]=='-'&&a[i*2+1][0]!='-'){ int e=1;long value1=0,value2=0; for(int t=len1-1;t>0;t--){ value1-=(int)(a[i*2][t]-'0')*e; e*=10; } e=1; for(int t=len2-1;t>=0;t--){ value2+=(int)(a[i*2+1][t]-'0')*e; e*=10; } value1=value1+value2; printf("%ld\n",value1); } else if(a[i*2][0]=='-'&&a[i*2+1][0]=='-'){ int e=1;long value1=0,value2=0; for(int t=len1-1;t>0;t--){ value1-=(int)(a[i*2][t]-'0')*e; e*=10; } e=1; for(int t=len2-1;t>0;t--){ value2-=(int)(a[i*2+1][t]-'0')*e; e*=10; } value1=value1+value2; printf("%ld\n",value1); } else if(a[i*2][0]!='-'&&a[i*2+1][0]!='-'){ int e=1;long value1=0,value2=0; for(int t=len1-1;t>=0;t--){ value1+=(int)(a[i*2][t]-'0')*e; e*=10; } e=1; for(int t=len2-1;t>=0;t--){ value2+=(int)(a[i*2+1][t]-'0')*e; e*=10; } value1=value1+value2; printf("%ld\n",value1); } else if(a[i*2][0]!='-'&&a[i*2+1][0]=='-'){ int e=1;long value1=0,value2=0; for(int t=len1-1;t>=0;t--){ value1+=(int)(a[i*2][t]-'0')*e; e*=10; } e=1; for(int t=len2-1;t>0;t--){ value2-=(int)(a[i*2+1][t]-'0')*e; e*=10; } value1=value1+value2; printf("%ld\n",value1); } } return 0; }