A.Medium Number
题目:
Given three distinct integers a, b, and c, find the medium number between all of them.
The medium number is the number that is neither the minimum nor the maximum of the given three numbers.
For example, the median of 5,2,6 is 5, since the minimum is 2 and the maximum is 6.
题目的意思是说有三个数,从中选出那个比最大值小,比最小值大的那个数。
思路:
每一组都是三个数,只需要排序然后输出第二个数就可以了。
代码:
点击查看代码
import java.util.Scanner;
public class MainA {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] arr = new int[n][3];
for(int i=0;i<n;i++) {
for(int j = 0;j<3;j++) {
arr[i][j] = scanner.nextInt();
}
}
for(int i = 0;i < n;i++) {
sort(arr[i]);
}
for(int i = 0;i < n;i++) {
System.out.println(arr[i][1]);
}
scanner.close();
}
public static void sort(int[] arr) {
for(int i = 0;i < arr.length-1;i++) {
for(int j = 0;j < arr.length-1-i;j++) {
if(arr[j]>arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
B.Yes-Yes?
题目:
You talked to Polycarp and asked him a question. You know that when he wants to answer "yes", he repeats Yes many times in a row.
Because of the noise, you only heard part of the answer — some substring of it. That is, if he answered YesYes, then you could hear esY, YesYes, sYes, e, but you couldn't Yess, YES or se.
Determine if it is true that the given string s is a substring of YesYesYes... (Yes repeated many times in a row).
题目的意思是说根据输入的字符串判断YES和NO,如果是YesYesYes...中的片段就是YES,否则就是NO
思路:
只需要判断输入的字符串是否包含于50个连续的YES就行
代码:
点击查看代码
import java.util.*;
public class MainB {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String[] str = new String[n];
String str1 = "YesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYes"
+ "YesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYes";
for(int i = 0;i < str.length;i++) {
str[i] = scanner.next();
}
for(int k = 0;k < n;k++) {
if(str1.contains(str[k])) {
System.out.println("YES");
continue;
}else {
System.out.println("NO");
}
}
scanner.close();
}
}
C.Atilla's Favorite Problem
题目:
In order to write a string, Atilla needs to first learn all letters that are contained in the string.
Atilla needs to write a message which can be represented as a string s. He asks you what is the minimum alphabet size required so that one can write this message.
The alphabet of size x (1≤x≤26) contains only the first x Latin letters. For example an alphabet of size 4 contains only the characters a, b, c and d.
题目的意思就是说:给出一个字符串,找到它里面序列最大的字母的序号
思路:
这道题直接对号入座就行,从z开始倒过来检验。
代码:
点击查看代码
import java.util.*;
public class MainC {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
String[] str = new String[n];
for(int i = 0;i<n;i++) {
int num = scanner.nextInt();
str[i] = scanner.next();
}
for(int i = 0;i<n;i++) {
if(str[i].contains("z")) {
System.out.println(26);
}else if(str[i].contains("y")) {
System.out.println(25);
}else if(str[i].contains("x")) {
System.out.println(24);
}else if(str[i].contains("w")) {
System.out.println(23);
}else if(str[i].contains("v")) {
System.out.println(22);
}else if(str[i].contains("u")) {
System.out.println(21);
}else if(str[i].contains("t")) {
System.out.println(20);
}else if(str[i].contains("s")) {
System.out.println(19);
}else if(str[i].contains("r")) {
System.out.println(18);
}else if(str[i].contains("q")) {
System.out.println(17);
}else if(str[i].contains("p")) {
System.out.println(16);
}else if(str[i].contains("o")) {
System.out.println(15);
}else if(str[i].contains("n")) {
System.out.println(14);
}else if(str[i].contains("m")) {
System.out.println(13);
}else if(str[i].contains("l")) {
System.out.println(12);
}else if(str[i].contains("k")) {
System.out.println(11);
}else if(str[i].contains("j")) {
System.out.println(10);
}else if(str[i].contains("i")) {
System.out.println(9);
}else if(str[i].contains("h")) {
System.out.println(8);
}else if(str[i].contains("g")) {
System.out.println(7);
}else if(str[i].contains("f")) {
System.out.println(6);
}else if(str[i].contains("e")) {
System.out.println(5);
}else if(str[i].contains("d")) {
System.out.println(4);
}else if(str[i].contains("c")) {
System.out.println(3);
}else if(str[i].contains("b")) {
System.out.println(2);
}else if(str[i].contains("a")) {
System.out.println(1);
}
}
}
}
D. Lost Permutation
题目:
A sequence of nn numbers is called a permutation if it contains all integers from 11 to nn exactly once. For example, the sequences [3,1,4,23,1,4,2], [11] and [2,12,1] are permutations, but [1,2,11,2,1], [0,10,1] and [1,3,41,3,4] — are not.
Polycarp lost his favorite permutation and found only some of its elements — the numbers b1,b2,…bmb1,b2,…bm. He is sure that the sum of the lost elements equals ss.
Determine whether one or more numbers can be appended to the given sequence b1,b2,…bmb1,b2,…bm such that the sum of the added numbers equals ss, and the resulting new array is a permutation?
Note
In the test case of the example, m=3,s=13,b=[3,1,4]. You can append to b the numbers 6,2,5, the sum of which is 6+2+5=13. Note that the final array will become [3,1,4,6,2,5], which is a permutation.
In the second test case of the example, m=1,s=1,b=[1]. You cannot append one or more numbers to [1] such that their sum equals 1 and the result is a permutation.
In the third test case of the example, m=3,s=3,b=[1,4,2]. You can append the number 3 to b. Note that the resulting array will be [1,4,2,3], which is a permutation.
思路:
首先排序,然后使用另一个数组,这个数组的每个元素等于下标+1,然后将在第一个数组中出现过的元素改为0,然后将前面n个数加起来与输入的结果作对比,如果相等,那么就是“YES”如果已经超过,那就是“NO”,没有就继续往后面加一个,判断是否相等,相等输出“YES”,小于就继续加,大于就输出“NO”。
代码:
点击查看代码
import java.util.*;
public class MainD {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int nums,sum;
int[] arr;
for(int i = 0;i < n;i++) {
nums = scanner.nextInt();
sum = scanner.nextInt();
arr = new int[nums];
for(int j = 0;j < nums;j++) {
arr[j] = scanner.nextInt();
}
detemine(arr,sum);
}
}
public static void detemine(int[] arr,int s) {
int[] arr1 = new int[50];
for(int i = 0;i<50;i++) {
arr1[i] = i+1;
}
for(int i = 0;i < arr.length-1;i++) {
for(int j = 0;j < arr.length-1-i;j++) {
if(arr[j]>arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int i=0;i<arr1.length;i++) {
for(int j = 0;j<arr.length;j++) {
if(arr1[i]==arr[j]) {
arr1[i]=0;
break;
}
}
}
int sum=0;
for(int i=0;i<arr[arr.length-1];i++) {
sum+=arr1[i];
}
int flag = arr[arr.length-1];
while(true) {
if(sum>s) {
System.out.println("NO");
break;
}
else if(sum==s) {
System.out.println("YES");
break;
}else if(sum<s) {
flag++;
sum += flag;
}
}
}
}
E.Challenging Valleys
题目:
You are given an array a[0…n−1] of n integers. This array is called a "valley" if there exists exactly one subarray a[l…r] such that:
0≤l≤r≤n−1,
al=al+1=al+2=⋯=ar,
l=0 or al−1>al,
r=n−1 or ar<ar+1.
Here are three examples:
The first image shows the array [3,2,2,1,2,2,3], it is a valley because only subarray with indices l=r=3 satisfies the condition.
The second image shows the array [1,1,1,2,3,3,4,5,6,6,6], it is a valley because only subarray with indices l=0,r=2 satisfies the codition.
The third image shows the array [1,2,3,4,3,2,1], it is not a valley because two subarrays l=r=0 and l=r=6 that satisfy the condition.
You are asked whether the given array is a valley or not.
Note that we consider the array to be indexed from 0.
思路:
这道题就是判断是不是谷,看最低点的个数。
代码:
点击查看代码
import java.util.*;
public class MainE {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int k = 0;k < n;k++) {
int m = scanner.nextInt();
int[] arr = new int[m];
for(int i = 0;i < m;i++) {
arr[i] = scanner.nextInt();
}
int[] arr1 = new int[m];
int length = 0;
for(int i = 0;i < m-1;i++) {
if(arr[i]!=arr[i+1]) {
arr1[length++] = arr[i];
}
}
arr1[length++] = arr[m-1];
//这个一定要放到这个新的数组这里来判断,如果说给了以排重复的数,最后新数组里面只有一个数,下面的length-2就越界了
if(length == 1||length==2) {
System.out.println("YES");
continue;
}
int count = 0;
if(arr1[0]<arr1[1]) {
count++;
}
if(arr1[length-1]<arr1[length-2]) {
count++;
}
for(int i = 1;i < length-1;i++) {
if(arr1[i]<arr1[i-1]&&arr1[i]<arr1[i+1]) {
count++;
}
}
if(count<=1) {
System.out.println("YES");
}else {
System.out.println("NO");
}
}
}
}
F.Advantage
题目:
There are n participants in a competition, participant i having a strength of si.
Every participant wonders how much of an advantage they have over the other best participant. In other words, each participant i wants to know the difference between si and sj, where j is the strongest participant in the competition, not counting i (a difference can be negative).
So, they ask you for your help! For each i (1≤i≤n) output the difference between si and the maximum strength of any participant other than participant i.
Note
For the first test case:
The first participant has a strength of 4 and the largest strength of a participant different from the first one is 7, so the answer for the first participant is 4−7=−3.
The second participant has a strength of 7 and the largest strength of a participant different from the second one is 5, so the answer for the second participant is 7−5=2.
The third participant has a strength of 3 and the largest strength of a participant different from the third one is 7, so the answer for the third participant is 3−7=−4.
The fourth participant has a strength of 5 and the largest strength of a participant different from the fourth one is 7, so the answer for the fourth participant is 5−7=−2.
思路:
找出最大值和第二大的值:除最大值以外,其他都减去最大值,最大值减去第二大的值就能够得到
代码:
点击查看代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i = 0;i < n;i++) {
int m = scanner.nextInt();
int[] arr = new int[m];
int[] arr1 = new int[m];
int max=0,next=0;
for(int j = 0;j < m;j++) {
arr[j] = scanner.nextInt();
if(arr[j]>=max) {
next = max;
max = arr[j];
}
if(arr[j]>next&&arr[j]<max) {
next = arr[j];
}
}
for(int j = 0;j < m;j++) {
if(arr[j]<max) {
arr1[j]=arr[j]-max;
}else if(arr[j]==max) {
arr1[j]=max-next;
}
}
for(int j = 0;j < m;j++) {
System.out.print(arr1[j]+" ");
}
System.out.println();
}
}
}