当前位置:网站首页>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);
}边栏推荐
- 556. The next larger element III
- 添加Zabbix计算类型项目Calculated items
- Doris学习笔记之数据表的创建
- Puzzle (016.4) domino effect
- How to query the baby category of tmall on Taobao
- Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
- Tonybot humanoid robot checks the port and corresponds to port 0701
- MySQL multi table query subquery
- 天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
- retrofit
猜你喜欢
随机推荐
NPM install is stuck with various strange errors of node NPY
洛谷P5194 [USACO05DEC]Scales S 题解
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
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
2021年区域赛ICPC沈阳站J-Luggage Lock(代码简洁)
Detailed explanation of four modes of distributed transaction (Seata)
retrofit
Exercise 6-6 use a function to output an integer in reverse order
洛谷P3065 [USACO12DEC]First! G 题解
Ultra simple mobile map development
中国锂电池电解液行业市场专项调研报告(2022版)
Eight sorts
别再问自己适不适合做软件测试了
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Table of mathematical constants by q779
7-16 find the set of integers that meet the given conditions
Facebook 如何将 Instagram 从 AWS 搬到自己的服务器
Recent learning summary
Use of constraintlayout
Code writing and playing method of tonybot humanoid robot at fixed distance









