SZU:G32 Mass fraction

Judge Info

  • Memory Limit: 32768KB
  • Case Time Limit: 5000MS
  • Time Limit: 5000MS
  • Judger: Float Numbers (1e-4) Judger

Description

The chemists are well known because of their weird. Especially when they add water or salt in the same beaker over and over again. Moreover, the still hope you can tell him the mass fraction of the liquor after many operations. In case of your forgetting your junior school chemistry class, now we particularly give you the formula of the mass fraction.

w=a/(a+b)×100% here w means the mass fraction, a means the mass of the salt, and b means the mass of water.

Input

The first line contains two integers, representing the mass of water and salt initially. Than each line will represent a operation. The operation contains:

  • 1. To add some salt into the beaker, the code is "salt x". x is a float number meaning the measure of salt The chemists add
  • 2. To add some water into the beaker, the code is "water x". x is a float number meaning the measure of water The chemists add
  • 3. Showing the mass fraction currently and the code is "show".
  • 4. Exit the test. The code is "exit".

The number will not be larger than 1e9.

Output

Output should be according to the operation. Print the mass fraction of the liquor. All the answer has an absolute error within 1e-4, will be consider as correct answer.

Sample Input

1.5 0.5
salt 0.5
water 1.5
show
exit

Sample Output

0.25

解题思路:printf("%g ", a/sum);  %g 指省略后面所有无效的0。 虽然这题很简单,但是每一道水题都要认真对待并且总结。

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 char A[10];
 5 
 6 int main()
 7 {
 8     float a,b,sum,c,d;
 9     scanf("%f%f",&b,&a);
10     while(1){
11         memset(A,'',sizeof(A));
12         scanf("%s", A);
13         if(strcmp(A,"salt")==0){
14             scanf("%f",&c);
15             a+=c;
16             continue;
17         }
18         if(strcmp(A,"water")==0){
19             scanf("%f",&d);
20             b+=d;
21             continue;
22         }
23         if(strcmp(A,"show")==0){
24             sum=a+b;
25             printf("%g
", a/sum);
26             continue;
27         }
28         if(strcmp(A,"exit")==0){
29             break;
30         }
31 
32     }
33     return 0;
34 }
原文地址:https://www.cnblogs.com/firstrate/p/3199730.html