猜数(C++)

链接:https://ac.nowcoder.com/acm/contest/5389/B
来源:牛客网

题目描述

纸上写了 n 个数字,牛牛在之前改动了几个数字,他忘了他具体改了些数字了。

但是他记得动之前这些数字的和是 ≥m 的,求他最少改动几个数字。

注意:这些数字在改之前和改之后均在 0 ~ 9 之间,且为整数。

输入描述:

第一行给出 n,m

第二行给出 n 个 0 ~ 9 的整数

输出描述:

输出最少改动了几个数字。(保证答案 ≤n)

示例1

输入

2 3
1 1

输出

1

备注:

对于 50  %数据有 n≤20

对于 100  %数据有 n≤10^6




思想:贪心。

我的思路:首先sort一下,排完序后从最小的数字开始,从小到大变成9,知道和大于m的值。

代码:

#include<bits/stdc++.h>
using namespace std;
int n,m,sum=0,ans=0;
int a[1000001];
int main() {
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
    	scanf("%d",&a[i]);
    	sum+=a[i];
	}
	if(sum>=m)
	{
		printf("%d\n",ans);
		return 0;
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n+1;i++)
	{	sum=sum+9-a[i];
		if(sum>=m)
		{
			printf("%d\n",ans+1);
			return 0;
		}
		else
		{
			ans++;
		}
	}

    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位小
C++中编写一个名为"51.3 纸杯"的游戏代码通常涉及到经典的概率测算法。这是一个典型的“蒙提霍尔问题”变种,家需要通过测装有不同量水的纸杯来赢取奖品。这里是一个简单的伪代码描述: ```cpp #include <iostream> #include <cstdlib> // 需要随机库 int cups[3] = {0, 1, 2}; // 初始化三个杯子,分别装0、1或2个球 bool openDoor(int target); // 函,模拟打开未包含目标球的门 void game() { int target; // 目标球的位置 std::cout << "欢迎参加纸杯游戏哪个纸杯里有球(1、2或3):" << std::endl; std::cin >> target; while (true) { if (openDoor(target)) { std::cout << "恭喜你,你赢了!目标球在第" << target << "个杯子里。\n"; break; } else { std::cout << "很遗憾,你错了。请再试一次:" << std::endl; } } } // 模拟打开一扇门并返回是否包含目标球 bool openDoor(int target) { // 随机选择一个非目标的杯号 int revealedCup = cups[rand() % 3]; if (revealedCup == target) return false; // 如果揭示的杯子就是目标,重置并尝试再次 std::swap(cups[target], cups[2]); // 移除目标球到最后一杯,模拟主持人动作 return true; } int main() { game(); return 0; } ``` 请注意,这只是一个简化的版本,实际运行时可能需要用户输入循环以及更完善的错误处理。此外,由于我们在这里没有实现完随机过程,`rand()`函可能会导致不公平的游戏体验,如果在真实项目中使用,应当引入真随机生成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值