标签:伤感
好久没写C程序,吃力啊,菜成狗。。。http://www.smartoj.com/p/1001
python四行代码java10多行,C居然写了70+,无聊ing。。。注意0000016+0000000000000003=19; 0+0=0;就是说忽略前导0的时候得考虑两个数都是0的情况。
其他的是个人都写得出。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
BigInteger bi = new BigInteger(scan.next());
BigInteger bi2 = new BigInteger(scan.next());
System.out.println(bi.add(bi2));
}
}
}#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char a[1005], b[1005];
void addAndOutput(char* longer, char* shorter){
/*init*/
int longer_length = strlen(longer);
int shorter_length = strlen(shorter);
int p = longer_length-1;
int q = shorter_length-1;
/*实现加法*/
int increase = 0, headFlag = 0;
while(q>=0){
if((longer[p]+shorter[q]-2*'0'+increase) >= 10){
longer[p] = (longer[p]+shorter[q]-2*'0'+increase)%10 + '0';
increase = 1;
}
else{
longer[p] = (longer[p]+shorter[q]-2*'0'+increase)%10 + '0';
increase = 0;
}
p--, q--;
}
/*若还有进位处理*/
if(increase){//有进位
if(p>=0){//未到头
while(p>=0 && increase!=0){
if(longer[p]-'0'+1>=10){
longer[p] = (longer[p]-'0'+1)%10 + '0';
increase = 1;
}else{
longer[p] = longer[p]+1;
increase = 0;
}
p--;
}
if(increase==1){
putchar('1');
headFlag = 1;
}
}else{//已经到头
putchar('1');
headFlag = 1;
}
}
/*去前导0*/
int k=0, zeroFlag=1;
if(!headFlag){
for(k = 0; k < longer_length; k++)
if(longer[k] != '0'){
zeroFlag = 0;
break;
}
}
if(zeroFlag) printf("0");
else for(int i = k; i < longer_length; ++i) printf("%c", longer[i]);
puts("");
}
int main(){
while(~scanf("%s%s", a, b)){
if(strlen(a) < strlen(b)) addAndOutput(b, a);
else addAndOutput(a, b);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
}
return 0;
}
标签:伤感
原文地址:http://blog.csdn.net/bw761813/article/details/41701583