【C语言】【面试题】5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果问题

时间:2021-10-17 14:14:20
#include <stdio.h>  
int main()  
{  
int A,B,C,D,E;
    for( A=1; A<=5; A++)  
       { 
       for( B=1; B<=5; B++)  
           { 
           for( C=1; C<=5; C++)  
               {
                for( D=1; D<=5; D++)  
                    {
                    for( E=1; E<=5; E++)  
                        {
                        if( (B==1) + (A==3) ==1 &&  
                                    (B==2) + (E==4) ==1 &&  
                                    (C==1) + (D==2) ==1 &&  
                                    (C==5) + (D==3) ==1 &&  
                                    (E==4) + (A==1) ==1 )  //给出的条件
                        {     
                           int val=0;
                           int flag=1;
                           val|=(1<<(a-1));//根据排名,每次右移排名-1位,然后与0或,使其为1;列入排名位1,2,3,4,5那么val为0000....11111,如果为1,2,2,3,4,那么val为0000....1111,如果为1,3,3,4,5,那么val为0000....11101;那么第二位就为0,所以不连续。
                           val|=(1<<(b-1));
                           val|=(1<<(c-1));
                           val|=(1<<(d-1));
                           val|=(1<<(e-1));
                           while(val)//判断排名是否连续,不连续说明不满足条件不输出
                           {
                               if(val%2==0)
                               {
                                   falg=0;
                               }
                               val/=2;
                           }
                           if(flag==1)
                           {
                               printf("a=%d b=%d c=%d d=%d e=%d\n", a, b, c, d, e");
                           }
                        }    
                        }
                    }
               }
           }
       }
    return -1;  
}