8.11字节跳动
牛牛闹钟,原题
第三题,左右遍历两次
4
1 3 2 1
700
public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } int[] dp=new int[n]; int left=0,right=n-1; int result=0; dp[0]=100; dp[n-1]=100; for(int i=1;i<n;i++) { if(a[i]>a[i-1]) { dp[i]=dp[i-1]+100; } else dp[i]=100; } for(int i=n-2;i>=0;i--) { if(a[i]>a[i+1]) { dp[i]=dp[i+1]+100; } } for(int i=0;i<n;i++) { result+=dp[i]; } System.out.println(result); }
输入
7 4
1110100110
输出
1001010
运算异或
1001010
1001010
1001010
1001010
----------------
1110100110
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); int[] sToInt = new int[s.length()]; for(int i=0; i<s.length(); i++) { sToInt[i] = Integer.parseInt(String.valueOf(s.charAt(i))); } int[] result = new int[n]; result[0] = sToInt[0]; result[n-1] = sToInt[s.length()-1]; for(int i=1; i<k&&i<n; i++) { result[i] = sToInt[i] ^ sToInt[i-1]; } int first=1; if(n>1) first=result[1]; for(int j=1; j<k&&j<n; j++) { first ^= result[j]; } int temp; for(int i=k; i<n-1; i++) { temp = sToInt[i]; result[i] = temp^first; first=first^result[i]^result[i-k+1]; } for(int i=0; i<result.length; i++) { System.out.print(result[i]); } System.out.println(); }
本文来自博客园,作者:LeeJuly,转载请注明原文链接:https://www.cnblogs.com/peterleee/p/11336853.html

浙公网安备 33010602011771号