1. How many ways are there to exchange for change?
enter:
The first line asks the number t. (that is, there are t groups of data)
The second line has two numbers n and k, where n is the number of denominations of the change, and k is the change to be exchanged.
The third row has n numbers, representing n face values
E.g:
1
3 5
1 2 5
output
4
That is, there are 4 ways to combine loose change: 11111, 1112, 122, 5
That is to say, if you want Niu Niu to exchange 1 piece of money, the bank has 3 different denominations of change to choose from. The denomination of the money to be exchanged is 5, and the three denominations are distributed as 1, 2 and 5.
So the code is as follows, just for reference:
import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { int t = sc.nextInt(); Map<Integer, java.util.List<Integer>> map = new HashMap<Integer, java.util.List<Integer>>(); for (int i = 0; i < t; i++) { java.util.List<Integer> mylist = new ArrayList<>(); int n = sc.nextInt(); mylist.add(n); int k = sc.nextInt(); mylist.add(k); for (int s = 0; s < n; s++) { int temp = sc.nextInt(); mylist.add(temp); } map.put(i, mylist); } Collection<java.util.List<Integer>> value = map.values(); Iterator<java.util.List<Integer>> iter = value.iterator(); while (iter.hasNext()) { java.util.List<Integer> list = iter.next(); find(list); } } sc.close(); } private static void find(java.util.List<Integer> list) { // TODO Auto-generated method stub int len = list.size(); int[] num = new int[len]; for (int i = 0; i < list.size(); i++) { num[i] = list.get(i); } int n = num[0]; int k = num[1]; int[] values = new int[n]; for (int i = 0; i < values.length; i++) { values[i] = num[i + 2]; } solution(values, k); } private static void solution(int[] value, int k) { if (value == null || value.length == 0 || k < 0) { System.out.println(0); } int[] dp = new int[k + 1]; for (int j = 0; value[0] * j <= k; j++) { dp[value[0] * j] = 1; } for (int i = 1; i < value.length; i++) { for (int j = 1; j <= k; j++) { dp[j] += j - value[i] >= 0 ? dp[j - value[i]] : 0; } } System.out.println(dp[k] % 100000007); } }
1. Niu Niu sells grass
Niuniu’s neighbors have surrounded the city in a circle. With two adjacent households, Niuniu can only be sold to one family. That is, when Niuniu sells to the Nth neighbor, it cannot be sold to the N-1th and N+1th households. Neighbor. How much can Niu Niu sell at most?
enter:
The first line is the query number t
The second row is Niuniu's neighbor number n
The third row is the amount of grass that Niu Niu's neighbors need
E.g:
2
4
8 9 2 8
2
10 100
That is, there are two sets of test data. The first set of data has 4 neighbors, and the required number of grasses is 8, 9, 2, and 8; the second set of data has 2 neighbors, and the required number of grasses is 10, 100.
output:
17
100
The first group of neighbors, Niuniu sold 17, and the second group of neighbors, Niuniu, sold 100;
code show as below:
import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Scanner; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int t = sc.nextInt(); Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>(); for (int i = 0; i < t; i++) { List<Integer> templist = new ArrayList<>(); int n = sc.nextInt(); templist.add(n); for (int j = 0; j < n; j++) { int temp = sc.nextInt(); templist.add(temp); } map.put(i, templist); } Collection<List<Integer>> values = map.values(); Iterator<List<Integer>> iter = values.iterator(); while (iter.hasNext()) { List<Integer> goallist = iter.next(); find(goallist); } } sc.close(); } private static void find(List<Integer> goallist) { // TODO Auto-generated method stub int len = goallist.size(); int[] num = new int[len]; for (int i = 0; i < goallist.size(); i++) { num[i] = goallist.get(i); } int n = num[0]; int[] value = new int[n]; for (int i = 0; i < value.length; i++) { value[i] = num[i+1]; } solution(n,value); } private static void solution(int n, int[] value) { // TODO Auto-generated method stub if (n==1||value.length==1) { System.out.println(value[0]); }else{ int max = 0; for (int i = 0; i < value.length; i++) { LinkedList<Integer> link = new LinkedList<>(); for (int j = i; j < value.length; j++) { link.add(value[j]); } for (int j = 0; j < i; j++) { link.add(value[j]); } int count = 0; int k = value.length/2; while (k>0) { int temp = link.poll(); count = count+temp; link.poll(); k = k - 1; } if (count>=max) { max = count; } } System.out.println(max); } } }(The above code has passed the test case in the title, but it has not been tested online. In addition, I hope that all the great gods and Daniel will actively criticize and correct the code, and optimize the code!)