当前位置:网站首页>[c language] PTA 7-55 query fruit price
[c language] PTA 7-55 query fruit price
2022-07-29 04:29:00 【LastWhisperw】
Given four fruits , They are apples (apple)、 pear (pear)、 orange (orange)、 grapes (grape), The unit price corresponds to 3.00 element / kg 、2.50 element / kg 、4.10 element / kg 、10.20 element / kg .
First, display the following menu on the screen :
[1] apple
[2] pear
[3] orange
[4] grape
[0] exitThe user can enter the number 1~4 Query the unit price of the corresponding fruit . When the number of consecutive queries exceeds 5 When the time , The program should exit the query automatically ; Less than 5 And then the user enters 0 Quit immediately ; Enter another number , Show price as 0.
Input format :
Enter a number of numbers in a row that the user has entered in succession .
Output format :
First, show the menu on the screen . Then corresponding to each input of the user , In one line by format “price = Price ” Output query results , Two decimal places are reserved for the price . When the number of continuous queries exceeds 5 Time 、 Or active input 0 when , Program end .
sample input 1:
3 -1 0 2No blank lines at the end
sample output 1:
[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
price = 4.10
price = 0.00No blank lines at the end
sample input 2:
1 2 3 3 4 4 5 6 7 8sample output 2:
[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
price = 3.00
price = 2.50
price = 4.10
price = 4.10
price = 10.20Their thinking : Use an array to store the input number , After saving, read and output the corresponding price in sequence .
I originally wanted to store the corresponding price with another data , But considering that there will be other serial numbers in the input , If the sequence number entered is larger than the size of the array or negative, it may cause an error , And it is known that the entered serial numbers are all integers , Final replacement switch case sentence .
The code is as follows :
#include<stdio.h>
int main(){
//float a[5]={0,3.0,2.5,4.1,10.2};
int s[5]={0,};
int i=0;
int n;
while(i<5){
scanf("%d",&n);
if(n==0){
break;
}
s[i]=n;
i++;
}
printf("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit");
int N=i;
for(i=0;i<N;i++){
printf("\nprice = ");
switch (s[i]){
case 1:
printf("%.2f",3.0);
break;
case 2:
printf("%.2f",2.5);
break;
case 3:
printf("%.2f",4.1);
break;
case 4:
printf("%.2f",10.2);
break;
default:
printf("%.2f",0.0);
break;
}
}
}边栏推荐
- Jenkins 参数化构建中 各参数介绍与示例
- Not for 60 days, magical dictionary
- Can you really write restful API?
- Interview notes of a company
- Exception resolution: error of not finding edu.stanford.nlp.semgraph.semgrex.semgrexpattern in cococaption package
- Deep learning training strategy -- warming up the learning rate
- On the use of pyscript (realizing office preview)
- 使用容器部署Jenkins
- Back propagation process of manual BP neural network
- Don't insist on 66 days. Weight generates random numbers
猜你喜欢

Not for 61 days. The shortest word code

Deep learning training strategy -- warming up the learning rate

6. Pytest generates an allure Report

The principle of inverse Fourier transform (IFFT) in signal processing

不会就坚持67天吧 平方根

Record the Niua packaging deployment project

Why is it necessary to scale the attention before softmax (why divide by the square root of d_k)

恒星科通邀您“湘”约第24届中国高速公路信息化大会暨技术产品展示会

Visio draw grid

9. Delay queue
随机推荐
Pyqt5 learning pit encounter and pit drainage (2) buttons in qformlayout layout cannot be displayed
Definition and implementation of stack and queue (detailed)
Use of construction methods
Oracle 插入数据
不会就坚持63天吧 最大的异或
Incubator course design (April 12, 2021)
10.回退消息
Dasctf2022.07 empowerment competition
Don't insist on 66 days. Weight generates random numbers
15.federation
不会就坚持69天吧 合并区间
Laya中的A星寻路
MySQL - 聚簇索引和辅助索引
Leftmost prefix principle of index
Not 67 days, square root
不会就坚持59天吧 替换单词
kotlin的List,Map,Set等集合类不指定类型
不会就坚持66天吧 权重生成随机数
C语言:typedef知识点总结
12.优先级队列和惰性队列