二分覆盖

简介: #include #include #include #include #include using namespace std; //this program's file name is coin.

#include <iostream>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <string.h>
using namespace std;

//this program's file name is coin.cpp
//this program is designed by 200624101101杨振平 on NOV 22th,2008

//the algorithm of divide into two parts
int find2(int coin[], int low, int high);
//the algorithm of divide into three parts
int find3(int coin[], int low, int high);
//main function
void  main()
{
 //define variables
    int i, *coin,n,temp;
 
    cout <<"Please import the coin number (the n maximum is a machine round number numerical value):";
    cin>>n;
 //srand()function generate a random seed which begin with the current time
    srand( (unsigned)time( NULL ) );
    cout <<"The random produces a data in course of (use 0 and 1 to represent counterfeit money and real currency respectively).../n/n";
 //genenrate one more position use coin[0] to store the count of call weight this time
    coin=new int[n+1];
 //randomly generate a number between 1 and n
    temp=1+rand()%n;
 //counter
    coin[0]=0;
 //initial the coin array
    for(i=1; i <n+1; ++i)
       coin[i]=(i!=temp)?1:0;
    //print the coin array       
    for(i=1; i <n+1; ++i)
        if(i!=temp)
            cout <<"The " <<setw(3) <<i <<"th coin is real currency(1)" <<endl;
        else
            cout <<"The " <<setw(3) <<i <<"th coin is counterfeit money(0)" <<endl;
     //print the result of the algorithm of divide into two parts
        cout <<endl <<endl <<"the result of counterfeit money with the algorithm of divide into two parts:" <<endl;
        cout <<"counterfeit money is in the" <<setw(3) <<find2(coin,1,n) <<"th position!" <<endl;
        cout <<"the total count of weight is" <<setw(3) <<coin[0] <<" times!" <<endl;
     //print the result of the algorithm of divide into three parts
        cout <<endl <<endl <<"the result of counterfeit money with the algorithm of divide into three parts:" <<endl;
        cout <<"counterfeit money is in the" <<setw(3) <<find3(coin,1,n) <<"th position!" <<endl;
        cout <<"the total count of weight is" <<setw(3) <<coin[0] <<" times!" <<endl <<endl <<endl;
}
//implement of find2 function
int find2(int coin[], int low, int high)
{
 //define variables
    int i,mid,sum1,sum2,sign;
   
    coin[0]=0;
    do //Location instructed by lower bound and upper bound
    {
            mid=(low+high)/2;
            sign= (low+high)%2;//Mark odd number and even number
            //cout <<sign <<endl;
            sum1=sum2=0;

            if(sign==0)//Handle the odd number condition
            {   
                for(i=low; i <mid; i++)
                    sum1 += coin[i];
                  for(i=mid+1; i <=high; i++)
                    sum2 += coin[i];
         
                  if( sum1==0 && sum2==0  )
                  ;
                  else
                    coin[0] += 1;

                if(sum1==sum2) 
                    return mid;
                else if(sum1 <sum2) 
                {
                    high=mid-1;
                }
                else 
                {
                    low=mid+1;
                }
           
            }
              else//Handle the even number condition
            {
                for(i=low; i <=mid; i++)
                    sum1 += coin[i];
                for(i=mid+1; i <=high; i++)
                    sum2 += coin[i];
                if( sum1==0 && sum2==0  )
                  ;
                else
                    coin[0] += 1;
                if(sum1 <sum2) 
                    high=mid;
                else 
                    low=mid+1;
            }
             
    }while(1);//end while
 
    return -1;
}

