您的位置:首页 > 产品设计 > UI/UE

UVA - 424 - Integer Inquiry

2014-04-16 21:18 429 查看
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=365

题意:

输入大数,最多100行,最长100位,非负,无小数。

解题:

先用字符串输入,再用二维整形数组保存。然后再一个一个加起来。

题目好像做过,忘了什么时候了,不过还是写得不简洁,0.009s过就算了~

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>

using namespace std;

const int MAXN = 110;

int main()
{
int Bign[MAXN][MAXN];
memset(Bign, 0, sizeof(Bign[0]) * MAXN);
int nCase;
string strIn;

nCase = 0;
while ( 1 )
{
getline(cin, strIn);
if ( strIn[0] == '0' )
{
break;
} // end if

nCase++;
Bign[nCase][0] = strIn.length();
for ( int i=1; i<=strIn.length(); i++ )
{
Bign[nCase][i] = strIn[strIn.length()-i]-'0';
} // end for
} // end while

int Sum[MAXN];
memset(Sum, 0, sizeof(Sum));
int total;
for ( int i=1; i<=nCase; i++ )
{
int next = 0;
int re = 0;
int k;
for ( k=1; k<=Sum[0] || k<=Bign[i][0] || next != 0; k++)
{
total = Sum[k] + Bign[i][k] + next;
next = total / 10;
re = total % 10;
Sum[k] = re;
} // end for
if ( k-1 > Sum[0] )
{
Sum[0] = k-1;
} // end if
} // end for

for ( int i=Sum[0]; i>=1; i-- )
{
cout <<Sum[i];
} // end for
cout <<'\n';

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: