UVa 424 - Integer Inquiry

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

中文網址

Solution

單純做大數。

Code

UVa 424
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<stdio.h>
#include<string.h>
#define N 110

int main() {

char s[N];
int sum[N] = { 0 };
int i;

while (fgets(s, N, stdin) && !(s[0] == '0'&&s[1] == '\0'))
{

int len = strlen(s) - 1;
s[len] = NULL;

for (i = 0; i < len; i++)
{
sum[i] += s[len - i - 1] - '0';
if (sum[i] >= 10)
{
sum[i + 1] += sum[i] / 10;
sum[i] %= 10;
}
}
}

for (i = N - 1; i >= 0 && !sum[i]; i--);

while (i >= 0)
printf("%d", sum[i--]);
putchar('\n');

return 0;
}