当前位置:网站首页>[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] exit
The 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 2
No blank lines at the end
sample output 1:
[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
price = 4.10
price = 0.00
No blank lines at the end
sample input 2:
1 2 3 3 4 4 5 6 7 8
sample 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.20
Their 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;
}
}
}
边栏推荐
- Pyscript cannot import package
- Dabao and Erbao
- Shell string segmentation
- Whether the modification of import and export values by ES6 and commonjs affects the original module
- 9. Delay queue
- String, array, generalized table (detailed)
- Mongo Shell交互式命令窗口
- Simply change the picture color
- Pytoch distributed training
- Pyqt5 learning pit encounter and pit drainage (1) unable to open designer.exe
猜你喜欢
Not for 63 days. The biggest XOR
6. Pytest generates an allure Report
post导出数据,返回
No, just stick to it for 64 days. Find the insertion location
Realize the effect of univariate quadratic equation through JS. Enter the coefficients of a, B and C to calculate the values of X1 and x2
C语言力扣第61题之旋转链表。双端队列与构造循环链表
Vscode one click compilation and debugging
不会就坚持59天吧 替换单词
Not for 58 days. Implement prefix tree
Idea small settings
随机推荐
Sequence list and linked list
恒星科通邀您“湘”约第24届中国高速公路信息化大会暨技术产品展示会
Use of torch.optim optimizer in pytorch
Make a virtual human with zego avatar | virtual anchor live broadcast solution
不会就坚持70天吧 数组中第k大的数
Can you really write restful API?
你真的会写Restful API吗?
Pyscript cannot import package
Vscode one click compilation and debugging
不会就坚持65天吧 只出现一次的数字
On the use of pyscript (realizing office preview)
不会就坚持62天吧 单词之和
VScode 一键编译和调试
The third ACM program design competition of Wuhan University of Engineering
The principle of inverse Fourier transform (IFFT) in signal processing
The pit I walked through: the first ad Sketchpad
Multi card training in pytorch
settings.xml
Flutter实战-请求封装(二)之dio
Realize the effect of univariate quadratic equation through JS. Enter the coefficients of a, B and C to calculate the values of X1 and x2