【九度OJ--1002】grading

题目描述:

    Grading hundreds of thousands of Graduate Entrance Exams is a hard work. It is even harder to design a process to make the results as fair as possible. One way is to assign each exam problem to 3 independent experts. If they do not agree to each other, a judge is invited to make the final decision. Now you are asked to write a program to help this process.
    For each problem, there is a full-mark P and a tolerance T(<P) given. The grading rules are:
    • A problem will first be assigned to 2 experts, to obtain G1 and G2. If the difference is within the tolerance, that is, if |G1 - G2| ≤ T, this problem's grade will be the average of G1 and G2.
    • If the difference exceeds T, the 3rd expert will give G3.
    • If G3 is within the tolerance with either G1 or G2, but NOT both, then this problem's grade will be the average of G3 and the closest grade.
    • If G3 is within the tolerance with both G1 and G2, then this problem's grade will be the maximum of the three grades.
    • If G3 is within the tolerance with neither G1 nor G2, a judge will give the final grade GJ.

输入:

    Each input file may contain more than one test case.
    Each case occupies a line containing six positive integers: P, T, G1, G2, G3, and GJ, as described in the problem. It is guaranteed that all the grades are valid, that is, in the interval [0, P].

输出:

    For each test case you should output the final grade of the problem in a line. The answer must be accurate to 1 decimal place.

样例输入:
20 2 15 13 10 18
样例输出:

14.0

题目解析:该题思路简单,只需要按照rule流程if-else即可。C代码如下:

#include<stdio.h>
#include<stdlib.h>
int main(){
	int p,t,g1,g2,g3,gj;
	while(scanf("%d%d%d%d%d%d", &p, &t, &g1, &g2, &g3, &gj) == 6){
		int temp1, temp2, temp3, max;
		temp1 = abs(g1 - g2);
		temp2 = abs(g1 - g3);
		temp3 = abs(g2 - g3);
		max = g1;
		if(max < g2) max = g2;
		if(max < g3) max = g3;
		if(temp1 <= t){
			printf("%.1f\n", (g1+g2)/2.0);
		}else if((temp2 <= t && temp3 >t) || (temp3 <= t && temp2 >t)){
			if(temp2 < temp3){
				printf("%.1f\n", (g1 + g3) / 2.0);
			}else{
				printf("%.1f\n", (g2 + g3) / 2.0);
			}
		} else if(temp2 <= t && temp3 <= t){
			printf("%.1f\n", (float)max);
		} else if(temp2 > t && temp3 > t){
			printf("%.1f\n", (float)gj);
		}
	}
	return 0;
} 

题外话:这道题在输出结果的时候碰到一个有意思的问题,那就是pirntf("%f",int型变量)的问题。

例如printf("%f",2),结果会是0.000000原因如下:printf();函数中的参数“%f”  “%d”等,只是用来告诉编译器对数据的读取以及处理方式,如果用%f 则编译器会从&2的起始地址开始读取 sizeof(float)个字节然后进行浮点型数据的处理,并不会对原数据做类型转换,所以printf("%f",2); 输出0.0000000 而 printf("%f",(float)2);输出就是2.000000。这涉及到数据在计算机中的存储方式,如果学过计算机组成原理,就比较好理解了。根据IEEE754标准,很容易理解,根据1-8-23的数符-阶码-尾数的二进制形式,比较容易理解,整形数值2的尾数部分会转化为0.000000...0010,%f截取6位后会变为0.000000.所以需要自己手动的显示的强制类型转换,即(float)2,才会得到想要的结果。

你可能感兴趣的:(c,printf)