Good Fours and Good Fives(4和五)(C++)

考完期末,作者又回来更新了!!

题目描述:

FinnFinn 非常喜欢 44 和 55,他认为所有的数都可以用 44 和 55 进行相加得出。

例:

14=5+5+4

20=4+4+4+4+4或 20=5+5+5+5。

40=4+4+4+4+4+4+4+4+4+4 或 40=4+4+4+4+4+5+5+5+5或 40=5+5+5+5+5+5+5+5

当然,4和5的顺序并不重要,重要的是他们的个数。

给你一个正整数n,问有多少种方法可以用4和5拼凑成n。

输入格式:

一行,一个整数n,表示要被拼凑的数。

输出格式:

一行,表示方法的数量。如果这个数不能被拼凑,请输出 0。

输入样例 #1:

14

输出样例 #1: 

1

输入样例 #2: 

40

输出样例 #2: 

3

输入样例 #3: 

6

输出样例 #3: 

0


分析: 

这道题作者用一个简单易懂的方法。

首先,我们知道这道题让我们用4和5组合成n,那么这就成了一道简单的一元二次方程

n=4x+5y,x和y代表4和5的数量,我们知道了组合的方程,但这题问我组合的数量,那这又成了枚举题。

从0开始到n/4枚举x,然后看5能不能出金身下的,如果可以方案加1。

理论存在,开始实践!


代码: 

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n,ans=0;
	cin>>n;//给的数字
	for(int i=0;i<=n/4;i++)//枚举4的个数
	{
		if((n-i*4)%5==0)//如果剩下部分5能处进
		{
			ans++;//答案++
		}
	}
	cout<<ans;//输出答案
	return 0; //return 0是个好习惯 
}

看完了,你学会这个简单的方法了吗? 

 题目:洛谷P8395 [CCC2022 S1] Good Fours and Good Fives,直接AC

有问题可以在评论区讨论!!

A: 以下是代码示例: ``` #include<iostream> #include<iomanip> using namespace std; void PrintScorecard(int die1, int die2, int die3, int die4, int die5, int round1, int round2, int round3, int round4, int round5, int round6, int round7, int round8, int round9, int round10, int round11, int round12, int round13) { int totalScore = round1 + round2 + round3 + round4 + round5 + round6 + round7 + round8 + round9 + round10 + round11 + round12 + round13; cout << "Scorecard" << endl << endl; cout << left << setw(25) << "Ones:" << setw(5) << die1 << " " << right << setw(5) << round1 << " " << left << setw(5) << (die1 == 1 ? die1 : "-") << " " << right << setw(5) << (round1 != 0 ? round1 : "-") << endl; cout << left << setw(25) << "Twos:" << setw(5) << die2 << " " << right << setw(5) << round2 << " " << left << setw(5) << (die2 == 2 ? die2 : "-") << " " << right << setw(5) << (round2 != 0 ? round2 : "-") << endl; cout << left << setw(25) << "Threes:" << setw(5) << die3 << " " << right << setw(5) << round3 << " " << left << setw(5) << (die3 == 3 ? die3 : "-") << " " << right << setw(5) << (round3 != 0 ? round3 : "-") << endl; cout << left << setw(25) << "Fours:" << setw(5) << die4 << " " << right << setw(5) << round4 << " " << left << setw(5) << (die4 == 4 ? die4 : "-") << " " << right << setw(5) << (round4 != 0 ? round4 : "-") << endl; cout << left << setw(25) << "Fives:" << setw(5) << die5 << " " << right << setw(5) << round5 << " " << left << setw(5) << (die5 == 5 ? die5 : "-") << " " << right << setw(5) << (round5 != 0 ? round5 : "-") << endl; cout << endl; cout << left << setw(25) << "Three of a Kind:" << right << setw(5) << round6 << " " << right << setw(5) << (round6 != 0 ? round6 : "-") << endl; cout << left << setw(25) << "Four of a Kind:" << right << setw(5) << round7 << " " << right << setw(5) << (round7 != 0 ? round7 : "-") << endl; cout << left << setw(25) << "Full House:" << right << setw(5) << round8 << " " << right << setw(5) << (round8 != 0 ? round8 : "-") << endl; cout << left << setw(25) << "Small Straight:" << right << setw(5) << round9 << " " << right << setw(5) << (round9 != 0 ? round9 : "-") << endl; cout << left << setw(25) << "Large Straight:" << right << setw(5) << round10 << " " << right << setw(5) << (round10 != 0 ? round10 : "-") << endl; cout << left << setw(25) << "Yahtzee:" << right << setw(5) << round11 << " " << right << setw(5) << (round11 != 0 ? round11 : "-") << endl; cout << left << setw(25) << "Chance:" << right << setw(5) << round12 << " " << right << setw(5) << (round12 != 0 ? round12 : "-") << endl; cout << endl; cout << left << setw(25) << "Total Score:" << right << setw(5) << totalScore << endl; } int main() { int die1, die2, die3, die4, die5; int round1 = 0, round2 = 0, round3 = 0, round4 = 0, round5 = 0, round6 = 0, round7 = 0, round8 = 0, round9 = 0, round10 = 0, round11 = 0, round12 = 0, round13 = 0; // Code to roll dice and update rounds goes here PrintScorecard(die1, die2, die3, die4, die5, round1, round2, round3, round4, round5, round6, round7, round8, round9, round10, round11, round12, round13); return 0; } ``` 输出示例: ``` Scorecard Ones: 3 3 - - Twos: 2 4 2 4 Threes: 5 - - 5 Fours: 1 - - - Fives: 6 10 - 10 Three of a Kind: - - 10 - Four of a Kind: - - - - Full House: - - - - Small Straight: - - 15 - Large Straight: - - - - Yahtzee: - - 0 - Chance: - 14 - - Total Score: 29 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值