首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一路浙大机试题,求大神指导,想了一晚上了。

2013-07-08 
一道浙大机试题,求大神指导,想了一晚上了。。。。我是新手,自学了c最近,就去九度上找了一道浙大的题练习。这道

一道浙大机试题,求大神指导,想了一晚上了。。。。
我是新手,自学了c最近,就去九度上找了一道浙大的题练习。这道题本身并不难,但我感觉有个问题让我想了一个晚上也没搞懂。
首先题目如下(好吧英语,但基本一看就明白了,就是简单的进行if选择):
题目描述: 
    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
————————————————————————————————————————————————
我最终AC的程序如下:
#include <stdio.h>
#include <math.h>
void main()
{
        int P,T,G1,G2,G3,GJ;
        double max(int i,int j,int k);
     while ((scanf("%d%d%d%d%d%d",&P,&T,&G1,&G2,&G3,&GJ))!=EOF&&(T<=P&&T>=0)&&(G1<=P&&G1>=0)&&(G2<=P&&G2>=0)&&(G3<=P&&G3>=0)&&(GJ<=P&&GJ>=0))
     {


                         double a;
                 if(abs(G1-G2)<=T)
                         a=(G1+G2)/2.0;
                  else  if((abs(G3-G1)<=T)||(abs(G2-G3)<=T))
                 {
                         if(abs(G3-G1)<=T&&abs(G3-G2)>T)
                                 a=(G1+G3)/2.0;
                         else if(abs(G3-G1)>T&&abs(G3-G2)<=T)
                                 a=(G2+G3)/2.0;
                         else if(abs(G3-G1)<=T&&abs(G3-G2)<=T)  
                                 a=max(G1,G2,G3);                           
                 }
                 else
                         a=(double)GJ;
                 printf("%.1f\n",a);
        
         } 
}
double max(int i,int j,int k)
{
         double temp;
         if(i>j)
                 temp=i;
         else
                 temp=j;
         if(temp>k)
                 return temp;
         else
                 return k;
————————————————————————————————————————————————
重点看红色标注的那段,如果我把那段改为(就是全改为if,把else去掉):
else  if((abs(G3-G1)<=T)||(abs(G2-G3)<=T))
                 {


                         if(abs(G3-G1)<=T&&abs(G3-G2)>T)
                                 a=(G1+G3)/2.0;
                         if(abs(G3-G1)>T&&abs(G3-G2)<=T)
                                 a=(G2+G3)/2.0;
                         if(abs(G3-G1)<=T&&abs(G3-G2)<=T)  
                                 a=max(G1,G2,G3);                           
                 }

——这样再提交就错了。。——————————————————————————————————————————————
我试了试,在这种情况下,只会针对if(abs(G3-G1)<=T&&abs(G3-G2)>T)这种情况会出现问题。
对于 if(abs(G3-G1)>T&&abs(G3-G2)<=T)和 if(abs(G3-G1)<=T&&abs(G3-G1)<=T)这两种情况仍可以正确输出。
比如 输入:
20 2 18 12 16 15(第一种情况)
结果会输出:
18.0   (答案该是17.0,但这里是18.0)
经多次换值尝试,发现这里会错误输出G1的值。
比如 输入:
20 4 20 10 17 14(第一种情况)
输出:
20.0  (答案该是18.5,但这里是20.0即G1)
我的问题:
1.我想来想去这两个程序除了在执行效率方面,在处理结果上感觉是等效的啊,为什么后面的那个却是错的,希望能详细解答。
2.为什么会错误输出G1的值,我感觉很诡异啊。。。我想就算是程序错了导致条件判断出现问题,最后a的值怎么会诡异的出现等于G1。。。。
[解决办法]
话说我用你那段所谓错误的代码AC了。。是你改的时候哪里搞错了吧。。


#include <stdio.h>
#include <math.h>
void main()
{
        int P,T,G1,G2,G3,GJ;
        double max(int i,int j,int k);
     while ((scanf("%d%d%d%d%d%d",&P,&T,&G1,&G2,&G3,&GJ))!=EOF&&(T<=P&&T>=0)&&(G1<=P&&G1>=0)&&(G2<=P&&G2>=0)&&(G3<=P&&G3>=0)&&(GJ<=P&&GJ>=0))
     {
                         double a;
                 if(abs(G1-G2)<=T)
                         a=(G1+G2)/2.0;
 /*                 else  if((abs(G3-G1)<=T)


[解决办法]
(abs(G2-G3)<=T))
                 {
                         if(abs(G3-G1)<=T&&abs(G3-G2)>T)
                                 a=(G1+G3)/2.0;
                         else if(abs(G3-G1)>T&&abs(G3-G2)<=T)
                                 a=(G2+G3)/2.0;
                         else if(abs(G3-G1)<=T&&abs(G3-G2)<=T)  
                                 a=max(G1,G2,G3);                           
                 }*/
 else  if((abs(G3-G1)<=T)
[解决办法]
(abs(G2-G3)<=T))
                 {
                         if(abs(G3-G1)<=T&&abs(G3-G2)>T)
                                 a=(G1+G3)/2.0;
                         if(abs(G3-G1)>T&&abs(G3-G2)<=T)
                                 a=(G2+G3)/2.0;
                         if(abs(G3-G1)<=T&&abs(G3-G2)<=T)  
                                 a=max(G1,G2,G3);                           
                 }
                 else
                         a=(double)GJ;
                 printf("%.1f\n",a);
        


         } 
}
double max(int i,int j,int k)
{
         double temp;
         if(i>j)
                 temp=i;
         else
                 temp=j;
         if(temp>k)
                 return temp;
         else
                 return k;
}


热点排行
Bad Request.