标签:
一、题目要求
#include<iostream.h>
void main()
{
int arr[11]={1,2,2,3,4,2,2,5,3,2,2};//假设的ID号
int shuiwang = 0;
int count=0; //标记
for(int i=0;i<11;i++)
{
if(count == 0)
{
shuiwang = arr[i];
count = 1;
}
else
{
if(shuiwang == arr[i])
count ++;
else
count --;
}
}
cout<<"水王的ID是"<<shuiwang<<endl;
}
四、截图

五、总结
这次实验题目有个条件就是水王发帖的ID号超过所以ID号一半以上,因此这就是这题的突破点,对于时间复杂度,好的设计思路可以大大降低时间复杂度,比如这题就可以将时间复杂度从O(N*N)降低到O(N)。
标签:
原文地址:http://www.cnblogs.com/2015tan/p/4448218.html