当前位置:网站首页>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]
原因:使用 scanf
When 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 scanf
A 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:
- 数组越界(大概率):
- Check to see if the array size is underwritten0(I have had it before…),如果题目要求1000,建议写1010
- Check the process of loop traversal,Whether there are individual subscripts out of bounds.
- sort函数里cmpComparison functions must have a return value,比如只有一个return,Don't write another one up frontifThe sentence is superfluous
- 如果数组比较大,一般来说>10000就算大,就请在maindeclare the array outside of
- 对于char 数组,If the title says no more than characters8,Please declare it10+
- Write it in the loop bodyprintfCheck out the intermediate results you want to see,You might know where I made a mistake
- 堆栈溢出:
- 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;
}
边栏推荐
猜你喜欢
随机推荐
mysql主主复制+keepalived高可用
mysql包select结果无法同步的问题
【接入指南 之 直接接入】手把手教你快速上手接入HONOR Connect平台(上)
Embedded Development: Embedded Basics - Mapping Peripherals Using Arrays of Pointers
软件工程基础知识--需求分析
fastjson chain analysis (1.2.22-47)
全新接口——邻家好货 API
聚焦企业流程智能化发展新趋势,中国信通院2022 RPA创新产业峰会即将开启
leetcode:1013. 将数组分成和相等的三个部分
华为-求int型正整数在内存中存储时1的个数
sprintboot验证码kaptcha 自定义图片样式
网易云信亮相LiveVideoStackCon2022,解构基于WebRTC的开源低延时播放器实践
神经网络的图像识别技术,神经网络识别图像原理
建筑施工员证怎么考?报名条件及报考时间是什么?
leetcode:337. 打家劫舍 III
百日刷题挑战--错题01day
Pytorch GPU模型推理时间探讨
skywalking漏洞学习
训练一个神经网络要多久,神经网络训练时间过长
浅谈泰山众筹系统开发技术说明及dapp链上众筹系统开发分析