TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre class="cpp" snippet_file_name="blog_20131202_1_2646179" code_snippet_id="91880" name="code"><pre class="cpp" snippet_file_name="blog_20131202_1_2646179" code_snippet_id="91880" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131210_3_3111195" class="cpp" name="code">/************************************************************************/
- /*
- 77. 大数相加。
- 写一个程序,实现对任意长的两个大整数(例如100位)进行相加。每个数可用以下形式存放,
- 例如整数179534679198可放于数组N中,其中:
- N[1]=198, N[2]=679, N[3]=534, N[4]=179
- 把两个数组中的元素一一相加,并根据需要进行进位。
- 要求:
- 1) 输出一个大数时中间不允许有空格, 如上述N数组输出时不允许输出:
- 179 534 679 198
- 而应输出
- 179534679198
- 2) 输出的数据中间的零不能用空格代替。
- 假设N[1]=23, N[2]=5, N[3]=534, N[4]=179时应输出
- 179534005023
- 而不应输出
- 179534 5 23
- 思考: 一个数组元素存放一位数时应如何修改程序?
-
- */
- /************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- int GETWS77(int num)
- {
- int count=0;
- while(num)
- {
- count++;
- num/=10;
- }
- return count;
- }
- void Printfarr(int * arr,int n)
- {
- int i=0;
- while(arr==0)
- i++;
- for(;i<n;i++)
- {
- if (GETWS77(arr)==3)
- {
- printf("%d",arr);
- }
- else
- {
- for (int j=0;j<3-GETWS77(arr);j++)
- { printf("%d",0);
- }
- printf("%d",arr);
- }
- }
- printf("\n");
- }
- int * ADD77(int *arr1,int * arr2,int n)
- { int JW=0;
- for (int i=n-1;i>=0;i--)
- {
- if (arr1+arr2+JW>1000)
- {
- arr1=(arr1+arr2+JW)%1000;
- JW=(arr1+arr2+JW)/1000;
- }
- else
- {
- arr1=(arr1+arr2+JW);
- JW=0;
- }
- }
- return arr1;
- }
- void main()
- {
- int arr[30]={0};
- arr[25]=192;
- arr[26]=123;
- arr[27]=154;
- arr[28]=176;
- arr[29]=98;
- Printfarr(arr,30);
- int brr[30]={0};
- brr[25]=192;
- brr[26]=123;
- brr[27]=154;
- brr[28]=176;
- brr[29]=998;
- Printfarr(brr,30);
- ADD77(arr,brr,30);
- Printfarr(arr,30);
- system("pause");
- }</pre><br><br><br></pre></pre></pre>
|
|