1 thích 0 k thích
7.5k đã xem

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

 

[Exam] đã hỏi trong Final Exam
ID: 42300 - Xem được từ: 2018-01-17 14:00 - Hiệu lực đến: Không giới hạn

chọn từ khóa lại bởi | 7.5k đã xem

32 Trả lời

0 thích 0 k thích
Hidden content!
#include <stdio.h>
#include * * * * * * **
#include **** **** ***
char a[100],sum[200],space,i;
int main()
{
** *** ** ** ***** * **** * *

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

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


**** ** ***** * * * * ********* 0;
}
trả lời bởi (-286 điểm)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 thích 0 k thích
Hidden content!
** ** * **** ** **
** * * * ***
* ** * * *** **** *
int main(){



}
trả lời bởi (-32 điểm)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 thích 0 k thích
Hidden content!
#include<stdio.h>
#include<string.h>
int main (void)
{
** ** * * * * *** * num1[51],num2[51];
* * * *** **** **** * i,m;
** * ***** ***** **** * arr[51]={0};
** * * *** * * * * * ***
* ****** * ** * ** *** * * %s",num1,num2);
   for (i=0;i<(strlen(num1));i++)
    {
* ** * **** * * * ** **** *** * ** * **** **** * * *
    }
* *** * ****** * *** ** (i=0;i<(strlen(num2));i++)
* **** * * * ** * **
* *** *** ** *** ** * *** * * * ******* ** * * * * *** *
** ** *** * ***** * * *
*** ** * **** ***** * * (i=0;i<=50;i++)
* * *** ** * **** **
*** * ** * * ***** * * * ** ** ** * * * * ** *** *
*** *** ** * *** *** * * * * * **
***** ***** ***** ** ****** * * * **** * * ** **** * * ** ** * * ***
* ** ** ** * ** **** *** ** * ** ** * ** ** *** * * * * * *
**** * ** *** * * * *** * * * ** * * **
    }
**** * * * * * * * **** * * * * *
    {
** * ***** * * ** * **** * ** * * * * * *** *** *
** *** ** ** * *** ** ******* * * ** ** ** * * ** **** *
* * *** ** *** * * * * * ** *** * * * ** *** ****** **** * **** **
*** *** *** * * ** * **** *** ** * * * * ***** ******
** * ** * * **** ********* ** * * *** * ***** * **
* ** ** ******** * ** * * * ** * **** * ** *
* * * **** * * * * ** **** * * * * * * ** *** ************** * ***
****** *** * *** * * ** ****** * ** * * *** * * ********* ** * * *** * ***** * *
*** * * ** * * **** * *** ** *** * * * * ** *
    }
* ** * * ** ****** ** * * * * 0;

}
trả lời bởi (-168 điểm)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
0 thích 0 k thích
Hidden content!
#include<stdio.h>
#include<string.h>
#define n 50
int main()
{
** *** ** ** * * a[n],b[n];
* * ** * * * * *** **** * ** *** *** * **
** * *** *** * ** *** l1,l2;
*** * * * * * *** ** *** i=0;
* * * * * * * * *
* ** **** ****** * **** ***
* ** ** * * ****** **** * **** *** ** *
* ** **** ** ** * *** * ** * *** * **
* ** * *** *** * ** * * ******



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





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



}
trả lời bởi (-329 điểm)
đã sửa bởi
0 0
prog.c: In function 'main':
prog.c:7:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[50]' [-Wformat=]
     scanf("%s%s",&a,&b);
             ^
prog.c:7:15: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[50]' [-Wformat=]
     scanf("%s%s",&a,&b);
               ^
0 0
prog.c:28:1: error: expected identifier or '(' before '}' token
 }
 ^
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 thích 0 k thích
Hidden content!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
** ** * * * * * ** * a[50];

* **** * **** *** ** * * b[50];

** ****** *** *** ** *** * c[51] = {0};

** * * * **** ** *** ** ** * *** ** a, b);

* * ** * * * * *** * * i = 0;

* ******** * ** * len_a = strlen(a);

* ** * * * ***** * len_b = strlen(b);

* * * **** ** * * * = 0; i < len_a; i++)
* * * * * ** * ** * *
** ** * *** * * ** *** **** ** * * **** = a[len_a - i - 1] - '0';
* ** **** ** * * *
* ** ** **** ** = 0; i < len_b; i++)
* * ** ** * **
*** ** *** * ** * * *** * * *** * ** * += b[len_b - i - 1] - '0';
* ** * ** ** * * **** *

* * * * ** * * ** * * *** = 0; i < 51; i++)
* * * ** *** ** * **
* ******* ** *** ** *** * * * ***** * * ** * * ** > 9)
*** * * * * ***** ***** ** ** * * *** *
* *** *** ** ** * ***** * **** ** *** * * **** * ** * + 1]++;

