码迷,mamicode.com
首页 > 其他好文 > 详细

E - 钱币兑换问题

时间:2017-07-03 12:21:31      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:sam   有一个   UI   cst   cstring   back   ane   span   输入   

 
 
 
在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法。请你编程序计算出共有多少种兑法

Input每行只有一个正整数N,N小于32768。

Output对应每个输入,输出兑换方法数。

Sample Input

2934
12553

Sample Output

718831
13137761


题解:首先都会想到循环,但是只要控制不好都会超时,我的想法是,将循环次数控制到最小;
首先大循环以三分为基础,剩下就是分配给二分和一分,因为有一分,所以肯定可以分完,
只要求出两分的有几种就可以,剩下的一分肯定能补齐;代码:
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
const int MS = 32768/2;


int main(){


 int n;
 while(cin>>n){
    int cnt=0;
    int MA=n/3;
    for(int i=0;i<=MA;i++){

        int MZ=(n-i*3);

            cnt+=(MZ/2+1);


    }

    cout<<cnt<<endl;
 }




}

 





#include<iostream>#include<cmath>#include<cstring>#include<string>#include<cstdio>usingnamespacestd; constint MS = 32768/2; int main(){ int n; while(cin>>n){ int cnt=0; int MA=n/3; for(int i=0;i<=MA;i++){ int MZ=(n-i*3); cnt+=(MZ/2+1); } cout<<cnt<<endl; } }

E - 钱币兑换问题

标签:sam   有一个   UI   cst   cstring   back   ane   span   输入   

原文地址:http://www.cnblogs.com/xuyibao/p/7109693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!