赞
踩
年会抽奖
思路:年会抽奖
import java.util.Scanner; public class Main{ public static float fac(int n){ if(n == 0) return 1; return n * fac(n - 1); } public static float count(int n) { if(n == 1){ return 0; } if(n == 2){ return 1; }else{ return (n - 1) * (count(n - 1) + count(n - 2)); } } public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextInt()){ int n = sc.nextInt(); float ret = count(n) / fac(n) * 100; System.out.println(String.format("%.2f", ret) + "%"); } } }
数字和为sum的方法数
思路:动态规划
数字和为sum的方法数
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); int m = cin.nextInt(); int arr[] = new int[n+1]; //代表你使用前i个数组组成j的最大组合数 long dp[][] = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { arr[i] = cin.nextInt(); } for (int i = 0; i < m; i++) { dp[0][i] = 0; } //注意,你的dp[0][0]一定要是1,否则会出错 for (int i = 0; i < n; i++) { dp[i][0] = 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (arr[i] <= j) { dp[i][j] = dp[i - 1][j] + dp[i - 1][j - arr[i]]; } else { dp[i][j] = dp[i - 1][j]; } } } System.out.println(dp[n][m]); } }
奇偶校验
思路:这一题里面将数字和字母统一看成是char类型的,所以数字3实际存储时为ASCII码中的‘3’,其十进制表示是51,转化为二进制表示就是0110011,取最高位为奇校验位,校验位为1,所以校验后的二进制数为10110011,字母同理。故本题只需将输入的字符减去‘\0’得到字符的十进制表示,再将其转化为七位二进制数加上一位校验位输出即可。
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String s = sc.next(); char[] arr = s.toCharArray(); for(char c : arr){ String s1 = Integer.toBinaryString(c); String s2 = String.format("%07d", Integer.parseInt(s1)); int count = 0; for(int i = 0; i < 7; i++){ if(s2.charAt(i) == '1') count++; } System.out.println(count % 2 == 0 ? "1" + s2 : "0" + s2); } } } }
大整数排序
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; import java.math.BigInteger; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ ArrayList<BigInteger> list = new ArrayList<BigInteger>(); int n = sc.nextInt(); for(int i = 0; i < n; i++){ list.add(new BigInteger(sc.next())); } Collections.sort(list); for(BigInteger i : list){ System.out.println(i); } } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String s = sc.nextLine(); String[] arr = s.split(" "); for(int i = arr.length - 1; i >= 0; i--){ if(i != 0){ System.out.print(arr[i] + " "); }else{ System.out.print(arr[i]); } } } } }
简单错误记录:
看下大佬的代码,膜拜。。
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); Map<String, Integer> map = new LinkedHashMap<>(); while(sc.hasNextLine()){ String s = sc.nextLine(); String[] arr = s. split(" "); String key = arr[0].substring(arr[0].lastIndexOf("\\") + 1) + " " + arr[1]; map.put(key, map.containsKey(key) ? map.get(key) + 1 : 1); } List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>(){ @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2){ return o2.getValue().compareTo(o1.getValue()); } }); for(int i = 0; i < 8; i++){ String[] arr1 = list.get(i).getKey().split(" "); if(arr1[0].length() > 16){ arr1[0] = arr1[0].substring(arr1[0].length() - 16); } System.out.println(arr1[0] + " " + arr1[1] + " " + list.get(i).getValue()); } } }
在霍格沃茨找零钱
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String[] arr = sc.nextLine().split(" "); String[] s1 = arr[0].split("\\."); String[] s2 = arr[1].split("\\."); int[] a1 = new int[3]; int[] a2 = new int[3]; for(int i = 0; i < 3; i++){ a1[i] = Integer.parseInt(s1[i]); a2[i] = Integer.parseInt(s2[i]); } int m1 = ((a1[0] * 17 + a1[1]) * 29) + a1[2]; int m2 = ((a2[0] * 17 + a2[1]) * 29) + a2[2]; int diff = m2 - m1; if(m2 < m1){ System.out.print("-"); diff = -diff; } System.out.println(diff / (17 * 29) + "." + (diff % (17 * 29)) / 29 + "." + (diff % (17 * 29)) % 29); } } }
思路:https://blog.csdn.net/huzhigenlaohu/article/details/51779365
import java.util.*;
public class Count2 {
public int countNumberOf2s(int n) {
int count = 0;
for(int i = 1; i <= n; i *= 10){
int a = n / i;
int b = n % i;
count += (a + 7) / 10 * i + ((a % 10 == 2) ? b + 1 : 0);
}
return count;
}
}
锤子剪刀布
链接:https://www.nowcoder.com/questionTerminal/79db907555c24b15a9c73f7f7d0e2471 来源:牛客网 public class B1008RockScissorsPaper { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); Model mA = new Model(); Model mB = new Model(); for (int i = 0; i < N; i++) { String a = sc.next(); String b = sc.next(); judge(a, b, mA, mB); } System.out.println(mA.win + " " + mA.tie + " " + mA.lose); System.out.println(mB.win + " " + mB.tie + " " + mB.lose); System.out.println(getMostGen(mA.map) + " " + getMostGen(mB.map)); } public static void judge(String a, String b, Model mA, Model mB) { if (a.equals("C")) { if (b.equals("C")) { mA.tie++; mB.tie++; } else if (b.equals("J")) { mA.win++; mB.lose++; mA.map.put("C", mA.map.get("C") + 1); } else { mA.lose++; mB.win++; mB.map.put("B", mA.map.get("B") + 1); } } else if (a.equals("J")) { if (b.equals("C")) { mA.lose++; mB.win++; mB.map.put("C", mA.map.get("C") + 1); } else if (b.equals("J")) { mA.tie++; mB.tie++; } else { mA.win++; mB.lose++; mA.map.put("J", mA.map.get("J") + 1); } } else { if (b.equals("C")) { mA.win++; mB.lose++; mA.map.put("B", mA.map.get("B") + 1); } else if (b.equals("J")) { mA.lose++; mB.win++; mB.map.put("J", mA.map.get("J") + 1); } else { mA.tie++; mB.tie++; } } } public static String getMostGen(Map<String, Integer> map) { if (map.get("C") >= map.get("J")) { if (map.get("C") > map.get("B")) { return "C"; } else { return "B"; } } else { if (map.get("J") > map.get("B")) { return "J"; } else { return "B"; } } } static class Model { int win; int tie; int lose; Map<String, Integer> map; Model() { map = new HashMap<>(); map.put("B", 0); map.put("C", 0); map.put("J", 0); } } }
import java.util.*; public class GoUpstairs { //Fibonacci sequence f(n) = f(n-1)+f(n-2)+f(n-3); And f(1)=1,f(2)=2,f(3)=4 public int countWays(int n) { int[] arr = {1, 2, 4}; if(n <= 0) return 0; else if(n <= 3) return arr[n - 1]; else{ for(int i = 4; i <= n; i++){ int tmp = ((arr[0] + arr[1]) % 1000000007 + arr[2]) % 1000000007; arr[0] = arr[1]; arr[1] = arr[2]; arr[2] = tmp; } } return arr[2]; } }
旧键盘:
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String s1 = sc.nextLine().toUpperCase(); String s2 = sc.nextLine().toUpperCase(); StringBuilder ret = new StringBuilder(); for(int i = 0; i < s1.length(); i++){ if(s2.indexOf(s1.charAt(i)) == -1 && ret.toString().indexOf(s1.charAt(i)) == -1) ret.append(s1.charAt(i)); } System.out.println(ret); } } }
import java.util.Scanner; import java.math.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ double[] arr = new double[6]; for(int i = 0; i < 6; i++){ arr[i] = (double)sc.nextInt(); } double r = Math.sqrt((arr[3] - arr[0]) * (arr[3] - arr[0]) + (arr[4] - arr[1]) * (arr[4] - arr[1]) + (arr[5] - arr[2]) * (arr[5] - arr[2])); double v = 4.0 / 3.0 * Math.acos(-1.0) * r * r * r; System.out.printf("%.3f ", r); System.out.printf("%.3f", v); System.out.println(); } } }
风口的猪,中国牛市
import java.math.*; public class Solution { /** * 计算你能获得的最大收益 * * @param prices Prices[i]即第i天的股价 * @return 整型 */ public static int getMaxDiff(int[] prices, int left, int right){ if(left >= right) return 0; int ret = 0; for(int i = left; i <= right; i++){ for(int j = left; j < i; j++){ ret = Math.max(ret, prices[i] - prices[j]); } } return ret; } public int calculateMax(int[] prices) { if(prices == null || prices.length < 2) return 0; int ret = 0; for(int i = 1; i < prices.length; i++){ ret = Math.max(ret, getMaxDiff(prices, 0, i) + getMaxDiff(prices, i + 1, prices.length - 1)); } return ret; } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ char[] arr = sc.nextLine().toCharArray(); int[] ret = new int[256]; for(char c : arr){ ret[c]++; } for(int i = 'A'; i <= 'Z'; i++){ System.out.println((char)i + ":" + ret[i]); } } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); ArrayList<Integer> list = new ArrayList<>(); for(int i = 0; i < n; i++){ int op = sc.nextInt(); int l = sc.nextInt(); if(op == 1){ list.add(l); }else{ int index = list.indexOf(l); list.remove(index); } if(list.size() < 3){ System.out.println("No"); }else{ Collections.sort(list, Collections.reverseOrder()); int max = list.get(0); int sum = 0; for(int j = 1; j < list.size(); j++){ sum += list.get(j); } if(sum > max){ System.out.println("Yes"); }else{ System.out.println("No"); } } } } } }
地下迷宫
import java.util.Iterator; import java.util.LinkedList; import java.util.Scanner; public class Main{ static int n, m, maxRemainEnergy = 0; static int[][] map; static String path = ""; static boolean flag = false; static LinkedList<String> list = new LinkedList<>(); public static void runMap(int x, int y, int energy){ if(x < 0 || y < 0 || x >= n || y >= m || energy < 0 || map[x][y] != 1) return; else{ list.offer("[" + x + "," + y + "]"); map[x][y] = 0; if(x == 0 && y == m - 1){ if(energy >= maxRemainEnergy){ maxRemainEnergy = energy; updatePath(); } map[x][y] = 1; list.removeLast(); flag = true; return; } runMap(x, y + 1, energy - 1); runMap(x + 1, y, energy); runMap(x - 1, y, energy - 3); runMap(x, y - 1, energy - 1); map[x][y] = 1; list.removeLast(); } } public static void updatePath(){ StringBuilder sb = new StringBuilder(); Iterator<String> iterator = list.iterator(); while(iterator.hasNext()){ sb.append(iterator.next() + ","); } if(sb.length() > 0){ sb.deleteCharAt(sb.length() - 1); } path = sb.toString(); } public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ n = sc.nextInt(); m = sc.nextInt(); int p = sc.nextInt(); map = new int[n][m]; for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ map[i][j] = sc.nextInt(); } } runMap(0, 0, p); if(!flag){ System.out.println("Can not escape!"); }else{ System.out.println(path); } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。