POJ 1503 Integer Inquiry

http://poj.org/problem?id=1503

题意:
给出n个数,计算和。

思路:

基础的大整数加法题。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include<vector>
 7 using namespace std;
 8
 9 const int maxn = 100 + 5;
10
11 char s[maxn][maxn];
12 int num[maxn][maxn];
13 int ans[maxn];
14 int max_len;
15 int n;
16
17 int add()
18 {
19     int i;
20     int t = 0;
21     for (i = 0; i < max_len; i++)
22     {
23         int k = 0;
24         for (int j = 0; j < n; j++)
25         {
26             k += num[j][i];
27         }
28         k += t;
29         ans[i] = k % 10;
30         t = k / 10;
31     }
32     while (t)
33     {
34         ans[i++] = t % 10;
35         t /= 10;
36     }
37     return i;
38 }
39
40 int main()
41 {
42     //freopen("D:\\txt.txt", "r", stdin);
43     n = 0;
44     max_len = 0;
45     memset(num, 0, sizeof(num));
46     while (gets(s[n]))
47     {
48         if (!strcmp(s[n], "0"))  break;
49         int len = strlen(s[n]);
50         max_len = max(max_len, len);
51         for (int i = len - 1; i >= 0; i--)
52             num[n][len - 1 - i] = s[n][i] - ‘0‘;
53         n++;
54     }
55     int cnt = add();
56     for (int i = cnt - 1; i >= 0;i--)
57         printf("%d", ans[i]);
58     printf("\n");
59     return 0;
60 }
时间: 2024-11-03 22:21:51

POJ 1503 Integer Inquiry的相关文章

POJ 1503 Integer Inquiry(大数相加,java)

题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @param args */ public static void main(String[] args) throws Exception { // 定义并打开输入文件 Scanner cin = new Scanner(System.in); BigInteger a, sum

poj 1503 Integer Inquiry(多个大数相加)

题目链接:http://poj.org/problem?id=1503 Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. ``This supercomput

POJ 1503 Integer Inquiry 大数 难度:0

题目链接:http://poj.org/problem?id=1503 1 import java.io.*; 2 import java.math.BigInteger; 3 import java.util.Scanner; 4 5 public class Main { 6 public static void main(String []args) throws IOException{ 7 Scanner scanner=new Scanner(System.in); 8 BigInt

POJ 1053 Integer Inquiry (大数加法,还是Java大法好)

Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32674   Accepted: 12789 Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he

POJ 1053 Integer Inquiry &amp;&amp; HDOJ 1047 Integer Inquiry (大数加法)

题目链接(POJ):http://poj.org/problem?id=1503 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=1047 Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30856   Accepted: 12007 Description One of the first users of BIT's new su

hdu1047 Integer Inquiry

/* Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15874 Accepted Submission(s): 4079 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He exte

hdu acm-1047 Integer Inquiry(大数相加)

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11678    Accepted Submission(s): 2936 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He ex

HDOJ 1047 Integer Inquiry

JAVA睑板.... Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12042    Accepted Submission(s): 3037 Problem Description One of the first users of BIT's new supercomputer was Chip D

UVa 424 Integer Inquiry 【大数相加】

解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当输入a的第一个字符为0的时候结束运算,输出结果.  Integer Inquiry  One of the firstusers of BIT's new supercomputer was Chip Diller. He extended his explorationof powers of 3