1001 A+B Format (string processing)

1001 A+B Format (string processing)

This kind of problem hardly needs data structure and algorithm basis, and is mainly realized through simple logic flow and judgment.

1. General idea

Given two integers a and b, the value range of a and b is [-1000000, 1000000], then calculate the sum of a and b, and output the sum of a and b, but the output cannot be directly output, but needs to be Convert to a standard format, the so-called standard format, that is, add a "," after each three-digit number, and do not add "," at the end of the number.

2. Basic idea:

Define two integer variables a and b, because the value range of the int type is roughly ±2^32, which is larger than the given value range. Add directly to get the result. However, the format of the result obtained at this time does not meet the requirements. To meet the requirements, it is necessary to convert the obtained integer into a string, and then operate on the string, that is, add "," in the corresponding position of the obtained string.

3. Problem solving process

This is the first PAT A-level question I have done, and I am a little unaccustomed to it.

3.1 warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]

When writing C code on PAT, warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]

Reason: there is a return value when using scanf

a.c: In function 'main':
a.c:5:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ^~~~~~~~~~~~~~

A bug unique to PAT that no other OJ has ever encountered.

#include <stdio.h>

int main() {
    int a;
    scanf("%d", &a);
    printf("%d", a);
    return 0;
}

Solution: Give scanf a return value when writing code

#include <stdio.h>
 
int main() {
    int a;
    if(scanf("%d", &a)){   
        printf("%d", a);
    }else{
        printf("error");
    }
    return 0;
}

3.2 Segmentation fault

weird error

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
long long a,b,c;
stack<int>d;

int main() {
	cin>>a>>b;
	c=a+b;
	if(c==0) {
		cout<<"0"<<endl;
	} else {
		int k;
		while(!d.empty()) {
			d.pop();
		}
		if(c<0) {
			cout<<"-";
			c=-1*c;
		}
		while(c>0) {
			k=c%10;
			d.push(k);
			c=c/10;
		}
		while(!d.empty()) {
			if(d.size()==1) {
				k=d.top();
				d.pop();
				cout<<k;
			}
			k=d.top();
			d.pop();
			cout<<k<<",";
		}
		cout<<endl;
	}
	return 0;
}

segfault

If a segmentation fault occurs, it is generally the following situations:

  1. Array out of bounds (high probability):
    1. Check if the array size is missing a 0 (the author has it before...), if the title requires 1000, it is recommended to write 1010
    2. Check whether there are individual subscripts out of bounds in the process of loop traversal.
    3. The cmp comparison function in the sort function must have a return value. For example, there is only one return. Don't write an if statement in front of it.
    4. If the array is relatively large, generally >10000 is considered large, please declare the array outside main
    5. For char array, if the title says that the characters are not more than 8, please declare it as 10+
    6. Write a printf in the loop body to see the intermediate results you want to see, maybe you will know where you wrote wrong
  2. stack overflow:
    1. Usually this happens in recursive programs, such as when DFS traverses trees or graphs, you can write a printf in the recursion to see the intermediate results you want to see, maybe you know where you wrote wrong

Of course, the problem with this code of mine is the size() function.

3.2.1 size() function

The reason is that the size() function returns an unsigned number, when a is empty, the binary value size is not -1, but treated as an unsigned integer, $0000 0000H (true value 0) + 1111 1111H = 2^{32} - 1$. Errors are bound to happen.

The solution is to write $ i + 1 < a.size()$, or i < ( i n t ) a . s i z e ( ) − 1 i < (int) a.size() - 1 i<(int)a.size()−1 is enough.

3.3 %1000 problem

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
long long a,b,c,d[100000];

int main() {
	cin>>a>>b;
	c=a+b;
	if(c==0) {
		cout<<"0"<<endl;
	} else {
		long long k=0,ans=0;
		if(c<0) {
			cout<<"-";
			c=-1*c;
		}
		while(c>0) {
			k=c%1000;
			d[ans++]=k;
			c=c/1000;
		}
		for(long long i=ans-1; i>0; i--) {
			cout<<d[i]<<",";
		}
		cout<<d[0]<<endl;
	}
	return 0;
}

The result was 15 points, and I thought for a long time why.

Find answers through the idea of ​​white box testing

Enter: 1 999
 output: 1,0
#include<iostream>
#include<cstdio>
#include<stack>
#include<iomanip>
using namespace std;
long long a,b,c,d[100000];

int main() {
	cin>>a>>b;
	c=a+b;
	if(c==0) {
		cout<<"0"<<endl;
	} else {
		long long k=0,ans=0;
		if(c<0) {
			cout<<"-";
			c=-1*c;
		}
		while(c>0) {
			k=c%1000;
			d[ans++]=k;
			c=c/1000;
		}
		if(ans==1) {
			cout<<d[ans-1];
		} else {
			cout<<d[ans-1]<<",";
			for(long long i=ans-2; i>0; i--) {
				cout<<setfill('0')<<setw(3)<<d[i]<<",";
			}
			cout<<setfill('0')<<setw(3)<<d[0]<<endl;
		}
	}
	return 0;
}

This is also AC

3.4 AC Code

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
long long a,b,c,d[100000];

int main() {
	cin>>a>>b;
	c=a+b;
	if(c==0) {
		cout<<"0"<<endl;
	} else {
		long long k=0,ans=0;
		if(c<0) {
			cout<<"-";
			c=-1*c;
		}
		while(c>0) {
			k=c%10;
			d[ans++]=k;
			c=c/10;
		}
		for(long long i=ans-1; i>=0; i--) {
			cout<<d[i];
			if(i>0&&i%3==0)
				cout<<",";
		}
	}
	return 0;
}

Tags: Algorithm

Posted by nz_mitch on Thu, 15 Sep 2022 21:33:37 +0530