The golden point game of pairing programming

Source: Internet
Author: User
Tags rounds

I. About the game

The Golden Dot game is a digital mini-game whose game rules are:

n students (n usually greater than ten), each writing a rational number between 0~100 ( excluding 0 or three ), to the referee, the referee calculates the average of all the numbers, and then times the 0.618(so-called golden partition constant) , the G value is obtained. The number of the submitted nearest to G(absolute value) of the students get n points, the farthest from the G students get-2 points, the other students get 0 points. after a few days of playing, we found some interesting phenomena, such as the golden dots moving down gradually.

Now please according to the rules of the game, make up a can play with a number of small game program, the requirements are as follows:

1, this job belongs to the pair programming project, must be completed by two people, and will be sent to the blog, respectively, the operation of the source code submitted to the codeing system;

2, if possible to use the C/s or b/n as much as possible, that is, using the server to receive and process all the players submitted by the number, and the results back to the players, players can be submitted by the client number;

3, if the use of single-machine approach, it is necessary to provide users with convenient input interface;

4, the game can run at least 10 rounds at a time, and can keep the results of the rounds.

Two. About the code

1. First we choose to write programs in C + +.

Originally considered to use an array to store the information to be entered, but also consider the more cumbersome to choose the map in C + +. Use the map container in the main function to store the name of the game and the number entered. Use the While loop to control the number of cycles and use Mp.clear () to clear the data after each round of the game. The specific procedures are as follows.

int main () {    int game_num;    map<string,double>mp;    int i,n;    Double Num,sum,ave;    Double g,temp=0;    string name;    printf ("Please enter the number of games:");    scanf ("%d", &n);    printf ("\ n");    printf ("Please enter the number of rounds in this game:");    scanf ("%d", &game_num);    printf ("\ n");    while (game_num--)    {    ++temp;    Mp.clear ();   Clears the data sum=0;ave=0 in the map    ; g=0;    printf ("Round%d start: \ n", temp);    printf ("Please enter each person's name and rational number (space separated): \ n");    for (i=0;i<n;i++)    {        cin>>name>>num;        if (num>0.0&&num<100.0)        {             mp[name]=num;   Establish a map of the relationship             Sum=sum+num;        }        else        {            printf ("Please enter a rational number (0-100) \ n");            i--;        }    }

2. Next calculate sum, average and G point value. Use ITER to store the absolute value of the difference between the number entered by each person and the G-spot, update the value in MP, start Mp.begin () to Mp.end () compare size, put the maximum value in Result_high, and place the minimum value in Result_high. The result of the difference is the maximum of 2 points, the difference is the smallest to get n points, N is the number of participants in the game. The rest of the people gave 0 points, outputting everyone's name and scoring. The specific procedures are as follows.

    printf ("Sum:%.4lf\n", sum);    ave=sum/n;    printf ("Average:%.4lf\n", ave);    g=ave*0.618;    printf ("G-point value:%.4lf\n", g); For (Iter=mp.begin (); Iter!=mp.end (); iter++) {Iter->second = (double) fabs (g (double) iter-&gt               ; second); } for (Iter=mp.begin (), Result_high=mp.begin (), Result_low=mp.begin (); Iter!=mp.end (); iter++) {if (iter->se        Cond<result_high->second) Result_high=iter;       if (Iter->second>result_low->second) Result_low=iter;        } cout<<endl;        cout<< "name" << "\ T" << "difference" <<endl; cout<<result_high->first<< "\ t" <<result_high->second<< "\ T" << "get" <<n        << "Points" <<endl; cout<<result_low->first<< "\ t" <<result_low->second<< "\ T" << "get-2 points" <<        Endl        cout<<endl; For (Iter=mp.begin (); Iter!=mp.end (); iter++) {iter-&Gt;second = 0;                          } for (Iter=mp.begin (); Iter!=mp.end (); iter++) {if (Iter==result_high)                  iter->second+=n;              if (Iter==result_low) iter->second+=-2;        } cout<< "<<temp<<" Round results: "<<endl;        cout<< "name" << "T" << "score" <<endl; For (Iter=mp.begin (); Iter!=mp.end (); iter++) cout<<iter->first<< "\ T" <<iter->second<         <endl; cout<<endl;

3. About Map

Map is a standard container for C + + and provides a good one-to-one relationship, and creating a map in some programs can be a multiplier effect.

Map is a class of associative containers that use map to contain the header file where map is tired, and several map keywords are used when programming.

Definition of Map<string,double>mp//map
#include <map>//header File
Begin ()
End ()
Clear ()//clear

4. Program Run Results

Three. About pairing programming

This time the programming requirement is to need two person pair programming, together completes the entire program design and the writing. My programming teammate is Cheng, he is a serious and studious person, with his pair of programming can learn a lot of things, his programming ability is stronger, and my programming ability is weak, many things are under his help guidance to complete, here express thanks. In addition, I also learned a lot in the programming process, I hope to have the opportunity to learn together.

Attach a photo of work:

Four. Program code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <math.h>
using namespace Std;
int main ()
{
int game_num;
map<string,double>mp;
int i,n;
Double num,sum,ave,g;
int temp=0;
String name;
printf ("Please enter the number of games:");
scanf ("%d", &n);
printf ("\ n");
printf ("Please enter the number of rounds in this game:");
scanf ("%d", &game_num);
printf ("\ n");
    while (game_num--)
{
++temp;
Mp.clear (); Empty the data in the map
sum=0;ave=0; g=0;
printf ("Round%d start: \ n", temp);
printf ("Please enter each person's name and rational number (space separated): \ n");
for (i=0;i<n;i++)
{
        cin>>name>>num;
if (num>0.0&&num<100.0)
{
Mp[name]=num; Establishing a map relationship
Sum=sum+num;
}
Else
{
printf ("Please enter (0-100) the rational number \ n");
i--;
}
}
    Map<string,double>::iterator Iter,result_high,result_low;
    printf ("Sum:%.4lf\n", sum);
ave=sum/n;
printf ("Average:%.4lf\n", ave);
g=ave*0.618;
printf ("G-point value:%.4lf\n", g);
    For (Iter=mp.begin (); Iter!=mp.end (); iter++)
{
Iter->second = (double) fabs (g (double) iter->second);
              }
    For (Iter=mp.begin (), Result_high=mp.begin (), Result_low=mp.begin (); Iter!=mp.end (); iter++)
{
if (Iter->second<result_high->second)
Result_high=iter;
if (Iter->second>result_low->second)
Result_low=iter;
}
        cout<<endl;
cout<< "name" << "\ T" << "difference" <<endl;
cout<<result_high->first<< "\ t" <<result_high->second<< "\ T" << "get" <<n << "Points" <<endl;
cout<<result_low->first<< "\ t" <<result_low->second<< "\ T" << "get-2 points" << Endl
cout<<endl;
        For (Iter=mp.begin (); Iter!=mp.end (); iter++)
{
Iter->second = 0;
}
         For (Iter=mp.begin (); Iter!=mp.end (); iter++)
{
if (Iter==result_high)
iter->second+=n;
if (Iter==result_low)
iter->second+=-2;
}
cout<< "<<temp<<" Round results: "<<endl;
cout<< "name" << "T" << "score" <<endl;
For (Iter=mp.begin (); Iter!=mp.end (); iter++)
cout<<iter->first<< "\ T" <<iter->second<<endl;
cout<<endl;
}
return 0;
}

The golden point game of pairing programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.