describe
Find the sum of two nonnegative integers with no more than 200 bits.
input
There are two lines. Each line is a non negative integer with no more than 200 bits. There may be redundant leading zeros.
output
One line is the result of addition. The result cannot have redundant leading 0, that is, if the result is 342, it cannot be output as 0342.
I don't want to talk about it myself, because this question tests your ideas, which is the first difficulty in oj (in fact, I'm lazy and it's too hard to explain it). However, I see that many students are stuck in this problem and have nothing to do. I feel very sad. It's a pity that the precious time to brush the problem has been wasted. So I wrote this article for those students who have thought for a long time but really don't have any ideas (those who haven't thought about it had better not look, because it's just my idea, maybe you can think of something better. This article just helps these students make this problem with what I think is a "stupid" method to expand their limited ideas) there's not much nonsense, We officially started.
Difficulty 1: the difficulty of this problem is that the data he gives is too large, far beyond the range of int or even long long (only limited to c, python and java. There are no such problems). Therefore, the first step is to know how to input data
I input in the form of string. Why use string instead of array
Because the string input is very convenient, directly a% s, and the data of this line can be input directly (you can also use gets, I like to use gets). We can easily define a string with a large length, so as to ensure that our data can be input.
This is not the complete code, just the input code. I defined a string with a length of 201 (the title should not exceed the length of 200)
#include<stdio.h> #include<string.h> int main(){ char x[201]; char y[201]; gets(x); gets(y); int len1=strlen(x),len2=strlen(y); //Use the strlen function to detect the length of the string }
Here we need to measure the length of our input data (what's the use will be explained later). When using strlen() function, we need to add a #include < string. H > (the same reason as math.h)
Function of strlen: measure the length of a string. For example, if I enter a "1000000" string, the length can be measured as 7 with strlen.
Difficulty 2: I input in the form of string, but it will be troublesome to use string in calculation, and it is more convenient to calculate after reordering (there is one step in addition).
So I convert the data in the string into the form of int and transfer it back to the array (after all, this chapter uses the array, so it's easier to explain it back)
int a[201]={0}; //Represents the string x (all initialized to 0) int b[201]={0}; //Represents the string y int c[201]={0}; //This is the output array c that I defined int len=201; //Because the array size I defined is 201, I define a variable len with a length of 201 for(int i=0;i<len1;i++){ a[len-len1]=x[i]-'0'; //Here's the specific explanation. Look down (flag1) len++; } len=201; for(int i=0;i<len2;i++){ b[len-len2]=y[i]-'0'; len++; }
A pseudo code is written here to substitute the data of the string into the number
flag1: Here's why I set len=200. My idea is to arrange numbers from back to front, that is, we usually see the method of sorting numbers. In this way, it is more intuitive and easy to understand, so I arrange numbers one by one, from the largest bit to the smallest bit (see the table below)
Suppose we enter an 1890, which is the case in the array
Array sequence number | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 |
input data | 0 | 0 | 0 | 0 | 1 | 8 | 9 | 0 | The last one is empty |
In this way, in the subsequent calculation, we can add the same bit arrays. Every 10 is one. This is what we should do next. This step is relatively simple. I'll put it out directly
if(len1>=len2){ //Here I divide it into two cases (the equal case can be put anywhere) for(int i=200;i>=200-len1;i--){ //The advantage of dividing into two cases is that you can know the length of the largest data and cycle to one more than the largest bit (addition can only enter one bit) c[i]+=a[i]+b[i]; //We put the values of array a+b into array c if(c[i]>=10){ //One in every 10 c[i]-=10; c[i-1]+=1; } } int flag=0; //Here, a flag is defined to facilitate the previous reset and ensure that 012 will not be output for(int i=0;i<=200;i++){ if(flag==0&&c[i]!=0){ //We can change the flag from the initial position to the back row until the maximum item that is not 0, so that the subsequent data (including itself) can be output flag=1; } if(flag==1){ printf("%d",c[i]); } } } else{ for(int i=200;i>=200-len2;i--){ //The same as above, but the longest value of the data is changed c[i]+=a[i]+b[i]; if(c[i]>=10){ c[i]-=10; c[i-1]+=1; } } int flag=0; for(int i=0;i<=200;i++){ if(flag==0&&c[i]!=0){ flag=1; } if(flag==1){ printf("%d",c[i]); } } }
The above is my personal practice of large integer addition. It is relatively simple and naive. I hope you can understand it.
Now send out the complete code (if you think it's useful to you, you might as well like it)
#include<stdio.h> #include<string.h> int main(){ char x[201]; char y[201]; int a[201]={0}; int b[201]={0}; int c[201]={0}; gets(x); gets(y); int len1=strlen(x),len2=strlen(y); int len=201; for(int i=0;i<len1;i++){ a[len-len1]=x[i]-'0'; len++; } len=201; for(int i=0;i<len2;i++){ b[len-len2]=y[i]-'0'; len++; } if(len1==1&&len2==1){ if(a[200]==0&&b[200]==0){ printf("0"); return 0; } } if(len1>=len2){ for(int i=200;i>=200-len1;i--){ c[i]+=a[i]+b[i]; if(c[i]>=10){ c[i]-=10; c[i-1]+=1; } } int flag=0; for(int i=0;i<=200;i++){ if(flag==0&&c[i]!=0){ flag=1; } if(flag==1){ printf("%d",c[i]); } } } else{ for(int i=200;i>=200-len2;i--){ c[i]+=a[i]+b[i]; if(c[i]>=10){ c[i]-=10; c[i-1]+=1; } } int flag=0; for(int i=0;i<=200;i++){ if(flag==0&&c[i]!=0){ flag=1; } if(flag==1){ printf("%d",c[i]); } } } return 0; }