您的位置 首页 编程语言

Write a function named countRepresentations that returns the number of ways that an amount of money in rupees can be represented as rupee notes.

 

美国面试题:

Write a function named countRepresentations that returns the number of ways that an amount of money in rupees can be represented as rupee notes. For this problem we only use rupee notes in denominations of 1, 2, 5, 10 and 20 rupee notes.
The signature of the function is:
int countRepresentations(int numRupees)
For example, countRepresentations(12) should return 15 because 12 rupees can be represented in the following 15 ways.

 

1. 12 one rupee notes
2. 1 two rupee note plus 10 one rupee notes
3. 2 two rupee notes plus 8 one rupee notes
4. 3 two rupee notes plus 6 one rupee notes
5. 4 two rupee notes plus 4 one rupee notes
6. 5 two rupee notes plus 2 one rupee notes
7. 6 two rupee notes
8. 1 five rupee note plus 7 one rupee notes
9. 1 five rupee note, 1 two rupee note and 5 one rupee notes 10. 1 five rupee note, 2 two rupee notes and 3 one rupee notes 11. 1 five rupee note, 3 two notes and 1 one rupee note
12. 2 five rupee notes and 2 one rupee notes
13. 2 five rupee notes and 1 two rupee note
14. 1 ten rupee note and 2 one rupee notes
15. 1 ten rupee note and 1 two rupee note

 

 

 

 

java 实现代码:

 

public class Representations {

    public static void main(String[] args) {

        System.out.println(countRepresentations(12));
    }


    // 卢布 1,2,5,10,20

    public static int countRepresentations(int numRupees){

        int count =0;

//        1*x + 2*y + 5*z+10*s

        for(int rupee20=0;rupee20<= numRupees/20; rupee20++){
            for (int rupee10=0;rupee10<=numRupees/10; rupee10++){
                for (int rupee5=0;rupee5<=numRupees/5; rupee5++){
                    for(int rupee2=0;rupee2<=numRupees/2; rupee2++){
                        for (int rupee1=0;rupee1<=numRupees/1; rupee1++){
                            if(rupee20*20 + 10*rupee10+5*rupee5+2*rupee2+ 1* rupee1 == numRupees){
//                                System.out.println( "rupee20="+rupee20);
//                                System.out.println( "rupee10="+rupee10);
//                                System.out.println( "rupee5="+rupee5);


                                count ++;
                            }
                        }
                    }
                }
            }
        }

        return count;



    }
}

猫叔总结了 适合新手操作的副业 《淘宝虚拟产品月入2万的 6个 细分类目》的电子书 仅供参考

如果你对虚拟产品比较感兴趣,可以点击:

淘宝卖什么虚拟产品赚钱(月入2万+)

hadoopall

关于花猫大叔短视频创业 作者: hadoopall

热门文章

发表评论

电子邮件地址不会被公开。