大象怒怒的小脚


没有终了漂泊,于是成了树上的精灵.有梦想,却不会飞翔.用自己短短的羽翼,跳跃在迷离的枝头……

首页 新随笔 联系 订阅 管理
这几天一直看到这样的贴子 讨论怎么称出那只不一样的球。
其实这不怎么难的,只要静下心来好好想一想, 理清自己的思路的话
肯定能想到的
下面是我的分析过程
L: Left    R:Right
①Weight (0+1+2+3,4+5+6+7)
|---L=R  -->[8,9,10,11]
|---②Weight (8,9)
|    |---L=R  -->[10,11]
|    |---③Weight (8,10)
|    |    |---L=R  -->[11]
|    |    |---L!=R -->[10]
|    |
|    |---L!=R -->[8,9]
|    |---③Weight (8,10)
|         |---L=R  -->[9]
|         |---L!=R -->[8]
|    
|---L>R  -->[0,1,2,3,4,5,6,7]
|---②Weight (0+4+11, 1+2+5)
|    |---L=R  -->[3,6,7]
|    |---③Weight (6,7)
|    |    |---L=R  -->[3]
|    |    |---L>R  -->[7]
|    |    |---L<R  -->[6]
|    |
|    |---L>R  -->[0,5]
|    |---③Weight (0,11)
|    |    |---L=R  -->[5]
|    |    |---L!=R -->[0]
|    |
|    |---L<R  -->[1,2,4]
|    |---③Weight (1,2)
|    |    |---L=R  -->[4]
|    |    |---L>R  -->[1]
|    |    |---L<R  -->[2]
|
|---L<R  -->[0,1,2,3,4,5,6,7]
|---②Weight (0+4+11, 1+2+5)
    |---L=R  -->[3,6,7]
    |---③Weight (6,7)
    |    |---L=R  -->[3]
    |    |---L>R  -->[6]
    |    |---L<R  -->[7]
    |
    |---L>R  -->[1,2,4]
    |---③Weight (1,2)
    |    |---L=R  -->[4]
    |    |---L>R  -->[2]
    |    |---L<R  -->[1]
    |
    |---L<R  -->[0,5]
    |---③Weight (0,11)
    |    |---L=R  -->[5]
    |    |---L!=R -->[0]
这几天也闲得有那么受不了了,于是就用eclipse 配了C/C++的开发环境,简单测试了一下自己称球的程序.
数据要求:全是int范围,12个,有且只有一个是不一样的分量。
输出结果:那只异样的球所在的下标

#include<iostream>
#include 
<fstream>
using namespace std;
int Weight(int ,int );
int main(int argc,char*[])
{
    
int aBall[12];
    
int Balance;
    ifstream File;
    File.open(
"Data.txt");
    
for (int i =0 ;i<12;i++){
        File
>>aBall[i];
    }
   
//File.close;
    Balance = Weight(aBall[0+ aBall[1+ aBall[2+ aBall[3],aBall[4+ aBall[5+ aBall[6+ aBall[7]) ;
    
switch (Balance){
        
case 0:    {//8 9 10 11
            Balance = Weight(aBall[8],aBall[9]) ;
            
switch (Balance) {
                
case 0:{                        
                    Balance 
= Weight(aBall[8],aBall[10]) ;
                    
switch (Balance) {
                        
case 0:    {
                            cout
<< 11<<endl;
                            
break;
                        }
                        
case 1:
                        
case 2:{
                            cout
<< 10<<endl;
                            
break;
                        }
                    }
                    
break;
                }
                
case 1:
                
case 2:{
                    Balance 
= Weight(aBall[8],aBall[10]) ;
                    
switch (Balance) {
                        
case 0:{
                            cout
<< 9<<endl;    
                            
break;
                        }
                        
case 1:
                        
case 2:{
                            cout
<< 8<<endl;
                            
break;
                        }
                    }
                    
break;
                }
            }
            
break;
        }
        
case 1:{//0 1 2 3 4 5 6 7
            Balance = Weight(aBall[0]+aBall[4]+aBall[11],aBall[1]+aBall[2]+aBall[5]) ;
            
switch (Balance) {
                
case 0: {//3 6 7    
                    Balance = Weight(aBall[6],aBall[7]);
                    
switch (Balance) {
                        
case 0:{
                            cout
<< 3<<endl;
                            
break;
                        }
                        
case 1:{
                            cout
<< 7<<endl;
                            
break;
                        }
                        
case 2:{
                            cout
<< 6<<endl;
                            
break;
                        }
                    }
                    
break;
                }
                
case 1:{//0 5
                    Balance = Weight(aBall[0],aBall[11]) ;
                    
switch (Balance) {
                        
case 0:{
                            cout
<< 5<<endl;
                            
break;
                        }
                        
case 1:
                        
case 2:{
                            cout
<< 0<<endl;
                            
break;
                        }
                    }
                    
break;
                }
                
case 2:{//1 2 4
                    Balance = Weight(aBall[1],aBall[2]) ;
                    
switch (Balance) {
                        
case 0:{
                            cout
<< 4<<endl;
                            
break;
                        }
                        
case 1:{
                            cout
<< 1<<endl;
                            
break;
                        }
                        
case 2:{
                            cout
<< 2<<endl;
                            
break;
                        }
                    }
                    
break;
                }
            }
            
break;
        }
        
case 2:    {
            Balance 
= Weight(aBall[0]+aBall[4]+aBall[11],aBall[1]+aBall[2]+aBall[5]) ;
            
switch (Balance) {
                
case 0:{//3 6 7
                    Balance = Weight(aBall[6],aBall[7]) ;
                    
switch (Balance) {
                        
case 0:{
                            cout
<< 3<<endl;
                            
break;
                        }
                        
case 1:{
                            cout
<< 6<<endl;
                            
break;
                        }
                        
case 2:{
                            cout
<< 7<<endl;
                            
break;
                        }
                    }
                    
break;
                }
                
case 2:{//0 5
                    Balance = Weight(aBall[0],aBall[11]) ;
                    
switch (Balance) {
                        
case 0:{
                            cout
<< 5<<endl;
                            
break;
                        }
                        
case 1:
                        
case 2:{
                            cout
<< 0<<endl;
                            
break;
                        }
                    }
                    
break;
                }
                
case 1:{//1 2 4
                    Balance = Weight(aBall[1],aBall[2]) ;
                    
switch (Balance) {
                        
case 0:{
                            cout
<< 4<<endl;
                            
break;
                        }
                        
case 1:{
                            cout
<< 2<<endl;
                            
break;
                        }
                        
case 2:{
                            cout
<< 1<<endl;
                            
break;
                        }
                    }
                    
break;
                }
            }
            
break;
        }
    }
    
return 0;
}
//main
int Weight(int iLeft,int iRight) {
    
int iRet;
    
if (iLeft == iRight)
        iRet 
= 0;
    
if (iLeft > iRight)
        iRet 
= 1;
    
if (iLeft < iRight)
        iRet 
= 2;
    
return iRet;

}


posted on 2006-10-31 12:26  大象怒怒  阅读(965)  评论(0编辑  收藏  举报