枚举

2017-10-21  本文已影响0人  皮了个卡丘喵喵哒

完美立方:
#include <iostream>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    int N;
    cin>>N;
    for(int a=2;a<=N;a++)
    {
        for(int b=2;b<a;b++)
        {
            for(int c=b;c<a;c++)
            {
                for(int d=c;d<a;d++)
                {
                    if(a*a*a==b*b*b+c*c*c+d*d*d)
                    {
                        cout<<"Cube = "<<a<<",";
                        cout<<"Triple = ("<<b<<","<<c<<","<<d<<")"<<endl;
                    }
                }
            }
        }
    }
    return 0;
}

生理周期:
#include <iostream>
using namespace std;
#define N 21252
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    int p,e,i,d,caseNo=0;
    while(cin>>p>>e>>i>>d &&p!=-1)
    {
        ++caseNo;
        int k;
        for(k=d+1;(k-p)%23;++k);
        for(;(k-e)%28;k+=23);
        for(;(k-i)%33;k+=23*28);
        cout<<"Case "<<caseNo<<
        " :The next triple peek occurs in "<<k-d<<"days."<<endl;
        
    }
    return 0;
}

称硬币:
#include <iostream>
#include <cstring>
char Left[3][7];//天平左侧硬币,一共有12个硬币,称量的时候一边放6个,三次称量,所以数组开[3][7].
char Right[3][7];//天平右侧硬币
char result[3][7];//三次称量结果
bool isFake(char c,bool light);//假设硬币c是假币,light==true时假设它为轻,light==false时假设为重,返回值为true表示假设成立
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    int t;
    cin>>t;
    while(t--)
    {
        for(int i=0;i<3;++i)cin>>left[i]>>right[i]>>result[i];
        for(char c='A';c<='L';c++)//从'A'硬币到'L'硬币枚举
        {
            if(isFake(c,true))
            {
                cout<<c<<"is the counterfeit coin and it is light."<<endl;
                break;
            }
            if(isFake(c,false))
            {
                cout<<c<<"is the counterfeit coin and it is heavy."<<endl;
                break;
            }
            
        }
    }
    return 0;
}

//下面这个函数很重要,是本题的核心
bool isFake(char c,bool light)
{
    //light==true假设假币为轻,否则假设假币为重 
    for(int i=0;i<3;++i)
    {
        char* pleft;  //指向天平两边的字符串
        char* pright;
        if(light)   
        {
            pleft=Left[i];
            pright=Right[i];
        }
        else
        {
            pleft=Right[i];      //这么做是为了让下面的代码可重用
            pright=Left[i];
        }       
                //假设假币为轻
                switch(result[i][0])
        {
            case 'u':
                if(strchr(pright,c)==NULL)  //右侧up,右侧轻,轻假币在右侧。通过调用strchr判断char c是否在pright里
                    return false;
                break;
            case 'e':
                if(strchr(pright,c) || strchr(pright,c))  //两边相平,两侧都没有假币
                    return false;
                break;
            case 'd':
                if(strchr(pleft,c)==NULL)    //右侧下沉,轻假币在左侧
                    return false;
                break;
                
        } 
        
    }
    return true;
}

熄灯问题:
#include <iostream>
#include <string>
#include <cstring>
#include <memory>
using namespace std;
char oriLights[5];//原始灯亮灭情况
char lights[5];// 运行时灯亮灭情况
char result[5];//存放按开关的情况
int getBit(char c,int i);
void setBit(char &c,int i,int v);
void flipBit(char& c,int i);
void printResult(int i,char result[]);
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    int T;
    cin>>T;
    for(int t=1;t<=T;++t)
    {
        for(int i=0;i<5;++i)
        {
            for(int j=0;j<6;++j)
            {
                int s;
                cin>>s;
                setBit(oriLights[i],j,s);
            }
        }
        for(int n=0;n<64;++n)//枚举第一行按开关情况000000~111111 
        {
            int switchs=n;
            memcpy(lights,oriLights,sizeof(oriLights));
            for(int i=0;i<5;++i)
            {
                result[i]=switchs;
                for(int j=0;j<6;++j)//改变同行灯状态 
                {
                    if(getBit(switchs,j))
                    {
                        if(j>0)flipBit(lights[i],j-1);
                        flipBit(lights[i],j);
                        if(j<5)flipBit(lights[i],j+1);
                    }
                }
                lights[i+1]^=switchs;//改变下一列灯状态
                switchs=lights[i]; //下一行按开关取决于这一行灯的亮灭情况,如果本行某一列灯亮,就要在下一行按相同列的开关让它熄灭
            }
            if(lights[4]==0)
            {
                printResult(t,result);
                break;
            }
        }
    }
    return 0;
}

int getBit(char c,int i) 
{
    return (c>>i)&1;
}

void setBit(char &c,int i,int v)
{
    if(v)
    {
        c |= (1<<i);
    }
    else
    {
        c &= ~(1<<i);
    }
}

void flipBit(char& c,int i)
{
    c ^= (1<<i);
}

void printResult(int i,char result[]) 
{
    cout<<"PUZZLE #"<<endl;
    for(int i=0;i<5;++i)
    {
        for(int j=0;j<6;++j)
        {
            cout<<getBit(result[i],j);
            if(j<5)cout<<" ";
        }
        cout<<endl;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读