前言
水题比较多还是刷快一点好,今天是每日打卡第4天,然后这一次,我不仅仅要用java写题目了,我还要用Python,主要是java写,但是坑人的输入输出还有部分性能问题,评测问题为了拿分,必要时使用Python。
L1-026 I Love GPLT (5 分)
public class Main {
public static void main(String[] args) {
String Love = "I Love GPLT";
char[] chars = Love.toCharArray();
for (char aChar : chars) {
System.out.println(aChar);
}
}
}
每日水题~
L1-029 是不是太胖了 (5 分)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int height = scanner.nextInt();
if(height>100 && height <=300){
double wight = (height - 100) * 0.9;
System.out.printf("%.1f",wight*2);
}
}
}
L1-036 A乘以B (5 分)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int A = scanner.nextInt();
int B = scanner.nextInt();
System.out.println(A*B);
}
}
L1-038 新世界 (5 分)
print("Hello World")
print("Hello New World")
看到了吧,今天不是我飘了,而是那啥,水题比较多。
L1-040 最佳情侣身高差 (10 分)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String [][]input=new String[n][2];
for(int i=0;i<n;i++){
for(int j=0;j<2;j++){
input[i][j]=sc.next();
}
}
for(int i=0;i<n;i++){
if(input[i][0].equals("M")){
double a=Double.parseDouble(input[i][1])/1.09;
System.out.printf("%.2f",a);
}
if(input[i][0].equals("F")){
double a=Double.parseDouble(input[i][1])*1.09;
System.out.printf("%.2f",a);
}
if(i!=n-1){
System.out.println();
}
}
}
}
L1-037 A除以B (10 分)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double A = scanner.nextInt();
double B = scanner.nextInt();
double j=0;
if (B>0){
j=A/B;
System.out.printf("%.0f",A);
System.out.print("/");
System.out.printf("%.0f",B);
System.out.print("=");
System.out.printf("%.2f%n",j);
}
if(B<0){
j=A/B;
System.out.printf("%.0f",A);
System.out.print("/(");
System.out.printf("%.0f",B);
System.out.print(")=");
System.out.printf("%.2f%n",j);
}
if(B==0) {
System.out.printf("%.0f",A);
System.out.print("/");
System.out.printf("%.0f",B);
System.out.print("=");
System.out.println("Error");
}
}
}
L1-031 到底是不是太胖了 (10 分)
没啥好说的继续水~
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n=scanner.nextInt();
String[][] body =new String [n][2];
for(int i=0;i<n;i++) {
for(int j=0;j<2;j++) {
body[i][j]=scanner.next();
}
}
for(int i=0;i<n;i++) {
double wei=(Integer.parseInt(body[i][0])-100)*0.9*2;
int weight=Integer.parseInt(body[i][1]);
if(Math.abs(weight-wei)<wei*0.1) {
System.out.println("You are wan mei!");
}
else if(weight>wei){
System.out.println("You are tai pang le!");
}
else {
System.out.println("You are tai shou le!");
}
}
}
}
L1-028 判断素数 (10 分)
log N 判断素数
这里给出两个快速判断素数的方法,对了,1 不是素数,是一个广义素数。
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 num = scanner.nextInt();
if(isValid(num)){
System.out.println("Yes");
}else {
System.out.println("No");
}
}
}
public static boolean isValid(int number){
if(number==1){
return false;
}else {
int n= (int) Math.sqrt(number);
for (int i=2;i<=n;i++){
if(number%i==0){
return false;
}
}
}
return true;
}
}
最快判断
这个我是特意查了一下这个公式的,有一个定理。
public static boolean isOK(int number){
if (number == 2 || number == 3)
{
return true;
}
if (number % 6 != 1 && number % 6 != 5)
{
return false;
}
for (int i = 5; i <= (Math.sqrt(number)); i += 6)
{
if (number%i == 0 || number % (i + 2) == 0)
{
return false;
}
}
return true;
}
}
使用这个方法即可。 不好记就用上面那个。
L1-035 情人节 (15 分)
这题怎么说,就这样做,不过现在只要是分数>=15的我都怕输入问题。 不过还好,这题没事。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
String[] Peoples=new String[100];
int i=0;
boolean flag=true;
while (flag){
String s = scanner.nextLine();
if (s.equals(".")) {
flag=false;
}else {
Peoples[i] = s;
i++;
}
}
if (Peoples[1]==null) {
System.out.println("Momo... No one is for you ...");
return;
}
if (Peoples[13] == null) {
System.out.println(Peoples[1]+" is the only one for you...");
return;
}
System.out.println(Peoples[1]+" and "+Peoples[13]+" are inviting you to dinner...");
}
}
L1-033 出生年 (15 分)
这题注意细节就可以,然后可以直接枚举出来。
import java.util.Scanner;
public class Main {
public static void main(String [] args) {
Scanner input = new Scanner (System.in);
int X = input.nextInt();
int Y = input.nextInt();
int year = 0;
int Year [] = new int [4];
for(int i = X; ; i++) {
int count = 1;
Year[0] = i / 1000;
Year[1] = i % 1000 / 100;
Year[2] = i % 100 / 10;
Year[3] = i % 10;
if(Year[0] != Year[1] && Year[0] != Year[2] && Year[0] != Year[3])
count++;
if( Year[1] != Year[2] && Year[1] != Year[3])
count++;
if( Year[2] != Year[3])
count++;
if(count == Y) {
year = i;
break;
}
}
System.out.print(year - X + " ");
System.out.printf("%04d",year);
}
}
L1-030 一帮一 (15 分)
这题也好办,找就好了。性别无符合,后面的往前走,然后删掉这两货。 可以直接用ArraryList 但是考虑到这玩意会移动(删掉元素后,所以还是老老实实用数组)
import java.io.BufferedInputStream;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(new BufferedInputStream(System.in));
int N=scanner.nextInt();
String[]Names=new String[N];
int[]Sexs=new int[N];
for(int i=0;i<N;i++)
{
Sexs[i]=scanner.nextInt();
Names[i]=scanner.next();
}
for (int i=0;i<N/2;i++)
{
for (int j=N-1;j>=N/2;j--)
{
if(Sexs[i]!=Sexs[j]&&Sexs[j]!=-1)
{
System.out.println(Names[i]+" "+Names[j]);
Sexs[j]=-1;
break;
}
}
}
}
}
接下来还剩下四个20分大题,为了安全起见,非必要用Python。
L1-027 出租 (20 分)
题目有点骚,其实我早就相关这个问题,如果是完全还原的话我不清除,但是这个案例里面可不是完全还原,那么就简单了。
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(new BufferedInputStream(System.in));
String Phone = scanner.next();
char[] PhoneChars = Phone.toCharArray();
int[] nums = new int[10];
ArrayList<Integer> num = new ArrayList<>();
for (char phoneChar : PhoneChars) {
nums[phoneChar-'0']++;
}
for (int i = nums.length-1; i >=0; i--) {
if(nums[i]!=0){
num.add(i);
}
}
int[] index = new int[11];
for (int i = 0; i < PhoneChars.length; i++) {
int ind = num.indexOf((int) (PhoneChars[i] - '0'));
index[i]=ind;
}
show(index,num);
}
public static void show(int[] index,ArrayList<Integer> num){
StringBuilder arr = new StringBuilder();arr.append("int[] arr = new int[]");
StringBuilder index_ = new StringBuilder();index_.append("int[] index = new int[]");
arr.append("{");
for (int i = 0; i < num.size()-1; i++) {
arr.append(num.get(i)).append(",");
}
arr.append(num.get(num.size()-1));
arr.append("}").append(";");
index_.append("{");
for (int i = 0; i < index.length-1; i++) {
index_.append(index[i]).append(",");
}
index_.append(index[index.length-1]);
index_.append("}").append(";");
System.out.println(arr);
System.out.println(index_);
}
}
我只想吐槽那个坑逼的输出格式。
L1-032 Left-pad (20 分)
好,干“好事”我辈义不容辞。 不过这题,考虑到 10^4 所以,那啥用Python。
N, S = input().split(' ')
string = input()
N = int(N)
if N > len(string):
print(S*(N-len(string)) + str(string))
else:
print(string[-N:])
真滴,爽歪歪~~
L1-034 点赞 (20 分)
看到输入,我就果断选择Python。
x=int(input())
y=[]
a=[]
for i in range(x):
z=[eval(i) for i in input().split()]
z.pop(0)
a.extend(z)
a.sort(reverse=True)
for i in range(len(a)):
if i==0:
y.append(a.count(a[i]))
else:
if a[i]!=a[i-1]:
y.append(a.count(a[i]))
else:
y.append(-1)
b=max(y)
print(a[y.index(b)],b)
L1-039 古风排版 (20 分)
这种题目就是要找规律
package com.huterox.Level1;
import java.io.BufferedInputStream;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(new BufferedInputStream(System.in));
String n_=scanner.nextLine();
int n=Integer.parseInt(n_);
String str=scanner.nextLine();
char[] Chars=str.toCharArray();
int m=str.length();
int l=0;
if(m%n==0) {
l=m/n;
}else {
l=(int)(m/n)+1;
}
int sum=l*n;
char[] ch1=new char[sum];
for(int i=0;i<sum;i++) {
if(i<m) {
ch1[i]=Chars[i];
}else {
ch1[i]=' ';
}
}
for(int i=0;i<n;i++) {
int u=sum-n+i;
for(int j=0;j<l;j++) {
System.out.print(ch1[u-j*n]);
}
if(i<n-1) {
System.out.println();
}
}
}
}
|