当前位置:网站首页>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);
}边栏推荐
- Detailed explanation of four modes of distributed transaction (Seata)
- Exercise 8-8 moving letters
- Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
- 7-17 crawling worms (break exercise)
- tonybot 人形機器人 紅外遙控玩法 0630
- 必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿
- Add ZABBIX calculation type itemcalculated items
- Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
- 洛谷P5194 [USACO05DEC]Scales S 题解
- 556. The next larger element III
猜你喜欢

puzzle(016.3)千丝万缕

tonybot 人形机器人 查看端口并对应端口 0701

Use of constraintlayout

Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块

Mysql多表查询 #子查询

Code writing and playing method of tonybot humanoid robot at fixed distance
![洛谷P4047 [JSOI2010]部落划分 题解](/img/7f/3fab3e94abef3da1f5652db35361df.png)
洛谷P4047 [JSOI2010]部落划分 题解

Understand the application scenario and implementation mechanism of differential segment

Programming language: the essence of type system

必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿
随机推荐
Luogu p4047 [jsoi2010] tribal division solution
7-15 calculation of PI
puzzle(016.4)多米诺效应
Tonybot humanoid robot checks the port and corresponds to port 0701
超简单手机地图开发
tonybot 人形机器人 查看端口并对应端口 0701
tonybot 人形機器人 紅外遙控玩法 0630
Tonybot humanoid robot starts for the first time 0630
Too many files with unapproved license
adc128s022 ADC verilog设计实现
剑指 Offer 28. 对称的二叉树
Eight sorts
Sword finger offer 28 Symmetric binary tree
Luogu p5194 [usaco05dec]scales s solution
FPGA blocking assignment and non blocking assignment
Find specified characters
Facebook 如何将 Instagram 从 AWS 搬到自己的服务器
Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值
Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
NPM install is stuck with various strange errors of node NPY