** *** ** *** ** *** * * * ** * *** * * ** * ** ** * * ** ** ***** %= 10;
* **** * * *** * ** *** * * **
****** ** * ** * * *

***** * ** * ** * * ** * nonzero = 0;

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

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

******** *** **** *** * = nonzero; i > -1; i--)
***** * * ** * *** * **
***** * **** ******* * * * ** ** ** **** ** * *** * * * c[i]);
* * * ** ***** ** *
* ** ** * * ** * * * * ***** 0;
}
trả lời bởi (-285 điểm)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 thích 0 k thích
Hidden content!
#include<stdio.h>
#include<string.h>

int main()
{
    int i,e,begin=0;
    char a[51],b[51];
    int c[52]={0};
    scanf("%s",a);
    scanf("%s",b);

**** * *** * ***** ** * * *** *** * ** * * * * * ** ****
    for(i=0;i<strlen(a);i++)
* ** *** ** * * * * *** * * * *** * * * **
* ******* *** * * ** * *** ****** * *** ** **** *** * * ******* **
**** * * * ** ** ** * * ** * ** *****
    for(i=0;i<strlen(b);i++)
*** * * *** * * * * * ***** * **** ***** **** *
** ** ** * ** * * *** * * ****** ***** **** ** * * **** * * ** ** * *
* * * * * * * * * *** ** * * * * **** *
    for(i=0;i<50;i++)
** ** *** ** ** ** *** ** *** * **
** * * *** ** ** *** * * * **** * ** * **** * ** * *** ** ** * * > 9)
* * ** * *** * * * ** ** ** ************ * ** * ** * * * * *** * * ** ** *
** *** ** * ***** ** *** * * * * ***** * * ** * * * * * ** ****** *** * ***** * *** * * * **** ** * * * ** * **** * *** ** * *
*** ** *** ** * * * * * *** ** * *** ***** ** * * * *** * * * ** * ** * **** * ** * ***** * **** * ***
** * * *** ** * * * ** ** ** * * ** * ** ** ** * *** **** ** * *** * ** * * ***
** * ** * ***** ** * **** * ** *** * **** *
    for(e=51;e>0;e--)
* * * ** * * * * * ** * *** ** ** * * **
** ** * * ** * ** ** * * * ******* ** * * * * ** * *** * * ****** != 0)
** * *** * **** * *** ** **** * *** * ** ** * ***** * *** * *** * * *** ******* * ***
***** * ** * * * * ** * ** * ** * * * * ** ** ** * * * ** * * ***** * ** * * ** * *
** * * * **** * ** * * * ** * ** * * ** ** ** ** * * * * **** * * * * ** ** ***** ** * ** **** *** * **
** *** * *** * * *** * * * * * * ** ** **** * ** ***** ** * * ** * * *** ** ** * *
* ** * * * ** * * * ** * *
* * ** * ** * * * * ** ***** *** ** ******** * * * **
    for(i=begin;i>=0;i--)
* ** * *** * ** * * *** * ********* * ** * *
** * ** *** * ** * * * * *** ******** ***** *** * * * * *** * * * *** ***
** ** * ** *** * * ** * ****** *** ***
}
trả lời bởi (-301 điểm)
đã sửa bởi
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
0 thích 0 k thích
Hidden content!
#include <stdio.h>
#include <string.h>
int main()
{
* ** * * * ** *** i,j,k,l;
*** ** *** **** *** a[100];
* ** * ** * ** b[100];
* ** * * ****** ****** * *** ** * * ***
* ** ** ** ** *** * * * * * *** && ** * * ** * *
*** ******* * * * * * * *
* ** * * * * **** **** * ** * ******* * * ** ** * ****** * * *** ** * * **** *
** **** ** ** ** * *** ** *
**** **** * ** * ** * * * 0;
}
trả lời bởi (-229 điểm)
0 0
prog.c: In function 'main':
prog.c:9:12: warning: integer constant is too large for its type
     if (a==999999999999999999999999999999 && b==999999999999999999999999999999)
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:9:10: warning: comparison between pointer and integer
     if (a==999999999999999999999999999999 && b==999999999999999999999999999999)
          ^~
prog.c:9:49: warning: integer constant is too large for its type
     if (a==999999999999999999999999999999 && b==999999999999999999999999999999)
                                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:9:47: warning: comparison between pointer and integer
     if (a==999999999999999999999999999999 && b==999999999999999999999999999999)
                                               ^~
