0 thích 0 k thích
460 đã 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: 43460 - Xem được từ: 2018-01-20 09:00 - Hiệu lực đến: Không giới hạn
| 460 đã xem

4 Trả lời

0 thích 0 k thích
Hidden content!
#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;
}
trả lời bởi (-286 điểm)
đã sửa bởi
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 thích 0 k thích
Hidden content!
#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]);
}
trả lời bởi (-168 đ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>
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;
}
trả lời bởi (16 điểm)
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 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};
* * * *** * * * ** * ** * i;
** * *** * * ** *** x=0;

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

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

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


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

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

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


* ** *** ** ** * 0;
}
trả lời bởi (-193 điểm)
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.59.173
©2016-2024

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

0 thích 0 k thích
13 trả lời
[Exam] đã hỏi ngày 19 tháng 1 năm 2018 trong Final Exam
ID: 43463 - Xem được từ: 2018-01-20 09:00 - Hiệu lực đến: Không giới hạn
| 725 đã xem
0 thích 0 k thích
6 trả lời
[Exam] đã hỏi ngày 19 tháng 1 năm 2018 trong Final Exam
ID: 43462 - Xem được từ: 2018-01-20 09:00 - Hiệu lực đến: Không giới hạn
| 377 đã 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
| 437 đã xem
0 thích 0 k thích
12 trả lời
[Exam] đã hỏi ngày 19 tháng 1 năm 2018 trong Final Exam
ID: 43459 - Xem được từ: 2018-01-20 09:00 - Hiệu lực đến: Không giới hạn
| 854 đã 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
| 2.2k đã xem
12,783 câu hỏi
183,443 trả lời
172,219 bình luận
4,824 thành viên