0 喜歡 0 不喜歡
2.7k 瀏覽

You are requested to help in writing a program to perform addition of two large number up to 50-digits. In C there is no builtin datatype for large number, but we can use characters and array to solve the problem.

Input: The input will contain two positive integers are separated by a blank, the two positive integers do not exceed 50-digits.

Output: Sum.

寫一個程式輸入兩個大數(最大50個位數),計算其總合。C語言不提供大數資料型態,但我們可以用字元和陣列來解決這個問題。

Example input:

999999999999999999999999999999 999999999999999999999999999999

Example output:

1999999999999999999999999999998

 

[考試] 最新提問 分類:Final Exam |
ID: 43460 - 從幾時開始: 2018-01-20 09:00 - 到幾時結束: 無限制
| 2.7k 瀏覽

4 個回答

0 喜歡 0 不喜歡
內容已隱藏
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char input[500],s1[500],s2[500],temp;

int i,space,sum[500],carry,temp2,flag=0;

int main(){
* * * **** ** ** * ** *** %s",&s1,&s2);
** *** * ** * ** ****** * ** * ** *

///get space
* ** * * ** ** *** ** * * *** * *** ****
*** ** * * ** ** * ** ** * * *** *** * * *** * *** ** '){space=i;}
    }

///get s1
* * ** **** ** ** * ** * * ** *** ';i++){
*** * * ** *** * ******* ** * * * ***** ** *
    }

* *** ***** * * * * ** ** *** * * **
///reverse input
** ***** * ***** ***** * *** * * **** * *
* * * ** ** ** ** * * * *** * * **** *** *** * ****
** ***** **** * ** ** * * * ******** ****** * * **** * ** **
**** * * *** *** * **** ** * ** * * ** * *** **** *** ***** * *
    }

///get reversed s2
* * * * *** * ** ** *** * ';i++){
* * * * * * * * * * ********* * *** * * *** *
    }

///reverse s1

** * * **** * * * ** * * * * * ** **
* * ** * ******* * * * * *** **** ******

* * ** * ** ** * ** * * ** * ** ** **** ** * ** *** ****
* *** * * **** * *** *** ** ***** * * * ** ***** *
    }

///sum s1 and s2
** ******* ** * * **** **
* ** ** * ** ** * * *** * * ** ** ** *
** ** * * ** * * ** * *** * **** * * ** ** *** ****** *
* * ** ** * ** * ** * * * * * ** *** *** * *** **** * ***
****** * ** ** ** * * *** * *** *** ** * * * **** *** ** **
* *** ** * **** * * ** ** * ** *** **
*** * * * **** * * ** ** ***** **** ***** ** * * *

    }

*** *** ** ** *** *** ** ******* rev:%s\n",input);
* ** * * ** * * * *** *** ** ** * rev:%s\n",s1);
**** * ****** ** * * ** *** *** rev:%s\n",s2);
* *** **** * *** * ** *** ** * * ** **** ** **** * *** * * ** * * ** **

///reverse output
* ** **** * * * ** ** ***** * ******* *
*** * * * ** ** ** *** ** ** *** ** * *

***** ** ** * ** * * * * **** * *** * **** **** *
* * ** ** * * ** *** ** ******** **** ****** ** *
    }

///print
* ****** ** ** * *** **** * ** ***
* * ** ** ** * * * * * * * *** ******** ** ** ***** ** * * **** ** ***
* * * * ****** * * * * ******* ** *** * ** * * *
* ** ****** ** * * ** *** ** ** ** * * ** * * ****** * * * ** * ** * ** * * * *
* ** ** * ** *** * **** * * * * **** * ** * *** * ** * * *** * ** * ** * **** * * *
** * ****** **** *** * * ***** * * * * * * ** ** **** * * * * * * **

*** ** * * * *** ***** * ****** * *** * * * ** **

    }
* ********** * ** * * * *** * * ** **
    return 0;
}
最新回答 用戶: (-286 分)
修改於 用戶:
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
prog.c: In function 'main':
prog.c:80:12: warning: multi-character character constant [-Wmultichar]
     printf('10');
            ^~~~