//implement of find3 function
int find3(int coin[], int low, int high)
{
 //define variables
    int i,mid1,mid2,sum1,sum2,sum3,flag,my_space;
   
    coin[0]=0;

    if(high <3)
        {cout <<"您必须键入的个数要大于3" <<endl <<endl <<endl; exit(1);}
    while(low <=high) //Location instructed by lower bound and upper bound
    {
            sum1=sum2=sum3=0;
            my_space=(high-low+1)/3;   
              mid1=low+my_space-1;
            mid2=mid1+my_space;
   //The remainder that mark is got rid of by 3,
   //is that the remainder is 0 , 1 , 2 respectively
            flag= (high-low+1) % 3;
           
            for(i=low; i <=mid1; i++)
                sum1 += coin[i];
            for(i=mid1+1; i <=mid2; i++)
                sum2 += coin[i];
            for(i=mid2+1; i <=mid2+my_space; i++)  //Pay attention to here
                sum3 += coin[i];
            if( sum1==0 && sum2==0 && sum3==0 )
              ;
            else
                coin[0] += 1;
            if(flag==0)//Handle a remainder being 0's condition
            {
                    if(sum1==sum2)
                    low=mid2+1;
                if(sum1==sum3)
                {  low=mid1+1; high=mid2; }
                if(sum2==sum3)
                    high=mid1;
            }
            else if(flag==1)//Handle a remainder being 1's condition
            {
                if( sum1==sum2)
                {
                      if(sum1==sum3)
                      { return high;}
                      else
                      {low=mid2+1; high=high-1;}
                }
                else
                {
                      if(sum1==sum3)
                      { low=mid1+1; high=mid2;}
                      if(sum2==sum3)
                      { high=mid1; }
               
                }

            }   
            else//Handle a remainder being 2's condition
            {
                     
                 
                if( sum1==sum2)
                {
                      if(sum1==sum3)
                      {
                          coin[0] += 1;
                          if(coin[high-1]==0)
                              return high-1;
                          else
                              return high;
                     
                      }
                      else
                      {low=mid2+1; high=high-2;}
                }
                else
                {
                      if(sum1==sum3) 
                      { low=mid1+1; high=mid2;}
                      if(sum2==sum3)
                      { high=mid1; }
                }
            }          
    }//end while
 
    return -1;
}

目录
相关文章
|
6月前
|
测试技术
【动态规划】【状态压缩】LCP04 覆盖
【动态规划】【状态压缩】LCP04 覆盖
|
6月前
|
算法 测试技术 C#
C++二分查找算法:包含每个查询的最小区间
C++二分查找算法:包含每个查询的最小区间
|
6月前
|
算法 测试技术 C#
【图论 状态压缩 枚举】2959. 关闭分部的可行集合数目
【图论 状态压缩 枚举】2959. 关闭分部的可行集合数目
|
6月前
|
机器学习/深度学习 移动开发
【归并排序】【图论】【动态规划】【 深度游戏搜索】1569将子数组重新排序得到同一个二叉搜索树的方案数
【归并排序】【图论】【动态规划】【 深度游戏搜索】1569将子数组重新排序得到同一个二叉搜索树的方案数
|
6月前
代码随想录Day30 贪心05 LeetCode T435无重叠区间 T763划分字母区间 T56 合并区间
代码随想录Day30 贪心05 LeetCode T435无重叠区间 T763划分字母区间 T56 合并区间
50 0
|
6月前
|
算法 程序员 索引
【算法训练-二分查找 一】【基本二分】二分查找、在排序数组中查找元素的第一个和最后一个位置
【算法训练-二分查找 一】【基本二分】二分查找、在排序数组中查找元素的第一个和最后一个位置
56 0
|
11月前
|
算法 C# C++
C++二分算法:找到最接近目标值的函数值(二)
C++二分算法:找到最接近目标值的函数值
|
11月前
|
算法 测试技术 C++
C++二分算法:找到最接近目标值的函数值(一)
C++二分算法:找到最接近目标值的函数值
|
存储
LeetCode题:88合并两个有序数组,283移动零,448找到所有数组中消失的数字
LeetCode题:88合并两个有序数组,283移动零,448找到所有数组中消失的数字
68 0
|
人工智能 算法 BI
【贪心策略】区间问题、背包问题、贪心策略注意事项
【贪心策略】区间问题、背包问题、贪心策略注意事项
132 0