设计思路:比较4个ID,如果每四个ID号不相同,就减一,最后剩下的一定是三个小水王的ID
代码设计
1 #include<iostream> 2 usingnamespacestd; 3 4 voidFind_King(intshui_ID[],intamount_ID) 5 { 6 intID[3]; 7 intCounts[3]={0,0,0}; 8 inti=0; 9 for(i=0;i<amount_ID;i++) 10 { 11 12 if(Counts[0]==0 && shui_ID[i]!=ID[1] && shui_ID[i]!=ID[2]) 13 { 14 Counts[0]=1; 15 ID[0]=shui_ID[i]; 16 } 17 elseif(Counts[1]==0 &&shui_ID[i]!=ID[0] && shui_ID[i]!=ID[2]) 18 { 19 Counts[1]=1; 20 ID[1]=shui_ID[i]; 21 } 22 elseif(Counts[2]==0 &&shui_ID[i]!=ID[0] && shui_ID[i]!=ID[1]) 23 { 24 Counts[2]=1; 25 ID[2]=shui_ID[i]; 26 } 27 elseif(shui_ID[i]!=ID[0] && shui_ID[i]!=ID[1] && shui_ID[i]!=ID[2]) 28 { 29 Counts[0]--; 30 Counts[1]--; 31 Counts[2]--; 32 } 33 elseif(shui_ID[i]==ID[0]) 34 { 35 Counts[0]++; 36 } 37 elseif(shui_ID[i]==ID[1]) 38 { 39 Counts[1]++; 40 } 41 elseif(shui_ID[i]==ID[2]) 42 { 43 Counts[2]++; 44 } 45 } 46 for(i=0;i<3;i++) 47 { 48 cout<<ID[i]<<endl; 49 } 50 } 51 52 intmain() 53 { 54 int ID[10]={1,2,3,1,3,2,3,2,1,4}; 55 cout<<"ID: "<<endl; 56 for(inti=0;i<10;i++) 57 { 58 cout<<ID[i]<<" "; 59 } 60 cout<<endl; 61 62 cout<<"小水王是:"<<" "<<endl; 63 Find_King(ID,10); 64 65 return0; 66 }
截图: