当前位置:网站首页>1001 A+B Format (string processing)

1001 A+B Format (string processing)

2022-08-10 17:29:00 A bigot is lovely

1001 A+B Format(字符串处理)

在这里插入图片描述

These kinds of problems rarely require data structures,算法基础,It is mainly realized through simple logic flow and judgment.

1、大致题意

给出两个整数a和b,a和b的取值范围为[-1000000,1000000],然后计算a和b的和,并且输出a和b的和,But when outputting, it cannot be output directly,Instead, it needs to be converted to a standard format,The so-called standard format,That is, add one after every three digits“,”,The number does not need to be added at the end“,”.

2、基本思路:

定义两个整形变量a和b,因为intThe value range of type is roughly ±2^32,should be larger than the given value range.Add directly,得到结果.However, the format of the result obtained at this time does not meet the requirements,To meet the requirements,The resulting integer needs to be converted to a string,然后对字符串进行操作,That is, it is added at the corresponding position of the resulting string“,”即可.

3、解题过程

This is the first one I madePATGrade A questions,A little uncomfortable.

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

在PAT上写Ccode appears warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]

原因:使用 scanfWhen there is a return value

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);
  ^~~~~~~~~~~~~~

PAT unique error,其他的 OJ 从来没有遇到过.

#include <stdio.h>

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

解决:Give it when writing code scanfA return value is sufficient

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

3.2 段错误

奇奇怪怪的错误

#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;
}

段错误

在这里插入图片描述

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

  1. 数组越界(大概率):
    1. Check to see if the array size is underwritten0(I have had it before…),如果题目要求1000,建议写1010
    2. Check the process of loop traversal,Whether there are individual subscripts out of bounds.
    3. sort函数里cmpComparison functions must have a return value,比如只有一个return,Don't write another one up frontifThe sentence is superfluous
    4. 如果数组比较大,一般来说>10000就算大,就请在maindeclare the array outside of
    5. 对于char 数组,If the title says no more than characters8,Please declare it10+
    6. Write it in the loop bodyprintfCheck out the intermediate results you want to see,You might know where I made a mistake
  2. 堆栈溢出:
    1. This usually happens in recursive programs,比如DFSWhen traversing a tree or graph,It can be written in recursionprintfCheck out the intermediate results you want to see,You might know where I made a mistake

当然,My problem with this code,在于 size()函数.

3.2.1 size()函数

原因是 size()The function returns an unsigned number,当 a 为空时,At this time the binary value size is not -1,Instead, it is treated as an unsigned integer,$ 0000 0000H(真值为0) + 1111 1111H = 2^{32} - 1$ .Errors are bound to happen.

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

3.3 %1000的问题

#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;
}

结果 15 分,I thought about why.

在这里插入图片描述

The idea of ​​passing white box testing,寻找答案

输入: 1 999
输出: 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;
}

这个也AC了

3.4 AC 代码

#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;
}
原网站

版权声明
本文为[A bigot is lovely]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101612006671.html