AB5 .点击消除
点击消除,类似于括号匹配a(b[b]a){c{d,这种,利用栈去消除,这样正好可以处理,假如相同就不进栈,同时还要出栈。注意我们这么搞完他是一个逆序,我们需要一个reverse()
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //aba,aab String a = scanner.nextLine(); char[] b = a.toCharArray(); Stack<Character> c = new Stack<>(); for (int i = 0; i < b.length; i++) { if (c.isEmpty()) { c.push(b[i]); } else { if (b[i] == c.peek()) { c.pop(); } else c.push(b[i]); } } StringBuffer ret = new StringBuffer(); if (c.isEmpty()){ System.out.println(0); }else { while (!c.isEmpty()) { ret.append(c.pop()); } ret.reverse(); System.out.println(ret.toString()); } } }
BC.149简写单词
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String a1=in.nextLine(); String ret=""; String []a=a1.split(" "); int b=a.length; for(int i=0;i<b;i++){ if(a[i].charAt(0)>='a'){ ret+=(char)(a[i].charAt(0)-32); }else{ ret+=a[i].charAt(0); } } System.out.println(ret); } }
牛客.除2!
我们把偶数都放在堆里面,注意⚠️也要看数字的范围10^9我们需要,
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); //逆序的大根堆 PriorityQueue<Integer> a = new PriorityQueue<>((c,d)->{ return d-c; }); ArrayList<Integer> ret=new ArrayList<>(); for (int i = 0; i < n; i++) { long x=scanner.nextLong(); if(x%2==0){ //偶数放到堆里面 a.add((int)x); }else //奇数放到ArrayList里面 ret.add((int) x); } long count = 0; while (k > 0 && !a.isEmpty()) { int t = a.poll(); if(t%2==0){ a.add(t / 2); k--;} //有可能10/2=5 然后把5放进来之后,5并没有修改 else{ ret.add(t); } } for(int i=0;i<ret.size();i++) count += ret.get(i); while (!a.isEmpty()){ count+=a.poll(); } System.out.println(count); } }
牛客.Fibonacci数列
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); //假如不用集合也可以,但是数组是没有集合好的,毕竟数组的长度你是无法确定的 ArrayList<Integer>dp = new ArrayList<>(); if (a == 1) { System.out.println(0); } else { dp.add(0); dp.add(1); int m = 0; int n = 1; int k = 1; int count = Integer.MAX_VALUE; int j = 2; while (dp.get(j - 1) < a) { dp.add(j, dp.get(j - 1) + dp.get(j - 2)); j++; } for (int i = 2; i < dp.size(); i++) { m = dp.get(i - 2); n = dp.get(i - 1); k = dp.get(i); if (a < k && a > n) { count = Math.min(count, Math.min(Math.abs(a - k), Math.abs(a - n))); } else if (a < n && a > m) { count = Math.min(count, Math.min(Math.abs(a - m), Math.abs(a - n))); } } System.out.println(count); } } }