目录
题目一:最少硬币问题
问题描述
Mr_he 因讨厌马云而彻底放弃网购,他的日常用品都要到商场去购买,而且必须付现金。但是现 金购买,经常会遇到找零的问题,那么现在请你帮助他解决这样一个问题: 现在 Mr_he 手上有 n 种不同面值的硬币,每种硬币有无限多个。为了方便购物,他希望带尽量 少的硬币,但是要能组合出 1 到 m 之间的任意值。
输入格式
第一行为两个整数:m 和 n,他们的意义如题目描述。
接下来的 n 行,每行一个整数,第 i+1 行的整数表示第 i 种硬币的面值
输出格式
最少需要携带的硬币数量,如果无解则输出-1。
样例输入
20 4
1
2
5
10
样例输出
5
数据范围
50%的数据:1<=n<=10, 1<=m<=10^3;
100%的数据:1<=n<=100,1<=m<=10^9;
此题看上去像背包,然而范围太大,没有什么好方法可以处理。
没什么好方法的时候就应该考虑贪心。
考虑1−i1−i已经构出,那么再加一枚什么面值的硬币最优,显然选一枚<=sum+1<=sum+1且面值最大的即可。面值最大保证了硬币数最小。sumsum表示硬币面值和。
事实上,这个贪心可以从F[i]=F[i−A[k]]+1F[i]=F[i−A[k]]+1看出。F[i]F[i]必然是单调增的,因此贪心即可。F[i]表示处理完1−i的最少硬币数F[i]表示处理完1−i的最少硬币数
package writtenExam;
import java.util.Arrays;
import java.util.Scanner;
public class TencentSolution2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m= scanner.nextInt();
int n = scanner.nextInt();
int arr[]=new int [n];
for (int i =0;i<n;i++) {
arr[i] = scanner.nextInt();
}
Arrays.sort(arr);
//quickSort(arr, 0, arr.length-1); 验证有效
System.out.println(LestCion(m, arr));
}
public static int LestCion(int m,int coins[]) {
if (m<=0) return -1;
if (coins[0] !=1 ) return -1;
int currentCoinSum=0;int i=0;int res=0;
while(currentCoinSum < m) {
for (;i<coins.length;i++) {
//新加入得coin面值最好<=当前有的所有硬币面值纸盒currentCoinSum,并且是最大面值
//这里coin是是按照面值升序排好序的。假如大于currentCoinSum+1
if (coins[i] > currentCoinSum+1)
break;
}
currentCoinSum=currentCoinSum+coins[i-1];
res++;
}
return res;
}
//对数组进行快速排序
public static void quickSort(int arr[],int left,int right) {
if (left>=right)
return;
int mid=partition(arr, left, right);
quickSort(arr, left, mid-1);
quickSort(arr, mid+1, right);
}
public static int partition(int arr[],int left ,int right) {
int pivot=arr[left];
int pivotIndex = left;
while(left<right) {
while(left<right && arr[right]>=pivot)
right--;
arr[left] = arr[right];
while(left<right && arr[left]<pivot)
left++;
arr[right] = arr[left];
}
arr[left] = pivot;
pivotIndex = left;
return pivotIndex;
}
}
题目二: 字符串消除
给一串字符串,不能够使0和1相邻,如果相邻就消除,输出最终剩下的字符串长度:(详细题目找不到了,下面代码通过率100)
package writtenExam;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class TencentSolution1 {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
String input=scanner.next();
System.out.println(dele01S(input));
System.out.println(dele01S(input).length());
System.out.println(math01(input));
scanner.close();
}
public static boolean isOver( String string) {
boolean flag=true;
Map<Character, Integer> hmMap=new HashMap<>();
for(int i=0;i< string.length();i++) {
if (!hmMap.containsKey(string.charAt(i))) {
hmMap.put(string.charAt(i), 1);
}else {
hmMap.put(string.charAt(i), hmMap.get(string.charAt(i))+1);
}
}
if (hmMap.containsKey('0') && hmMap.containsKey('1')) {
flag=false;
}
return flag;
}
public static String dele01S(String s) {
if (s == "" || s == null)
return s;
if (isOver(s.replaceAll("01", "").replaceAll("10", ""))) {
return s.replaceAll("01", "").replaceAll("10", "");
}else {
return dele01S(s.replaceAll("01", "").replaceAll("10", ""));
}
}
public static int math01(String string) {
Map<Character, Integer> hmMap=new HashMap<>();
for(int i=0;i< string.length();i++) {
if (!hmMap.containsKey(string.charAt(i))) {
hmMap.put(string.charAt(i), 1);
}else {
hmMap.put(string.charAt(i), hmMap.get(string.charAt(i))+1);
}
}
int station = ( hmMap.get('1') > hmMap.get('0') )?1:0;
if (station==1) {
for (int i=0;i< hmMap.get('1')-hmMap.get('0');i++) { System.out.print("1"); }
}
if (station==0) {
for (int i=0;i< hmMap.get('0')-hmMap.get('1');i++) { System.out.print("0"); }
}
return Math.max(hmMap.get('1'), hmMap.get('0'))-Math.min(hmMap.get('1'), hmMap.get('0'));
}
}