考完期末,作者又回来更新了!!
题目描述:
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
有问题可以在评论区讨论!!