1.有两个用字符串表示的非常大的大整数,算出他们的乘积,也是用字符串表示。不能用系统自带的大整数类型。
解析:大数相乘,模拟一下。
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(); Listc = 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且不能将多块巧克力分给一个孩子或将一块分给多个孩子。
解析:一看就先想到了二分匹配,看了解析看到他们用的排序模拟也做出来了。
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+4
package pdd.cz;/** * Created by cuijunyong on 2018/3/20. */import java.util.*;public class A { private static Mapmap = new HashMap (); private static Set set = new HashSet (); 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. *//*61 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); }}