1.有兩個用字符串表示的很是大的大整數,算出他們的乘積,也是用字符串表示。不能用系統自帶的大整數類型。java
解析:大數相乘,模擬一下。ui
package pdd.nt; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Created by cuijunyong on 2018/3/20. */ public class Mul { public static void main(String[] args) { Scanner cin = new Scanner(System.in); String a = cin.next(); String b = cin.next(); String c = mul(a, b); System.out.println(c); } private static String mul(String x, String y) { char[] a = x.toCharArray(); char[] b = y.toCharArray(); List<Long> c = new ArrayList(); for(int i = a.length - 1; i >= 0; i--){ long v = 0; for(int j = b.length - 1; j >= 0; j--){ v += (a[i] - '0') * (b[j] - '0'); int pos = b.length - 1 - j + (a.length - 1 - i); if(c.size() <= pos){ c.add(v % 10); }else{ v += c.get(pos); c.set(pos, v % 10); } v /= 10; } while (v > 0){ c.add(v % 10); v /= 10; } } String str = ""; for(int i = c.size() - 1; i >= 0; i-- ){ str += c.get(i); } return str; } }
2.六一兒童節,老師帶了不少好吃的巧克力到幼兒園。每塊巧克力j的重量爲w[j],對於每一個小朋友i,當他分到的巧克力大小達到h[i] (即w[j]>=h[i]),他纔會上去表演節目。老師的目標是將巧克力分發給孩子們,使得最多的小孩上臺表演。能夠保證每一個w[i]> 0且不能將多塊巧克力分給一個孩子或將一塊分給多個孩子。spa
解析:一看就先想到了二分匹配,看了解析看到他們用的排序模擬也作出來了。code
package pdd.nt; import java.util.Scanner; /** * Created by cuijunyong on 2018/3/20. */ public class day61 { private static Scanner cin = new Scanner(System.in); private static int n, m; private static int[] h,w; private static int[][] mp; private static int[] d; private static int[] v; public static void main(String[] args) { init(); int ans = 0; for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ v[j] = 0; } if(dfs(i)){ ans ++; } } System.out.println(ans); } private static boolean dfs(int x){ for(int i = 0; i < m; i++){ if(v[i] == 0 && mp[x][i] == 1){ v[i] = 1; if(d[i] == -1 || dfs(d[i])){ d[i] = x; return true; } } } return false; } private static void init(){ n = cin.nextInt(); h = new int[n]; for(int i = 0; i < n; i++){ h[i] = cin.nextInt(); } m = cin.nextInt(); w = new int[m]; for(int i = 0; i < m; i++){ w[i] = cin.nextInt(); } d = new int[m]; for(int i = 0; i < m; i++){ d[i] = -1; } v = new int[m]; for(int i = 0; i < m; i++){ v[i] = 0; } mp = new int[n][m]; for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(h[i] <= w[j]){ mp[i][j] = 1; } } } } }
3.將1+3+3+4變成1+3*2+4blog
package pdd.cz; /** * Created by cuijunyong on 2018/3/20. */ import java.util.*; public class A { private static Map<Long, Integer> map = new HashMap<Long, Integer>(); private static Set<Long> set = new HashSet<Long>(); public static void main(String[] args) { Scanner cin = new Scanner(System.in); String str = cin.next(); init(str); } private static void init(String str) { map.clear(); set.clear(); Long x = 0L; Long sum = 0L; str += "+"; for(int i = 0; i < str.length(); i++){ char c = str.charAt(i); if(c >= '0' && c <= '9'){ x = x * 10 + c - '0'; }else{ Integer v = map.get(x); if(v == null){ v = 0; } v++; map.put(x, v); set.add(x); sum += x; x = 0L; } } Object[] objects = set.toArray(); long[] arr = new long[objects.length]; for(int i = 0; i < objects.length; i++){ arr[i] = (Long)objects[i]; } Arrays.sort(arr); String ans = ""; for(int i = 0; i < arr.length; i++){ Long next = arr[i]; if(i != 0){ ans += "+"; } ans += next + "*" + map.get(next); } System.out.println(ans); System.out.println(sum); } }
4.坐出租車,每輛最多坐4我的,要求一隊不能分開。排序
package pdd.cz; /** * Created by cuijunyong on 2018/3/20. */ /* 6 1 2 4 3 3 2 */ import java.util.*; public class B { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int N; N = cin.nextInt(); int[] p = new int[N]; int[] num = new int[5]; for(int i = 0; i < 5; i++){ num[i] = 0; } for(int i = 0; i < N; i++){ p[i] = cin.nextInt(); num[p[i]]++; } int ans; ans = num[4] + (num[2] + 1)/2; if(num[3] > num[1]){ ans += num[1] + num[3] - num[1]; }else{ ans += num[3] + (num[1] - num[3] + 3)/4; } if(num[2] % 2 == 1 && (num[1] - num[3])%4 <= 2 && (num[1] - num[3])%4 > 0){ ans --; } System.out.println(ans); } }