您的位置:首页 > 其它

抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

2015-10-12 14:23 453 查看
数字的反转:
就是将数字倒着存下来而已。(*^__^*) 嘻嘻…… 

大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出。

详见代码。

1  while (a)           //将每位数字取出来,取完为止
2         {
3             num1[i]=a%10;   //将每一个各位取出存在数组里面,实现了将数字反转
4             i++;            //数组的变化
5             a/=10;
6         }


趁热打铁 例题:hdu 4554 叛逆的小明

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4554

叛逆的小明

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 818    Accepted Submission(s):
568


[align=left]Problem Description[/align]
叛逆期的小明什么都喜欢反着做,连看数字也是如此(负号除外),比如:
小明会把1234它看成4321;把-1234看成-4321;把230看成032
(032=32);把-230看成-032(-032=-32)。

现在,小明做了一些a+b和a-b的题目(a,
b为整数且不含前导0),如果给你这些题目正确的答案,你能猜出小明会做得到什么答案吗?
 
 

[align=left]Input[/align]
输入第一行为一个正整数T(T<=10000),表示小明共做了T道题。
接下来T行,每行是两个整数x,y(-1000000<=x,
y<=1000000), x表示a+b的正确答案,y表示a-b的正确答案。
输入保证合法,且不需考虑a或b是小数的情况。
 
 

[align=left]Output[/align]
输出共T行,每行输出两个整数s
t,之间用一个空格分开,其中s表示小明将得到的a+b答案,t表示小明将得到的a-b答案。
 
 

[align=left]Sample Input[/align]

3

20 6

7 7

-100 -140

 
[align=left]Sample Output[/align]

38 24
7 7
-19 -23
 
题目大意:中文题目,清晰明了~
详见代码。

1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4
5 using namespace std;
6
7 int num1[10],num2[10];
8
9 int main ()
10 {
11     int T;
12     scanf("%d",&T);
13     while (T--)
14     {
15         int x,y,a,b,i=0,j=0;
16         int p=0,q=0;
17         scanf("%d%d",&x,&y);
18         memset(num1,0,sizeof(num1));
19         memset(num2,0,sizeof(num2));
20         a=(x+y)/2;
21         b=x-a;
22         while (a)
23         {
24             num1[i]=a%10;
25             i++;
26             a/=10;
27         }
28         while (b)
29         {
30             num2[j]=b%10;
31             j++;
32             b/=10;
33         }
34         for (int k=0; k<i; k++)     //换成数字
35             p=num1[k]+p*10;
36         for (int l=0; l<j; l++)
37             q=num2[l]+q*10;
38         printf ("%d %d\n",p+q,p-q);
39     }
40     return 0;
41 }


大数加法:

数字很大,假如直接加。很容易超时!所以换一种方法就是直接取出来一位一位加,不过不能换成数字。

1 int l=l1>l2?l1:l2;      //在两个之间取一个最长的
2     for (int i=0; i<l; i++)
3     {
4         num3[i]=num1[i]+num2[i];    //一位一位加
5         if (num3[i-1]>9&&i>=1)      //考虑进位的问题,如果大于9就需要进位
6         {
7             num3[i]++;
8         }
9
10     }
11     if (num[l-1]>9)                 //最后一位的进位问题
12     {
13         num[l]++;                   //仍需要进位
14         l++;                        //长度需要加一
15     }


例题:hdu 1002 A + B Problem II

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 238717    Accepted Submission(s):
46010


[align=left]Problem Description[/align]
I have a very simple problem for you. Given two
integers A and B, your job is to calculate the Sum of A + B.
 
 

[align=left]Input[/align]
The first line of the input contains an integer
T(1<=T<=20) which means the number of test cases. Then T lines follow,
each line consists of two positive integers, A and B. Notice that the integers
are very large, that means you should not process them by using 32-bit integer.
You may assume the length of each integer will not exceed 1000.
 
 

[align=left]Output[/align]
For each test case, you should output two lines. The
first line is "Case #:", # means the number of the test case. The second line is
the an equation "A + B = Sum", Sum means the result of A + B. Note there are
some spaces int the equation. Output a blank line between two test
cases.
 
 

[align=left]Sample Input[/align]

2

1 2
112233445566778899 998877665544332211

 
 

[align=left]Sample Output[/align]

Case 1:
1 + 2 = 3

Case 2:

112233445566778899 + 998877665544332211 = 1111111111111111110
 
参考以上的详解。将数字一位一位取出来,倒过来相加,在输出~

1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4
5 using namespace std;
6
7 int main ()
8 {
9     int n,flag=1;
10     char a[8000],b[8000];
11     int num1[8000],num2[8000],ans[8000];
12     scanf("%d",&n);
13     while (n--)
14     {
15         int k=0,s=0;
16         scanf("%s%s",a,b);
17         printf ("Case %d:\n",flag++);
18         int len1=strlen(a);
19         int len2=strlen(b);
20         memset(num1,0,sizeof(num1));
21         memset(num2,0,sizeof(num2));
22         memset(ans,0,sizeof(ans));
23         int l=len1>len2?len1:len2;
24         for (int i=len1-1; i>=0; i--)
25         {
26             num1[k]=a[i]-'0';
27             k++;
28         }
29         for (int j=len2-1; j>=0; j--)
30         {
31             num2[s]=b[j]-'0';
32             s++;
33         }
34         for (int i=0; i<l; i++)
35         {
36             ans[i]=num1[i]+num2[i];
37             if (i>=1)
38                 if (ans[i-1]>9)
39                 {
40                     ans[i]++;
41                 }
42                 //cout<<num1[i]<<" "<<num2[i]<<" "<<ans[i]<<endl;
43         }
44         if (ans[l-1]>9)
45         {
46             ans[l]=1;
47             l++;
48         }
49         printf ("%s + %s = ",a,b);
50         for (int i=l-1; i>=0; i--)
51             printf("%d",ans[i]%10);
52         if (n)
53             printf ("\n");
54         printf ("\n");
55     }
56     return 0;
57 }


 

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