mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

方法:设1分的为x个。2分的为y个,则x+2y+5(n-x-y)==m, 得5n-m==4x+3y;

注意:x+y<=n;

java代码:

import java.util.Scanner;

public class Main {
    static int t, n, m;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        t = sc.nextInt();
        int count = 0;
        int sum = 0;
        while (t-- > 0) {
            n = sc.nextInt();
            m = sc.nextInt();
            count = 0;
            sum = 5 * n - m;
            for (int i = 0; i <= n; i++) {
                for (int j = 0; j <= n; j++) {
                    if (sum == 4 * i + 3 * j && i + j <= n) {
                        count++;
                    }
                }
            }
            System.out.println(count);
        }
    }

}




统计硬币

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5281    Accepted Submission(s): 3646


Problem Description
如果一堆由1分、2分、5分组成的n个硬币总面值为m分,求一共同拥有多少种可能的组合方式(某种面值的硬币能够数量能够为0)。
 

Input
输入数据第一行有一个正整数T,表示有T组測试数据。
接下来的T行。每行有两个数n,m。n和m的含义同上。


 

Output
对于每组測试数据,请输出可能的组合方式数;
每组输出占一行。
 

Sample Input
2 3 5 4 8
 

Sample Output
1 2
 

Author
lemon
 

Source
 

Recommend
yifenfei   |   We have carefully selected several similar problems for you:  1085 2152 1171 1398 2082 


posted on 2017-05-23 21:51  mthoutai  阅读(298)  评论(0编辑  收藏  举报