当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
How to use bitwise operators in C language
一张图快速了解 Istio 的 EnvoyFilter
Return the next higher prime number
bp神经网络反向传播原理,BP神经网络反向传播
国内油价四连跌,但下跌趋势可能终止
DGIOT平台实时展示OPC上报数据全流程代码剖析
需求骤降,成本激增,PC行业再次入冬
Embedded Development: Embedded Basics - Mapping Peripherals Using Arrays of Pointers
#夏日挑战赛#【ELT.ZIP】啃论文俱乐部——学术科研方法论沉淀辑
深度学习培训二笔记
随机推荐
取Json中的数组进行遍历
LeetCode-1. Two Sum
教你自己搭建一个IP池(绝对超好用!!!!)
Trie字典树
JNDI and RMI, LDAP
招聘分析2020.6.1
64位 RT-Thread 移植到 Cortex-A53 系统 bug 修复笔记
aliexpress API 接入说明
1001 A+B Format(字符串处理)
unr #6day1 T2题解
fastjson chain analysis (1.2.22-47)
skywalking漏洞学习
vvic API 接入说明
Return the next higher prime number
perl编码转换
LeetCode-2. Add Two Numbers
重庆新壹汽与一汽集团达成新能源项目战略合作,赋能“碳中和”创造“碳财富”
初始网络原理
电力系统潮流计算(牛顿-拉夫逊法、高斯-赛德尔法、快速解耦法)【6节点 9节点 14节点 26节点 30节点 57节点】(Matlab代码实现)
nacos服务注册