当前位置:网站首页>[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;
}
}
}边栏推荐
- Whole house WiFi solution: mesh router networking and ac+ap
- Laya中的A星寻路
- [common commands]
- Pyqt5 learning pit encounter and pit drainage (2) buttons in qformlayout layout cannot be displayed
- WebRTC实现简单音视频通话功能
- mpc5744p简介与OpenSDA固件更新
- Sequence list and linked list
- Pytorch GPU and CPU models load each other
- Not for 60 days, magical dictionary
- Locker 2022.1.1
猜你喜欢
随机推荐
What is the difference between field, variable and property
Differences and principles of bio, NiO and AIO
Introduction and examples of parameters in Jenkins parametric construction
On the use of pyscript (realizing office preview)
Pytoch distributed training
It won't last for 70 days. The k-largest number in the array
Not for 63 days. The biggest XOR
The daily life of programmers
Pyscript cannot import package
DASCTF2022.07赋能赛
Shell string segmentation
The third ACM program design competition of Wuhan University of Engineering
15.federation
Mongo shell interactive command window
VScode 一键编译和调试
redux快速上手
Pytorch fixed random seed & recurrence model
i++与++i详解
JVM (heap and stack) memory allocation
不会就坚持65天吧 只出现一次的数字