prog.c:80:12: warning: passing argument 1 of 'printf' makes pointer from integer without a cast [-Wint-conversion]
In file included from prog.c:1:0:
/usr/include/stdio.h:364:12: note: expected 'const char * restrict' but argument is of type 'int'
 extern int printf (const char *__restrict __format, ...);
            ^~~~~~
prog.c:80:5: warning: format not a string literal and no format arguments [-Wformat-security]
     printf('10');
     ^~~~~~
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 喜歡 0 不喜歡
內容已隱藏
#include<stdio.h>
#include<string.h>
int main()
{
** *** ** **** * * ** ** ** string1[50];
*** ** * * * * * * ** string2[50];

* ***** * ** ** * *** **** ** * **** ** string1);
* * * *** * ** *** ***** * * *** string2);
** ** * ** * * *** * arr[51]={0},i;

** ******* *** * * ** * * i<strlen(string1); i++)
***** ***** **** ****
* * *** *** *** * * *** *** * *** += string1[strlen(string1) - 1 - i] - 48;
    }

* ** * * ** *** i<strlen(string2); i++)
* * * ** * ** * * **
****** ** * * ** * ** * *** * ** * * ** += string2[strlen(string2) - 1 - i] - 48;
*** **** * *** ***

** ** * * *** * * *** i<50; i++)
* * ** *** **** ** * *
* * * * ** * ** * * * *** ******** * * * * * * > 9)
* * * * **** ** ** * * * * * * * ****
* * * ** **** * *** * * * * ** ** ** * * **** -= 10;
* * * * *** ** * *** ** * * * *** ****** * ** ** **** * *****
** **** * ** **** ***** * * * **** ** **
* * ** * * * ** **

** * ** * * *** * i>=0; i--)
* * * * ** *** *** **
* ** ***** * ** * * ** ******* * ** * * *** != 0)
* ** ***** ** * *** ** * ** *** **** **** *** * ** *** ** **** * *
* ** * *** ** * * * * *

* * ** ** ** ** * * * * * i--)
* ** ** * * * *** * ** ** ** * ** * ** *** * * ** ******* arr[i]);
}
最新回答 用戶: (-168 分)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 喜歡 0 不喜歡
內容已隱藏
#include <stdio.h>
int main (){
int s1,s2,l1,l2;
int num1[50],num2[50],sum[50];
printf("enter input 1: ");
*** *** *** ** ** **** ** *
printf("enter input 2: ");
* **** ** * * * ** * * **
for(l1=s1[l1]!='\0')
**** * * * ******* * ** *
for(l2=s2[l2]!='\0')
* *** ** **** *** * * * * * *
int carry=0;
int k=0;
int i=l1-1;
int j=l2-1;
*** * * * ** * *
{
* * * * **** ** ** * *** *****
**** ** ** ** * ** * * *
******* **** * * ** * * * * * *** * * ** ** *** * * ** *
* ** * *** * * * * * * *** ** *** ** ** **** * ** ** ******* * * * **
*** * * * * * **** * * ** * ** ** ** **** ** ***** * ** ** ** * *** *** **** * **
* * *** **** * * * ** if (j>=0)
** * * * * *** * ** **** ** *****
* * ** **** **** * *** ** *** * * * * *** ** ***** * *** * * ** *** *** ** * **
* * * * * * * ** *** ** * * ***** *** ***** * * * * * *
** ** **** * * * ** * * * ** *** *** * * * * if(carr>0)
******* * *** ***** ** * * * *** * ** *** * *
** *** * * ** * *** ** * * ***** * * * * **** * ** * ");
* *** * * ** * * ** ** * ** ** * * ** ** *** * *
}
for(k--;k>=0;k--)
** * ** *** ** * *
* * * **** * * * * ** ** * * *
* *** * * ***** *** * ** *
  return 0;
}
最新回答 用戶: (16 分)
0 0
prog.c: In function 'main':
prog.c:9:10: error: subscripted value is neither array nor pointer nor vector
 for(l1=s1[l1]!='\0')
          ^
prog.c:9:20: error: expected ';' before ')' token
 for(l1=s1[l1]!='\0')
                    ^
prog.c:9:20: error: expected expression before ')' token
prog.c:10:10: error: subscripted value is neither array nor pointer nor vector
   num1=s1[l1]-'0';
          ^
