标签:
1 //Zhao Ziyin 2016/5/18 寻找水王 2 3 #include<iostream> 4 #include<fstream> 5 using namespace std; 6 #define MAX 50 7 8 void Find(int id[], int lenth, int &water) 9 { 10 int i = 0, count = 0; //count用来记录某id发帖次数 11 for (i = 0; i<lenth; i++) 12 { 13 if (count == 0) //次数为零 14 { 15 water = id[i]; //假设当前的id为水王 16 count++; 17 } 18 else 19 { 20 if (water == id[i]) //若前后id相同,水王的id不变,次数加1 21 count++; 22 else 23 count--; 24 } 25 } 26 } 27 int main(int argc, char* argv[]) 28 { 29 int ID[MAX]; //发帖记录表 30 int in[MAX+1]; //从文件中读出的数组 31 int i , num, King; //发帖数目、水王 32 /*cout << "请输入帖子的数量:"; 33 cin >> num; 34 cout << "请输入发帖ID:"; 35 for (int i = 0; i<num; i++) 36 { 37 cin >> ID[i]; 38 }*/ 39 40 //读取文件中的id号 41 ifstream infile("id_form.txt", ios::in); 42 if (infile.is_open() == false) 43 { 44 cerr << "open error!" << endl; 45 exit(1); 46 } 47 infile >> in[0];//读取帖子总数 48 num = in[0]; 49 cout << "帖子的总数为:"<<in[0]<<endl; 50 51 cout << "发帖的id号分别为:" << endl; 52 for (i = 0; i < num; i++) 53 { 54 infile >> in[i+1]; 55 ID[i] = in[i+1]; 56 cout << ID[i]<<" "; 57 } 58 cout << endl; 59 60 Find(ID, num, King); 61 cout << endl << "找到水王!!!" << endl; 62 cout << "发帖水王的id为:" << King << endl; 63 infile.close(); 64 return 0; 65 }
标签:
原文地址:http://www.cnblogs.com/2016helen/p/5507279.html