假币问题

 

一、概要设计

  1、用数组来存储钱币1表示真币0表示比真币轻2表示比假币中。

  2、用随机函数rand()来随机生成假币的在数组中的位置以及假币的值

  3、通过简单的比较找出一个真币,将值赋在数组的第一个里面,方便以后的比较。

  4、程序用2分法和3分法两种来查找假币的位置,并判断假币的轻重。

  5、变量index跟踪存储假币的位置。

 
  1. //二分法求解、、、、
  2.  //---------------------------------------------------------------------------
  3. #include "Head.h" 
  4. #define     LEFT_HEAVY      -1 
  5. #define     RIGHT_HEAVY     1 
  6. #define     HEAVY       2 
  7. #define     LIGHT       0 
  8. int     which_heavy = 0 ;//那一边比较重,初始化为0 ,即未确定
  9. int     degree  = 1 ;//假币轻重标志,初始化为1 ,即未确定
  10. int     Sum_Coin(const int A[] ,int from ,int to )
  11. {
  12.     int     sum = 0 ;
  13.     for(int i=from ;i<=to ;i++)
  14.     {   
  15.         sum +=  A[i] ;
  16.     }
  17.     return  sum ;
  18. }
  19.  //---------------------------------------------------------------------------
  20. void    Check_Degree(int A[] ,int& false_sign ,int from ,int to )
  21. {//在确定真币A[0]的前提下,剩下2个硬币--检测假币的所在位置和轻重
  22.     
  23.     if(A[0]==A[from])
  24.     {
  25.         false_sign = to ;
  26.         if( degree==1)
  27.         {//判断假币的轻重
  28.             if( A[0]<A[to] )
  29.                 degree = HEAVY ;
  30.             else
  31.                 degree = LIGHT ;
  32.         }
  33.     }
  34.     else
  35.     {
  36.         false_sign = from ;
  37.         if( degree==1)
  38.         {//判断假币的轻重
  39.             if( A[0]<A[from] )
  40.                 degree = HEAVY ;
  41.             else
  42.                 degree = LIGHT ;
  43.         }
  44.     }
  45. }
  46.  //---------------------------------------------------------------------------
  47. void    Check_Coin_3(int A[] ,int& false_sign ,int from ,int to )
  48. {/*假币问题求解:三分法*/
  49.     if( (to-from+1) < 3)
  50.     {确定真币的重量--币种        
  51.         if( from>1 )
  52.             A[0] = A[1] ;
  53.         else
  54.             A[0] = A[to+1] ;
  55.         Check_Degree(A ,false_sign ,from ,to ) ;    
  56.     }
  57.     else
  58.     {
  59.         int     i = (to-from+1)/3 ;
  60.         int     mid1 = from+i-1 ;
  61.         int     mid2 = mid1+i ;
  62.         //cout << "/n" << from << " " <<mid1 << " " <<mid2 <<endl  ;
  63.         if( Sum_Coin(A ,from ,mid1)==Sum_Coin(A ,mid1+1 ,mid2) ) 
  64.         {
  65.             if( degree==1)
  66.             {//判断假币的轻重
  67.                 if(which_heavy==LEFT_HEAVY)
  68.                     degree = LIGHT ;
  69.                 if(which_heavy==RIGHT_HEAVY)
  70.                     degree = HEAVY ;
  71.             }
  72.             Check_Coin_3(A ,false_sign ,mid2+1 ,to) ;
  73.         }
  74.         else
  75.         {
  76.             if( degree==1)
  77.             {//判断假币的轻重
  78.                 if( Sum_Coin(A ,from ,mid1) > Sum_Coin(A ,mid1+1 ,mid2) ) 
  79.                     which_heavy = LEFT_HEAVY ;
  80.                 else
  81.                     which_heavy = RIGHT_HEAVY ;
  82.             }
  83.             Check_Coin_3(A ,false_sign ,from ,mid2) ;
  84.         }
  85.     }
  86. }
  87.  //---------------------------------------------------------------------------
  88. void    Check_Coin_2(int A[] ,int& false_sign ,int from ,int to )
  89. {/*/*假币问题求解:二分法*/*/
  90.     if( (to-from+1) < 4)
  91.     {
  92.         if( (to-from+1) ==3)
  93.         {//3
  94.             if(A[from]==A[to])
  95.             {//from+1
  96.                 false_sign = from+1 ;
  97.                 if( A[0]<A[from+1] )
  98.                     degree = HEAVY ;
  99.                 else
  100.                     degree = LIGHT ;
  101.             }
  102.             else
  103.             {//from ,to //确定真币的重量--币种               
  104.                 A[0] = A[from+1] ;
  105.                 Check_Degree(A ,false_sign ,from ,to ) ;            
  106.             }
  107.         }
  108.         else
  109.         {//2//确定真币的重量--币种
  110.             if( from>1 )
  111.                 A[0] = A[1] ;
  112.             else
  113.                 A[0] = A[to+1] ;
  114.             Check_Degree(A ,false_sign ,from ,to ) ;
  115.         }
  116.     }
  117.     else
  118.     {
  119.         int     i = (to-from+1)/4 ;
  120.         int     mid1 = from+i-1 ;
  121.         int     mid2 = mid1+i ;
  122.         if( Sum_Coin(A ,from ,mid1)==Sum_Coin(A ,mid1+1 ,mid2) ) 
  123.         {
  124.             if( degree==1)
  125.             {//判断假币的轻重
  126.                 if(which_heavy==LEFT_HEAVY)
  127.                     degree = LIGHT ;
  128.                 if(which_heavy==RIGHT_HEAVY)
  129.                     degree = HEAVY ;
  130.             }
  131.             Check_Coin_3(A ,false_sign ,mid2+1 ,to) ;
  132.         }
  133.         else
  134.         {
  135.             if( degree==1)
  136.             {//判断假币的轻重
  137.                 if(which_heavy==LEFT_HEAVY)
  138.                     degree = HEAVY ;
  139.                 else 
  140.                 {
  141.                     if(which_heavy==RIGHT_HEAVY)
  142.                         degree = LIGHT ;
  143.                     else
  144.                     {
  145.                         if( Sum_Coin(A ,from ,mid1) > Sum_Coin(A ,mid1+1 ,mid2) )                       
  146.                             which_heavy = LEFT_HEAVY ;                      
  147.                         else                    
  148.                             which_heavy = RIGHT_HEAVY ;
  149.                     }
  150.                 }
  151.             }
  152.             Check_Coin_3(A ,false_sign ,from ,mid2) ;
  153.         }
  154.     }
  155. }
  156.  //---------------------------------------------------------------------------
  157. void    Print_Coin(const int coin[] ,int coin_num)
  158. {   
  159.     for(int i=1 ;i<=coin_num ;++i)
  160.         cout << coin[i] << "    "  ;
  161. }
  162. void    main()
  163. {
  164.     int*    coin ;
  165.     int     coin_num ;
  166.     int     false_coin ; //1 ,假币较重; 0 ,假币较轻
  167.     time_t t;
  168.     srand((unsigned) time(&t));
  169.     char c ;
  170.     do
  171.     {       
  172.         system("cls") ;     
  173.         cout << "/n/n   请输入货币总数: " ;
  174.         cin >> coin_num ;
  175.         coin = new int[coin_num+1] ;
  176.         for(int i=1 ;i<=coin_num ;++i)
  177.         {
  178.             coin[i] = 1 ;
  179.         }
  180.         false_coin = rand()%2 ;
  181.         int     false_position = (rand()%coin_num)+1 ;
  182.         if(false_coin==0)
  183.             coin[false_position] = 0 ;
  184.         else
  185.             coin[false_position] = 2 ;
  186.         Print_Coin(coin ,coin_num) ;
  187.         int     false_sign = -1 ; //假币位置
  188.         degree  = 1 ;//轻重标志,初始化为1 ,即未确定
  189.         which_heavy = 0 ;//那一边比较重,初始化为0 ,即未确定
  190.         Check_Coin_2(coin ,false_sign ,1,coin_num ) ;
  191.         cout << "/n 二分法求解 ,假币位置:" << false_sign  ;
  192.         if( degree==LIGHT )
  193.             cout << "   假币较轻!/n"  ;
  194.         else
  195.             cout << "   假币较重!/n"  ;
  196. /************************************************/
  197.         false_sign = -1 ; //假币位置
  198.         degree  = 1 ;//轻重标志,初始化为1 ,即未确定
  199.         which_heavy = 0 ;//那一边比较重,初始化为0 ,即未确定
  200.         Check_Coin_3(coin ,false_sign ,1,coin_num ) ;
  201.         cout << "/n 三分法求解 ,假币位置:" << false_sign  ;
  202.         if( degree==LIGHT )
  203.             cout << "   假币较轻!/n"  ;
  204.         else
  205.             cout << "   假币较重!/n"  ;
  206. /************************************************/          
  207.         cout << "/n/n   !!!按任意键继续,Esc退出程序!!!" << endl ;
  208.     }while( (c=getch())!=27 ) ;
  209.     return  ;
  210. }
  211. /***********************************************
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值