C++实现 L1-056 猜数字 (20分)

C++实现 L1-056 猜数字 (20分)

一、描述

一群人坐在一起,每人猜一个 100 以内的数,谁的数字最接近大家平均数的一半就赢。本题就要求你找出其中的赢家。

二、输入格式

输入在第一行给出一个正整数 N ( ≤ 1 0 4 ) N(≤10^4) N(104)。随后 N 行,每行给出一个玩家的名字(由不超过8个英文字母组成的字符串)和其猜的正整数 ( ≤ 100 ) (≤ 100) (100)

三、输出格式

在一行中顺序输出:大家平均数的一半(只输出整数部分)、赢家的名字,其间以空格分隔。题目保证赢家是唯一的。

四、输入样例

7
Bob 35
Amy 28
James 98
Alice 11
Jack 45
Smith 33
Chris 62

五、输出样例

22 Amy

六、思路

  1. 定义people结构体,包含姓名和报数;
  2. 录入的同时计算总数;
  3. 计算平均数/2,输出其整数部分;
  4. 遍历寻找绝对值最小的人,输出名字。

七、小知识点

  1. 浮点数取整数floor();
  2. 浮点数取绝对值fabs();

八、代码

#include <iostream>
#include <cmath>
using namespace std;

struct people
{
    string name;//姓名
    int num;//猜的数字
};
int main()
{
    int n;//录入n个人
    cin>>n;
    struct people p[n];//用于记录所有信息
    int sum=0;//记录总数
    for(int i=0;i<n;i++)//录入并计算总数
    {
        cin>>p[i].name>>p[i].num;
        sum+=p[i].num;
    }
    double ave=(double)sum/(2*n);//计算平均值/2
    cout<<floor(ave)<<' ';//输出平均值的整数部分
    string s;//记录赢家姓名
    double cz=9999999;//差值
    for(int i=0;i<n;i++)//遍历找到绝对值最小的
    {
        if(fabs(p[i].num-ave)<cz)
        {
            s=p[i].name;
            cz=fabs(p[i].num-ave);
        }
    }
    cout<<s;
    return 0;
}

guessing and betting number game The user starts with 100 dollars, and is able to bet and guess until they quit or have no more money. In each turn, they are asked for a bet. If they enter 0, then the program should give a farewell message and quit. The bet must be positive and within their available balance, If they enter incorrectly, the program should set the bet to their balance and tell them this. Once a bet is entered, they must pick a number between 1 and 10 (you do not need to errorcheck or correct this). This guess is then compared to a number the computer randomly generates each time (also between 1 and 10). If the guess is correct, the user wins the amount of their bet. If the guess is incorrect, the user will lose their bet divided by 5 and multiplied by the distance from the correct number - e.g. if the bet is 50, the computer’s number is 5 and the user guesses 7, then the user will lose 20 dollars( 50/5 * (7-5) = 20 ). However, they should not lose more than their balance, so if their balance is 50, the bet is 40, the computer’s number is 1 and the user guesses 10, (40/5 * (10-1) = 72 > 50 )then they should lose 50. After each turn, the program should display that turn's winnings (or loss amount) and the new balance,and then repeat the game until the user either quits or has no money left. Dollar values should be formatted in the standard way with two decimal places and a $ in front. Sample output from the program is below. You should make your program match this exactly (except for your name). An appropriate welcome message with your name in it should be shown at the start of the program. Assignment 1 : Guessing Game Written by yourname Please enter your bet (up to $100.00): $50 Guess a number between 1 and 10: 5 Correct! You win $50.00 Your new balance is $150.00 Please enter your bet (up to $150.00): $200 Your bet is $150.00 Guess a number between 1 and 10: 6 Wrong! The computer chose: 4 You lose $60.00 Your new balance is $90.00 Please enter your bet (up to $90.00): $80 Guess a number between 1 and 10: 1 Wrong! The computer chose: 7 You lose $90.00 Thank you for playing! Press any key to continue . . . Another Sample: CP1200 Guessing Game Written by Lindsay Ward Please enter your bet (up to $100.00): $-20 Your bet is $100.00 Guess a number between 1 and 10: 5 Wrong! The computer chose: 2 You lose $60.00 Your new balance is $40.00 Please enter your bet (up to $40.00): $10.50 Guess a number between 1 and 10: 12 Wrong! The computer chose: 2 You lose $21.00 Your new balance is $19.00 Please enter your bet (up to $19.00): $0 Thank you for playing! Press any key to continue . . . srand( static_cast<int>(time(0))) ; 初始化 computerNumber = 1 + rand( ) % 10 ; 产生随机数 cout << fixed << setprecision(2) ; 控制输出显示2位小数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值