一道簡簡單單的大數(shù)加法題,時(shí)間竟然卡在getchar()和putchar()上,我用scanf()和printf()硬是超時(shí)了,超時(shí)了啊親,這么坑爹的有木有啊,有木有?。?br />2602 Accepted 1136K
1938MS C++ 702B
以下是本題代碼:


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN 1000010
char num1[LEN];
int N;
int main()
{
int i, j;
char a, b;
scanf("%d", &N);
int count = N - 1;
getchar();
for(i = 0; i < N; i++)
{
a = getchar();
getchar();
b = getchar();
getchar();
num1[count] = a - '0' + b - '0';
count--;
}
int t = 0;
int t2;
for(i = 0; i < N + 2; i++)//carry bit
{
t2 = num1[i] + t;
num1[i] = t2 % 10;
t = t2 / 10;
}
for(i = N - 1; i >= 0; i--)//out
putchar(num1[i] + '0');
putchar(10);
//system("pause");
}
posted on 2012-08-11 15:33
小鼠標(biāo) 閱讀(329)
評論(0) 編輯 收藏 引用 所屬分類:
大數(shù)