prog.c:11:10: error: subscripted value is neither array nor pointer nor vector
 for(l2=s2[l2]!='\0')
          ^
prog.c:11:20: error: expected ';' before ')' token
 for(l2=s2[l2]!='\0')
                    ^
prog.c:11:20: error: expected expression before ')' token
prog.c:12:12: error: subscripted value is neither array nor pointer nor vector
     num2=s2[l2]-'0';
            ^
prog.c:17:15: error: expected ')' before ';' token
 for(;i>=0;j>=0;i--;j--;k++)
               ^
prog.c:23:13: error: expected ';' before 'carry'
             carry=(num1[i--]+num2[j]+carry)/10;
             ^~~~~
prog.c:27:13: error: expected ';' before 'carry'
             carry=(num1[j--]+num2[j]+carry)/10;
             ^~~~~
prog.c:28:18: error: 'carr' undeclared (first use in this function)
         }else if(carr>0)
                  ^~~~
prog.c:28:18: note: each undeclared identifier is reported only once for each function it appears in
0 喜歡 0 不喜歡
內容已隱藏
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
* *** ** ** * ** * * * *** a[50],b[50];
** ** ** * **** **** *** c[51]={0};
* *** ** ** *** *** * ** i;
* * * ** * * * * * * x=0;

*** * ***** **** * * * * *** ** %s",a,b);

* ** ** ** * **** * ** * ** **** *** *
    {
* * * * * * *** ** **** * ** **** *** *** ** * ** * *
    }

* ******* ** ********* ******* ** ** ****** * *
** * * ***** * ** ***
* * * * *** ****** **** ****** * * ** * *** * * * ***
**** * * * * ** * * *


* * *** ******** * * ** ** ** **
* *** ** ** **** *
** *** ** * ** *** *** ** * * *** ** **** **** * **
* *** * ** * * * ** ** * *** ** *** *
* **** * ** **** ** *** * **** *** * * * * **** *** ****** ** * * * * * *
* * *** *** * ** * * ****** * * * * *** ** * * * *******
* * *** ** ** ** * *** ** ** * ** * * *
    }

* *** ** * * * ** ****** * * *
*** * ** ** * *** **
* * * *** *** * * *** * * * ** * * * != 0)
* **** * * ** ** ** * ** * * * * * ***** ** * *
* ** ****** ** ** ** ***** *** * * * * * **** * **** * * * ** * * ** * **
*** * ** *** * ** ** * * * * ** ** *** ****** *** ** ******* ** **** *******
**** * ** ** ** * *** ** ** **** **** *****
    }

* *** *** * * * *** * **** *** *
    {
** ** ** ** *** **** * * **** ** ** *** **** ** * ** ** * * *
    }


* * ******** ** ** ** * 0;
}
最新回答 用戶: (-193 分)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.69.130.93
©2016-2025

相關問題

0 喜歡 0 不喜歡
13 回答
[考試] 最新提問 1月 19, 2018 分類:Final Exam |
ID: 43463 - 從幾時開始: 2018-01-20 09:00 - 到幾時結束: 無限制
| 4.3k 瀏覽
0 喜歡 0 不喜歡
6 回答
[考試] 最新提問 1月 19, 2018 分類:Final Exam |
ID: 43462 - 從幾時開始: 2018-01-20 09:00 - 到幾時結束: 無限制
| 2.3k 瀏覽
0 喜歡 0 不喜歡
8 回答
[考試] 最新提問 1月 19, 2018 分類:Final Exam |
ID: 43461 - 從幾時開始: 2018-01-20 09:00 - 到幾時結束: 無限制
| 2.7k 瀏覽
0 喜歡 0 不喜歡
12 回答
[考試] 最新提問 1月 19, 2018 分類:Final Exam |
ID: 43459 - 從幾時開始: 2018-01-20 09:00 - 到幾時結束: 無限制
| 4.5k 瀏覽
1 喜歡 0 不喜歡
37 回答
[考試] 最新提問 1月 16, 2018 分類:Final Exam |
ID: 42299 - 從幾時開始: 2018-01-17 14:00 - 到幾時結束: 無限制
| 13.4k 瀏覽
12,783 問題
183,442 回答
172,219 留言
4,824 用戶