zoukankan      html  css  js  c++  java
  • HDUOJ----Coin Change

    Coin Change

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 10590    Accepted Submission(s): 3535


    Problem Description
    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.
     
    Sample Input
    11
    26
     
    Sample Output
    4
    13
     
    Author
    Lily
     
    Source
     

    思路: 此题可以采取dfs,但是用分治法还是可以的,优化一下可以达到15ms......

    在此贴出代码:

     1 #include<iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     int n,count;
     6     int j,k,m,g,l;
     7     while(cin>>n)
     8     {
     9         count=0;
    10         for( j=0;j<=n/50;j++)   //50
    11         {  
    12             k=m=g=l=0;
    13             if(n==j*50+k*25+m*10+g*5+l)
    14                         {
    15                            count++;
    16                            break;
    17                         }
    18                         else
    19                             if(n<j*50+k*25+m*10+g*5+l)
    20                                 break;
    21 
    22             for( k=0;k<=n/25;k++)   //25
    23             {
    24                 m=g=l=0;
    25                 if(n==j*50+k*25+m*10+g*5+l)
    26                         {
    27                            count++;
    28                            break;
    29                         }
    30                         else
    31                             if(n<j*50+k*25+m*10+g*5+l)
    32                                 break;
    33                 for( m=0;m<=n/10;m++)   //10
    34                 {
    35                     g=l=0;
    36                     if(n==j*50+k*25+m*10+g*5+l)
    37                         {
    38                            count++;
    39                            break;
    40                         }
    41                         else
    42                             if(n<j*50+k*25+m*10+g*5+l)
    43                                 break;
    44                     for( g=0;g<=n/5;g++)  //5
    45                     {
    46                         l=0;
    47                         if(n==j*50+k*25+m*10+g*5+l)
    48                         {
    49                            count++;
    50                            break;
    51                         }
    52                         else
    53                             if(n<j*50+k*25+m*10+g*5+l)
    54                                 break;
    55                       for( l=0;l<=100-j-k-m-g;l++)  //1
    56                       {
    57                         if(n==j*50+k*25+m*10+g*5+l)
    58                         {
    59                            count++;
    60                            break;
    61                         }
    62                         else
    63                             if(n<j*50+k*25+m*10+g*5+l)
    64                                 break;
    65                        }
    66                     }
    67                 }
    68             }
    69         }
    70     
    71     cout<<count<<endl;
    72     }
    73 return 0;
    74 }
    View Code
     
  • 相关阅读:
    (AIDE)Android Eclipse JNI 调用 .so文件加载问题
    VMware10.06精简版安装后台运行
    RAID磁盘阵列笔记
    高科固定电话机铃声
    嵌入式Linux驱动学习之路(十八)LCD驱动
    嵌入式Linux驱动学习之路(十七)驱动程序分层分离概念-平台设备驱动
    嵌入式Linux驱动学习之路(十六)输入子系统
    嵌入式Linux驱动学习之路(十五)按键驱动-定时器防抖
    嵌入式Linux驱动学习之路(十四)按键驱动-同步、互斥、阻塞
    嵌入式Linux驱动学习之路(十三)按键驱动-异步通知
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3205018.html
Copyright © 2011-2022 走看看