zoukankan      html  css  js  c++  java
  • [华为机试练习题]35.找零钱

    题目

    描写叙述:

    我们知道人民币有1、2、5、10、20、50、100这几种面值。如今给你n(1≤n≤250)元。让你计算换成用上面这些面额表示且总数不超过100张,共同拥有几种。

    比方4元,能用4张1元、2张1元和1张2元、2张2元,三种表示方法。

    题目类别:

    循环  
    

    难度:

    0基础  
    

    执行时间限制:

    10Sec 
    

    内存限制:

    128MByte 
    

    阶段:

    入职前练习  
    

    输入:

    输入有多组,每组一行,为一个整合n。

    输入以0结束。

    输出:

    输出该面额有几种表示方法。
    

    例子输入:

    1
    4
    0
    

    例子输出:

    1
    3
    

    代码

    /*---------------------------------------
    *   日期:2015-07-02
    *   作者:SJF0115
    *   题目:找零钱
    *   来源:华为机试练习题
    -----------------------------------------*/
    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <list>
    using namespace std;
    
    int money[] = {1,2,5,10,20,50,100};
    
    // n 钱  money[index] <= n < money[index+1]
    int ChangeMoney(int n,int index){
        if(n == 1 || n == 0 || index == 0){
            return 1;
        }//if
        if(n < 0 || index < 0){
            return 0;
        }//if
        return ChangeMoney(n-money[index],index) + ChangeMoney(n,index-1);
    }
    
    int main(){
        int n;
        //freopen("C:\Users\Administrator\Desktop\c++.txt","r",stdin);
        int count = sizeof(money) / sizeof(money[0]);
        while(cin>>n && n != 0){
            int index = 0;
            for(int i = count - 1;i >= 0;--i){
                if(n >= money[i]){
                    index = i;
                    break;
                }//if
            }//for
            cout<<ChangeMoney(n,index)<<endl;
        }//while
        return 0;
    }
    
  • 相关阅读:
    mongoDb
    profile ,explain,
    header 里面的contenttype
    group by,distinct的使用(30万数据测试)
    ubuntu 12.04 mysql 错误 Errcode 13
    php curl,爬虫
    explain mysql
    php 文件的函数
    create User,grand,Load data file
    android 按钮点击测试
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7382897.html
Copyright © 2011-2022 走看看