发帖水王扩展题目-附带测试数据代码

欢迎使用Markdown编辑器

你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

题目

扩展问题:

随着Tango的发展,管理员发现,“超级水王”没有了。统计结果表明,有3个发帖很多的ID,他们的发帖数目都超过了帖子总数目N的1/4。你能从发帖ID列表中快速找出他们的ID吗?

思路

首先当只有一个水王,其发帖超过总数目的1/2的时候,采用以下思路

int candiate;
int Times = 1;
for i in 数组:
    if(candiate == i) Times++;
    else{
    	if(Times == 0){
    		candiate = i;
    		Times = 1;
    	}
    	else{
    		Times--;
    	}
    }

那么首先当概率变成1/4的时候,也就意味着每四个数中这个数只需要出现一次,也就是当遇到和当前的数一样的时候,Times++ 就应该是+3,如果只需要求一位水王的话,思路如下:

int candiate;
int Times = 3;
for i in 数组:
    if(candiate == i) Times += 3;
    else{
    	if(Times == 0){
    		candiate = i;
    		Times = 3;
    	}
    	else{
    		Times--;
    	}
    }

c++代码

// 寻找发帖水王-测试
#include <iostream>
#include <time.h>

using namespace std;

// when initOrRandom is 1,it means this function is used to init, zero to random.
void changeStoreData(int storeData[], const int totalLen, bool initOrRandom){
    if(initOrRandom){
        for(int i = 0; i < totalLen; i++){
            storeData[i] = -1;
        }
    }
    else{
        for(int i = 0; i < totalLen; i++){
            if(storeData[i] == -1){
                storeData[i] = rand();
            }
        }
    }
}
void generateTestData(const int totalLen, int num,int storeData[],int leastTimes){
    while(leastTimes){
        int randomNum =  rand()%totalLen;
        // 该位置没有存储数据
        if(storeData[randomNum]==-1){
            storeData[randomNum] = num;
            leastTimes--;
        }
    }
}

void printTestData(int storeData[], const int totalLen){
    for(int i = 0; i < totalLen; i++){
        cout<<storeData[i]<<"  ";
        if((i+1)%10 == 0){
            cout<<endl;
        }
    }
} 
// 找出发帖水王
void  findShuiWang(int storeData[], const int totalLen, int shuiWang[3]){
    int cntTimes[3] = {0};
    shuiWang[0] = shuiWang[1] = shuiWang[2] = storeData[0];
    for(int i = 1; i < totalLen; i++){
        if(storeData[i] == shuiWang[0]){
            cntTimes[0] += 3;
        }
        else if(storeData[i] == shuiWang[1]){
            cntTimes[1] += 3;
        }
        else if(storeData[i] == shuiWang[2]){
            cntTimes[2] += 3;
        }
        else if (cntTimes[0] == 0){
            shuiWang[0] = storeData[i];
            cntTimes[0] =3;
        }
        else if (cntTimes[1] == 0){
            shuiWang[1] = storeData[i];
            cntTimes[1] =3;
        }
        else if (cntTimes[2] == 0){
            shuiWang[2] = storeData[i];
            cntTimes[2] =3;
        }
        else {
            cntTimes[2] =0;
            cntTimes[1] =0;
            cntTimes[0] =0;
        }
    }
}

int main(){
    srand(time(NULL));     // 设置随机数种子 
    const int totalLen = 200;
    int storeData[totalLen];
    changeStoreData(storeData,totalLen,true);
    int leastTimes = totalLen/4 +1;
    // 产生三个不同的数
    int num[3];
    num[0] = rand();
    do{
        num[1] = rand();
    }while(num[1] == num[0]);
    do{
        num[2] = rand();
    }while(num[2] == num[0] || num[2] == num[1]);
    // 生成数据
    for(int i = 0; i < 3; i++){
        generateTestData(totalLen,num[i],storeData,leastTimes);
            
    }
    changeStoreData(storeData,totalLen,false);
    cout<<"TestData: "<<endl;
    printTestData(storeData, totalLen);
    int ans[3];
    findShuiWang(storeData,totalLen,ans);
    cout<<"num: "<<endl;
    printTestData(num, 3);
    cout<<endl<<"ans: "<<endl;
    printTestData(ans, 3);
    return 1;
}


代码并没有穷尽所有的测试样例,所以不一定就是完全正确的,如果有不通过的测试样例,欢迎大家评论和批评指正~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值