https://www.tuhd.top/2017/08/02/2017-08-02-%E6%8B%BC%E5%A4%9A%E5%A4%9A%E7%AC%94%E8%AF%95/
模拟乘法, 大数乘法
import java.util.Scanner;/** * Created by thd on 2017/8/1 */public class Main2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1 = sc.next(); String s2 = sc.next(); int[] a = new int[s1.length()]; int[] b = new int[s2.length()]; int[] c = new int[s1.length() + s2.length()]; for (int i = 0; i < s1.length(); i++) { a[s1.length() - 1- i] = s1.charAt(i) - '0'; } for (int i = 0; i < s2.length(); i++) { b[s2.length() - 1 - i] = s2.charAt(i) - '0'; } for (int i = 0; i < s1.length(); i++) { for (int j = 0; j < s2.length(); j++) { c[i+j]+=a[i]*b[j]; } } for (int i = 0; i < c.length - 1; i++) { c[i+1]+=c[i]/10; c[i]=c[i]%10; } StringBuffer sb = new StringBuffer(); boolean flag = true; for (int i = c.length - 1; i >= 0; i--) { if (c[i] == 0 && flag) { continue; } else { flag = false; } sb.append(c[i]); } if (flag) System.out.println(0); else System.out.println(sb.toString()); }}复制代码
探险
import java.util.*;/** * Created by thd on 2017/8/1 */import java.util.*; public class Main { static class Node{ int x; int y; int key; int step; public Node(int x,int y,int key,int step){ this.x=x; this.y=y; this.key=key; this.step=step; } } public static void main(String[] args){ Scanner in=new Scanner(System.in); int N=in.nextInt(); int M=in.nextInt(); in.nextLine(); char[][] G=new char[N][M]; for(int i=0;iqueue=new LinkedList<>(); int[][][] mp=new int[101][101][1025]; int[][] next={ {-1,0},{ 0,-1},{ 1,0},{ 0,1}}; queue.offer(new Node(si,sj,0,0)); while(!queue.isEmpty()){ Node node=queue.poll(); for(int i=0;i<4;i++){ int x=node.x+next[i][0]; int y=node.y+next[i][1]; int key=node.key; if(x<0||x>=N||y<0||y>=M||G[x][y]=='0') continue; // 如果走到了出口 直接return else if(G[x][y]=='3') return node.step+1; // 如果走到了有钥匙的位置,捡钥匙 else if(G[x][y]<='z'&&G[x][y]>='a') key=key|(1<<(G[x][y]-'a')); // 如果走到了门的位置,判断是否已经有这个门的钥匙,如果没有就没法走 else if(G[x][y]<='Z'&&G[x][y]>='A'&&(key&(1<<(G[x][y]-'A')))==0) continue; // 将走过的位置标记为1 if(mp[x][y][key]==0){ mp[x][y][key]=1; queue.offer(new Node(x,y,key,node.step+1)); } } } return -1; } }复制代码
分巧克力
public class Main3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] h = new int[n]; for (int i = 0; i < n; i++) { h[i] = sc.nextInt(); } int m = sc.nextInt(); int[] w = new int[m]; for (int i = 0; i < m; i++) { w[i] = sc.nextInt(); } Arrays.sort(h); Arrays.sort(w); int res = 0; int j = 0; for (int i = 0; i < h.length; i++) { if (w[j++] >= h[i]) { res++; } else i--; if (j == w.length) break; } System.out.println(res); }}复制代码
找数组中3个数字的最大乘积
import java.util.Scanner;/** * Created by thd on 2017/8/1 */public class Main1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); scanner.nextLine(); long max1 = 1; long max2 = 1; long max3 = 1; long min1 = 1; long min2 = 1; for (int i = 0; i < n; i++) { long num = scanner.nextLong(); if (num > 0) { if (num > max1) { max3 = max2; max2 = max1; max1 = num; continue; } else if (num > max2) { max3 = max2; max2 = num; } else if (num > max3) { max3 = num; } } else { if (num < min1) { min2 = min1; min1 = num; } else if (num < min2) { min2 = num; } } } if (min1 != 1 || min2 != 1) { if (min1 * min2 < max2 * max3) { System.out.println(max2 * max3 * max1); } else { System.out.println(min1 * min2 * max1); } } else { System.out.println(max2 * max3 * max1); } } }}复制代码