当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
神经网络有哪些激活函数,卷积神经网络有哪些
自适应模糊神经网络与bp神经网络的区别
Meaning of CDF graph
DASCTF2022.07 empowerment competition WEB topic recurrence
注解和反射、持续
还在用 Xshell?你 out 了,推荐一个更现代的终端连接工具,好用到爆!
中国芯片的营收首破万亿,优势凸显的成熟工艺产能将称霸全球
电力系统潮流计算与PowerWorld仿真(牛顿拉夫逊法和高斯赛德尔法)(Matlab实现)
The DGIOT platform displays the whole process code analysis of OPC reporting data in real time
v-model指令:获取和设置表单元素的值
随机推荐
生成树协议(STP---Spanning Tree Protocol)
C语言按位运算符如何使用
重庆新壹汽与一汽集团达成新能源项目战略合作,赋能“碳中和”创造“碳财富”
李斌带不动的长安新能源高端梦,华为和“宁王”能救吗?
软件工程基础知识--需求分析
MySQL增加字段SQL语句
Oracle Install [email protected] 7.6
浅析端口扫描原理
建筑施工员证怎么考?报名条件及报考时间是什么?
在Istio中,到底怎么获取 Envoy 访问日志?
聚焦企业流程智能化发展新趋势,中国信通院2022 RPA创新产业峰会即将开启
JWT 实现登录认证 + Token 自动续期方案
BalsnCTF2021
LeetCode-1. Two Sum
v-model指令:获取和设置表单元素的值
浅谈泰山众筹系统开发技术说明及dapp链上众筹系统开发分析
#夏日挑战赛#【ELT.ZIP】啃论文俱乐部——学术科研方法论沉淀辑
mysql主主复制+keepalived高可用
【QT VS项目名称修改】
如何构建一个自己的代理ip池