第四、五周集训总结

目录

题目总结:

1. 母牛的故事

AC代码 - 非正解但AC

2. 超级楼梯

AC代码

3. Coin Change

大致题意:

AC代码

小结:


题目总结:

1. 母牛的故事

有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

Input

输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。

Output

对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。

Sample Input

2
4
5
0

Sample Output 

2
4
6

这应该是一道动态规划的题目,但是刚学还不太会用就用了一种很奇怪的方法做了。没做的题还有很多,先标记一下,等到有空了在补一个动态规划的做法。

AC代码 - 非正解但AC

#include <iostream>
#define int long long
using namespace std; 
 
int ans(int n){
	int i=0;
	int sum=0;
	int a[5]={0,0,0,0,1}; //这个数组用来记录每个岁数的牛的数量
	for(i=2;i<=n;i++){
		a[4]+=a[3];
		a[3]=a[2];
		a[2]=a[1];
		a[1]=a[4];
	}
	
	for(int i=1;i<=4;i++){
		sum+=a[i];
	}
	return sum;
}

int n;

signed main()
{
	while(1){
		cin>>n;
		if(!n) break;
		cout<<ans(n)<<endl;
	}
	return 0;
}

2. 超级楼梯

有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?

Input

输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。

Output

对于每个测试实例,请输出不同走法的数量

Sample Input

2
2
3

Sample Output 

1
2

刚开始学习的蒟蒻只会做简单的动态规划题。

AC代码

#include <bits/stdc++.h> 
#define int long long
using namespace std;

int t;
int a[50];

signed main(){
	cin>>t;
	a[1]=0; //先把前几阶楼梯的走法写出来
	a[2]=1;
	a[3]=2;
	while(t--){
		int n;
		cin>>n;
		for(int i=4;i<=n;i++){
			a[i]=a[i-1]+a[i-2]; //根据题意,每次只能走一步或两步。
		}    //所以 n 的台阶可以看作是 (n-1) 的台阶的走法加上 (n-2) 台阶的走法
		cout<<a[n]<<endl;
	}
    return 0;
}

 

3. Coin Change

Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.

For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.

Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 100 coins.

Input

The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.

Output

For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.

大致题意:

有50,25,10,5,1价值的硬币,给你一个数X,问你有几种不同的方式让硬币的总价值等于X。

AC代码

#include<bits/stdc++.h>
#define int long long
using namespace std;

int n;
int a[5]={1,5,10,25,50};

signed main()
{	
	while(cin>>n){
		int dp[500][500]={1};
		for(int i=0;i<5;i++){
			for(int j=1;j<=100;j++){
				for(int k=a[i];k<=n;k++){
					dp[j][k]+=dp[j-1][k-a[i]];
				}
			}
		}
		
		int ans=0;
		for(int i=0;i<=n;i++){
			ans+=dp[i][n];
		}
		cout<<ans<<endl;
	}
	return 0;
 } 

小结:

8月15日Codeforces Round 169 (Rated for Div. 2) 给我打崩了,难题不会我不怕,但是简单题WA了我受不了,打的比赛不多这是我第一次掉分的比赛记录一下,奔溃qwq 。。。加油吧 ! n_n !

  • 23
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值