1. 程式人生 > >黃金點遊戲程序

黃金點遊戲程序

double style main 成功 space insert using 重復 iter

 1 #include"iostream"
 2 #include"windows.h"
 3 #include"string"
 4 #include"map"
 5 using namespace std;
 6 int main()
 7 {
 8     int check = 1;
 9     while (check == 1)
10     {
11         int people = 0;
12         double ave = 0.0;
13         int max = 0;
14         map<int, string>mapStudent;
15 pair< std::map< int, string >::iterator, bool > ret; 16 cout << "請輸入黃金點遊戲的範圍:" << endl; 17 cin >> max; 18 cout << "請輸入遊戲人數:" << endl; 19 cin >> people; 20 for (int i = 0; i < people; i++) 21 {
22 23 int ch; 24 string name; 25 cout << "黃金點的範圍:" << max << endl << "請輸入你的點數:" << endl; 26 cin >> ch; 27 if (ch>max || ch<0) 28 { 29 cout << "輸入的數範圍錯誤,請重新輸入:" << endl;
30 cout << "黃金點的範圍:" << max << endl << "請輸入你的點數:" << endl; 31 cin >> ch; 32 } 33 cout << "請輸入你的姓名:" << endl; 34 cin >> name; 35 ret = mapStudent.insert(pair<int, string>(ch, name)); 36 if (ret.second){ 37 std::cout << "輸入成功:" << std::endl; 38 Sleep(500); 39 system("cls"); 40 } 41 else{ 42 std::cout << "輸入的數重復,請重新輸入" << std::endl; 43 people++; 44 Sleep(500); 45 system("cls"); 46 } 47 } 48 map<int, string>::iterator iter; 49 for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) 50 { 51 ave += iter->first; 52 } 53 ave = (ave / 3.0)*0.618; 54 cout << "\t黃金點數" << " " << "\t差值(輸入的數與平均數的差)" << " " << "\t姓名" << endl; 55 for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) 56 { 57 double n = 0.0; 58 n = abs(iter->first - ave); 59 cout << "\t" << ave << " " << "\t\t" << n << " " << "\t\t\t\t" << iter->second << endl; 60 } 61 cout << "繼續遊戲請按1,退出遊戲請按其他鍵!" << endl; 62 cin >> check; 63 } 64 system("pause"); 65 return 0; 66 }

黃金點遊戲程序