0 thích 0 k thích
Hidden content!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


   int main()
   {
* ** ***** * *** ***** **** ** * * ** *** a[50],b[50];
* * ** ** * ** ** * ****** * * * *** *** * c[51];
** * * **** **** ** ** * *** **** n1, n2;
* ** * ** * ***** * * * ** i,j;

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

* * * *** * * ** ** * * ** * *** **** *** **
       {
* * * * ** ********** * **** ***** ***** ** ** * * *** * ** * * );
** * ***** * * ** ** * *** ** * ** * *

**** *** * ** * *** **** * ** * **** ** * * *
* * **** ** * ** ***** * ** * ** *
* * ** **** **** ** ** * ** ******* **** ** *** * * * * * * *** ** *** * b[i]);
*** ** * ***** *** * * * ** **** *



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

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

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

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


    return 0;
}
trả lời bởi (-193 điểm)
đã sửa bởi
0 0
prog.c: In function 'main':
prog.c:13:14: warning: format '%[^
   ' expects argument of type 'char *', but argument 2 has type 'int (*)[50]' [-Wformat=]
    scanf("%[^\n]*c%",&a);
              ^
prog.c:13:19: warning: spurious trailing '%' in format [-Wformat=]
    scanf("%[^\n]*c%",&a);
                   ^
prog.c:14:14: warning: format '%[^
   ' expects argument of type 'char *', but argument 2 has type 'int (*)[50]' [-Wformat=]
    scanf("%[^\n]*c%",&b);
              ^
prog.c:14:19: warning: spurious trailing '%' in format [-Wformat=]
    scanf("%[^\n]*c%",&b);
                   ^
prog.c:17:17: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
    for(i=strlen(a),j=0;1<=0;i--,j++)
                 ^
In file included from prog.c:4:0:
/usr/include/string.h:394:15: note: expected 'const char *' but argument is of type 'int *'
 extern size_t strlen (const char *__s)
               ^~~~~~
prog.c:22:18: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
     for(i=strlen(b),j=0;1<=0;i--,j++)
                  ^
In file included from prog.c:4:0:
/usr/include/string.h:394:15: note: expected 'const char *' but argument is of type 'int *'
 extern size_t strlen (const char *__s)
               ^~~~~~
0 0
prog.c: In function 'main':
prog.c:14:18: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
        n1=strlen(a);
                  ^
In file included from prog.c:3:0:
/usr/include/string.h:394:15: note: expected 'const char *' but argument is of type 'int *'
 extern size_t strlen (const char *__s)
               ^~~~~~
prog.c:15:18: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
        n2=strlen(b);
                  ^
In file included from prog.c:3:0:
/usr/include/string.h:394:15: note: expected 'const char *' but argument is of type 'int *'
 extern size_t strlen (const char *__s)
               ^~~~~~
prog.c:19:20: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
            scanf("%d",a[i] );
                    ^
prog.c:24:20: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
            scanf("%d", b[i]);
                    ^
prog.c:46:21: warning: format '%d' expects a matching 'int' argument [-Wformat=]
            printf("%d");
                     ^
0 0
prog.c: In function 'main':
prog.c:19:20: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
            scanf("%d",a[i] );
                    ^
prog.c:24:20: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
            scanf("%d", b[i]);
                    ^
prog.c:46:21: warning: format '%d' expects a matching 'int' argument [-Wformat=]
            printf("%d");
                     ^
0 thích 0 k thích
Hidden content!
#include *** ** *** **
#include *** ** * ****
#include ** ** ***
int main ()
{
* * * * * * * * ** i,n,m,c=0;
*** * ** * * * A[60]={0};
* **** * ** * * * *** ** ** * *
*** ** ** * * * ** ******** ("%s * ** * *
* *** * * * * * ** * **** ** **** ** ** * ** * * ** ***
* * ***** * * * *** * *** 0;
}
trả lời bởi (-32 điểm)
đã sửa bởi
0 0
prog.c: In function 'main':
prog.c:14:23: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
     for (i=0;i<strlen(A);i++)
                       ^
In file included from prog.c:2:0:
/usr/include/string.h:394:15: note: expected 'const char *' but argument is of type 'int *'
 extern size_t strlen (const char *__s)
               ^~~~~~
0 0
prog.c: In function 'main':
prog.c:11:5: error: expected ';' before 'return'
     return 0;
     ^~~~~~
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Wrong output
0 thích 0 k thích
Hidden content!
#include<stdio.h>

int main() {
    char a[50], b[50], sum[50];
    int i,j,tmp=0;

    for(i=0;i<50;i++) {
****** **** **** ** * * * **** *** *** * **** * * **** *****
    }

* * *** * *** **** * * * * * ***** a);
* ****** * *** *** * * * **** * ** b);

    for(;;) {
* * * ** ** **** * ****** **** ******* ****** * * *** ** {
** ** ** * * * ** ***** * *** ** * * ** * * **** * * * * ** *** *
* * ** ** ***** * ***** ** * *** ** * * * ** * ** * * * * ** *** *** ** *
* * *** * *** ** ** * * *** ******* ****** * * * **** ** **** * ** * * *
* ** ** * **** * * * * * **** *****
* **** * * *** * ********* ** ** *** ***** ** ***** * * ** {
* * * * * * ** * ** * * *** * ** * ****** * * **
* *** **** *** * **** * * * * ** *** * ***
    }

    for(;;) {
** * **** ** * * ** * ** * *** * ** *** * {
**** *** ** * * *** * ** *** * ** *** ** *** *** ** ******** ** *** *
**** ***** * * **** ** * * ** ** * ** * * ** * ***** ** * ****** ** ** * * *
* * *** *** ** * ***** * * **** * * * *** * ** *** * ** *** *** *** **** * * * ***
** * *** * * ** * *** **** * * * * ** *
*** * * *** * ** * *** *** ** ** * * * {
** * ** ** ** ** **** * * * * * * * * * ** *** * ** * ** ***
* * *** * * ** ** ** * *** *** ** * **** *** **
    }

* ** * * ** ** ** * **** *** {
* ** * * * * ** * * * * * ***** * ** ** && a[i]<=57) {
* ** * ** *** ** *** ** *** * * * ** ** ** * ** *** * * *
** * *** ** * ** * * * ** ** *** ** * ***
**** * * * * * **** *** **** ** ** ** * * * ** * * && b[i]<=57) {
* **** * *** ** ** ** * * * * ** * * **** ** * ** *** **** *****
***** * *** * * * * * * ** *** * ***
    }

    for(j=50;j>0;j--) {
* * *** * * ** ** * *** * * * ******* **** * * * * *** {
* * ** * * ** * ** ** ** * ** * ** ** ** * * *** ** **** **
******** ** ***** **** * ***** ** ** * *** ** *
* **** * * **** ******** * * ** ** * * * *** * if(sum[j]+a[j]+b[j]==10) {
** ** ** *** ** * ** * *** ** * ***** * * * ***** * * * ** *** *
** * * **** * ** * * **** ** ** * **** * * * *** * * *
* ** ** ***** ** **** * * **** * ****** * ** *
* ** * * ** * *** * ******** ** * * ******* if(sum[j]+a[j]+b[j]>10) {
**** ** *** *** *** ** * ** **** ** * * ** ** * * *** * ** **** ***
* * * * **** ** *** * ****** * * * *** ** * *** * * * ** * * ** ** * *
** * ** ** ** * * ** * * *** * ** ***
    }

    for(j=0;j<50;j++) {
* * ** * * ** ****** * **** * * ** * * *** * ** {
****** * * *** * * *** ** *** ** **** * *** * * * * * *** ** * * *
* * ** ** * *** *** * *** *** * ** * ** ***
    }

    for(;j<=49;j++) {
* *** * *** ** *** * **** **** * **** * ** * * ** * ** * ** *** sum[j]);
    }
}
trả lời bởi (-120 điểm)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.194.153
©2016-2025

Những câu hỏi liên quan

1 thích 0 k thích
37 trả lời
[Exam] đã hỏi ngày 16 tháng 1 năm 2018 trong Final Exam
ID: 42302 - Xem được từ: 2018-01-17 14:00 - Hiệu lực đến: Không giới hạn
| 7.5k đã xem
2 thích 0 k thích
31 trả lời
[Exam] đã hỏi ngày 16 tháng 1 năm 2018 trong Final Exam
ID: 42301 - Xem được từ: 2018-01-17 14:00 - Hiệu lực đến: Không giới hạn
| 6.5k đã xem
1 thích 0 k thích
37 trả lời
[Exam] đã hỏi ngày 16 tháng 1 năm 2018 trong Final Exam
ID: 42299 - Xem được từ: 2018-01-17 14:00 - Hiệu lực đến: Không giới hạn
| 7.2k đã xem
2 thích 0 k thích
30 trả lời
[Exam] đã hỏi ngày 16 tháng 1 năm 2018 trong Final Exam
ID: 42298 - Xem được từ: 2018-01-17 14:00 - Hiệu lực đến: Không giới hạn
| 5.8k đã xem
0 thích 0 k thích
8 trả lời
[Exam] đã hỏi ngày 19 tháng 1 năm 2018 trong Final Exam
ID: 43461 - Xem được từ: 2018-01-20 09:00 - Hiệu lực đến: Không giới hạn
| 1.4k đã xem
12,783 câu hỏi
183,442 trả lời
172,219 bình luận
4,824 thành viên