当前位置:网站首页>7-1 positive integer a+b (15 points)
7-1 positive integer a+b (15 points)
2022-07-03 14:33:00 【Study hard 867】
The goal of the question is very simple , It's two positive integers A and B And , among A and B It's all in the range [1,1000]. A little bit of a hassle , The input is not guaranteed to be two positive integers .
Input format :
Type in on a line to give A and B, They are separated by spaces . The problem is A and B It doesn't have to be a positive integer , Sometimes it can be out of range numbers 、 negative 、 A real number with a decimal point 、 It's even a mess of code .
Be careful : Let's put... In the input 1 A space is taken as A and B Separation of . Make sure that there is at least one space for the question , also B It's not an empty string .
Output format :
If the input is really two positive integers , According to the format A + B = and Output . If an input is not satisfactory , Output at corresponding position ?, Obviously at this time and also ?.
Examples 1:"> sample input 1:
123 456
sample output 1:
123 + 456 = 579
sample input 2:
22. 18
sample output 2:
? + 18 = ?
sample input 3:
-100 blabla bla...33
sample output 3:
? + ? = ?Be sure to pay attention to the number beyond the range , Number out of range !
When doing this problem, because PTA In many cases above, although he gave the range of data , But the operation not in this range will not actually care , It leads me to think that all numbers , As a result, there is a test point that cannot be passed .
Ideas : You can't use shaping to store , So we use character type to store data , Because there must be a space to space a and b Two strings , So the first acceptance we use while((a[i]=getchar())!=' '); To accept the first data , Then our next step is to end after returning , Direct use gets() that will do , After accepting, we need to take out each character and let them form a number , If there is any magical character, we will order one flag, If his value changes , Then we'll print it out ?, At the same time, we have to consider many situations , For example, the first one is not Hello , The first is not to say hello , The second is to say hello , Neither of them is to say hello to these situations . The string we try to define is longer , In case someone really wants to vent on the keyboard ( funny ), Now we can write code according to this idea .
Code :
#include <stdio.h>
#include <string.h>
int main(){
char a[1000],b[1000];
int i=0,j,flag=0,flag2=0;
int number1=0,number2=0;
while(((a[i])=getchar())!=' ')i++;
gets(b);
for(j=0;j<i;j++){
if(a[j]<'0'||a[j]>'9')flag=1;
else number1=number1*10+a[j]-'0';
}
if(number1>1000||number1<1)flag=1;
if(flag==1)printf("? + ");
else printf("%d + ",number1);
for(i=0;i<strlen(b);i++){
if(b[i]<'0'||b[i]>'9')flag2=1;
else number2=number2*10+b[i]-'0';
}
if(number2>1000||number2<1)flag2=1;
if(flag2==1)printf("? = ?");
else if(flag==0&&flag2==0)printf("%d = %d",number2,number1+number2);
else if(flag2==0&&flag==1)printf("%d = ?",number2);
}边栏推荐
- 数学常数表 by q779
- 添加Zabbix计算类型项目Calculated items
- 如何查询淘宝天猫的宝贝类目
- PCB中常用快捷键
- Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)
- How to query the baby category of tmall on Taobao
- 【7.3】146. LRU caching mechanism
- Puzzle (016.4) domino effect
- Find specified characters
- Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
猜你喜欢

Happy capital new dual currency fund nearly 4billion yuan completed its first account closing

Protobuf and grpc

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

使用并行可微模拟加速策略学习

Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in

Use of constraintlayout

愉悦资本新双币基金近40亿元完成首次关账

US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars

Programming language: the essence of type system

Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
随机推荐
7-18 finding the single root of polynomial by dichotomy
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Adc128s022 ADC Verilog design and Implementation
Code writing and playing method of tonybot humanoid robot at fixed distance
中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
Jiuyi cloud black free encryption free version source code
别再问自己适不适合做软件测试了
tonybot 人形機器人 紅外遙控玩法 0630
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
[clean up the extraordinary image of Disk C]
Use of constraintlayout
PCB中常用快捷键
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
npm install卡住与node-npy的各种奇怪报错
556. 下一个更大元素 III : 简单构造模拟题
Puzzle (016.3) is inextricably linked
Thread. Sleep and timeunit SECONDS. The difference between sleep
常见问题之PHP——ldap_add(): Add: Undefined attribute type in