找小水王

#include <iostream>
using namespace std;
#define MAXSIZE 30


void FindWater(int ID[], int len,int water[])
{
    int counter[3];                    //计数器
    counter[0]=counter[1]=counter[2]=0;
    water[0]=water[1]=water[2]=-1;    //初始化嫌疑人列表
    for(int i=0;i<len;i++)
    {
        if(counter[0]==0)            //先赋值
        {
            counter[0]++;
            water[0]=ID[i];
        }
        else if(counter[1]==0)
        {
            counter[1]++;
            water[1]=ID[i];
        }
        else if(counter[2]==0)
        {
            counter[2]++;
            water[2]=ID[i];
        }
        else if(ID[i]==water[0])    //如果counter为0,重新赋值
        {
            counter[0]++;
        }
        else if(ID[i]==water[1])
        {
            counter[1]++;
        }
        else if(ID[i]==water[2])
        {
            counter[2]++;
        }
        else                        //如果与三个嫌疑人的ID都不同的话,则三个计数器都自减一,直到counter为0
        {
             counter[0]--;
             counter[1]--;
             counter[2]--;
         }
    }
}

int main(int argc, char* argv[])
{
    int ID[MAXSIZE];                //发帖记录表
    int num,WaterKing[3];            //发帖数目、水桶
    cout<<"请输入帖子的数量:";
    cin>>num;
    cout<<"请输入发帖ID:";
    for(int i=0;i<num;i++)
    {
        cin>>ID[i];
    }
    FindWater(ID,num,WaterKing);
    cout<<"水王是:";
    for(int i=0;i<3;i++)
    {
        cout<<WaterKing[i]<<" ";
    }
    cout<<endl;
    return 0;
}

posted on 2016-05-23 13:55  897897  阅读(127)  评论(0编辑  收藏  举报

导航