美团第一次笔试练习(没写2.3)
1.
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int total=in.nextInt();
boolean flag = false;
double sum=0; double pricesum=0;
// 注意 hasNext 和 hasNextLine 的区别
while (total-->0) { // 注意 while 处理多个 case
double price = in.nextDouble();
double num = in.nextDouble();
if(num>price||num<=0){
System.out.print("error");
flag=true;
}
sum+=num;
pricesum+=price;
// System.out.println(num);
}
if(!flag){
double x=in.nextDouble();;
double y=in.nextDouble();;
if(x<y||y<=0){
System.out.print("error");
flag=true;
}
if(!flag){
if(pricesum>=x)pricesum-=y;
if(pricesum>sum){
String str=String.format("%.2f",sum);
System.out.print(str);
}else{
String str=String.format("%.2f",pricesum);
System.out.print(str);
}}
}
}
}
4.
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num= in.nextInt();
Map<Integer,Integer> map=new HashMap<>();
int i=0;
// 注意 hasNext 和 hasNextLine 的区别
while (num-->0) { // 注意 while 处理多个 case
map.put(in.nextInt(),i++);
}
int a=map.get(in.nextInt());
int b=map.get(in.nextInt());
if(a-b==1||b-a==1){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}
5.
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int a = in.nextInt();
int left=1,right=a;
while(left<=right){
System.out.print(left+++" ");
if(left<=right)
System.out.print(right--+" ");
}
}
}
6.
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int num = in.nextInt();
long sum=0;
int[] distance= new int[num];
for(int i=0;i<num;i++){
distance[i]=in.nextInt();
sum+=distance[i];
}
int x=in.nextInt();
int y=in.nextInt();
if(x>y){
int tmp=x;
x=y;
y=tmp;
}
long res=0;
for(int i=x-1;i<y-1;i++){
res+=distance[i];
}
if(res>sum-res){
System.out.print(sum-res);
}else{
System.out.print(res);
}
}
}
7.
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
char[][] nums = new char[n][m];
// 注意 hasNext 和 hasNextLine 的区别
for (int i = 0; i < n; i++) {
String str = in.next();
nums[i] = str.toCharArray();
}
int res = 0;
if (n < 3 || m < 3) {
System.out.print(0);
} else {
for (int i = 0; i < n - 2; i++) {
for (int j = 0; j < m - 2; j++) {
if (vaildall(i, j, nums)) {
res++;
}
}
}
System.out.print(res);
}
}
private static boolean vaildall(int i, int j, char[][] nums) {
// TODO
boolean hasA = false, hasB = false, hasC = false;
for (int a = i; a < 3 + i; a++) {
for (int b = j; b < j + 3; b++) {
char c = nums[a][b];
if (c == 'A') hasA = true;
else if (c == 'B') hasB = true;
else if (c == 'C') hasC = true;
else return false;
if (!vaild(a, b, nums, i, j)) return false;
}
}
return hasA && hasB && hasC;
}
private static boolean vaild(int a, int b, char[][] nums, int i, int j) {
// TODO
boolean flag = true;
if (a > i && nums[a][b] == nums[a - 1][b]) {
flag = false;
}
if (a < i + 2 && nums[a][b] == nums[a + 1][b]) {
flag = false;
}
if (b > j && nums[a][b] == nums[a][b - 1]) {
flag = false;
}
if (b < j + 2 && nums[a][b] == nums[a][b + 1]) {
flag = false;
}
return flag;
}
}
8.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] strings = reader.readLine().split(" ");
int n = Integer.parseInt(strings[0]);
int m = Integer.parseInt(strings[1]);
int[][] cake = new int[n][m];
for (int i = 0; i < n; i++) {
String[] s = reader.readLine().split(" ");
for (int j = 0; j < m; j++) {
cake[i][j] = Integer.parseInt(s[j]);
}
}
long minDiff = Long.MAX_VALUE;
long totalSum = 0;
for (int[] ints : cake) {
for (long anInt : ints) {
totalSum += anInt;
}
}
long sum = 0;
if (cake[0].length == 1) {
System.out.println(totalSum);
}
else {
//先想象把蛋糕横着分开
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++) {
for (int k = 0; k < m; k++) {
sum += cake[j][k];
}
}
minDiff = Math.min(minDiff, Math.abs(sum - (totalSum - sum)));
sum = 0;
}
//竖着分开的请况
for (int i = 1; i < m; i++) {
for (int[] ints : cake) {
for (int k = 0; k < m - i; k++) {
sum += ints[k];
}
}
minDiff = Math.min(minDiff, Math.abs(sum - (totalSum - sum)));
sum = 0;
}
System.out.println(minDiff);
}
}
}
9.
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int length = in.nextInt();
String str=in.next();
int min=1111111;
char[] c=str.toCharArray();
for(int i=1;i<length;i++){
if(length%i!=0)continue;
else{
int m=length/i;
char[][] nums=new char[i][m];
int index=0;
for(int a=0;a<i;a++){
for(int b=0;b<m;b++){
nums[a][b]=c[index++];
}
}
int res=0;
for(int a=0;a<i;a++){
for(int b=0;b<m;b++){
if(nums[a][b]!='0'){
dfs(a,b,nums,i,m,nums[a][b]);
res++;
}
}
}
min=Math.min(res,min);
}
}
System.out.print(min);
}
private static void dfs(int a, int b, char[][] nums, int i, int m,char tar) {
// TODO
if(a<0||a>=i||b<0||b>=m||nums[a][b]=='0'||nums[a][b]!=tar){
return;
}
nums[a][b]='0';
dfs(a+1,b,nums,i,m,tar);
dfs(a,b+1,nums,i,m,tar);
dfs(a-1,b,nums,i,m,tar);
dfs(a,b-1,nums,i,m,tar);
}
}
查看11道真题和解析
