天梯赛练习集-L1-031到L1-040–python - java
文章目录
Python
L1-031 到底是不是太胖了
n = int(input())
for i in range(n):h,w = map(int,input().split())a = (h-100) * 1.8if abs(a - w) >= a *0.1:if a > w:print("You are tai shou le!")else:print("You are tai pang le!")else:print("You are wan mei!")
L1-032 Left-pad
a = input().split()
n = int(a[0])
c = a[1]
s = input()
l = len(s)
if l >= n:print(s[l-n:])
else:print(c*(n-l)+s)
L1-033 出生年
def check(a,b):s = set()for i in range(len(a)):s.add(a[i])if len(a) != 4:s.add('0')return b == len(s)a = input().split()
y = a[0]
year = int(y)
x = int(a[1])
i = 0
while True: if check(y,x):print(i,"%.4d" %int(y))breaki += 1y = str(year+i)
L1-034 点赞
n = int(input())
a = [0 for i in range(1001)]
maxvalue = 0
maxkey = 0
for i in range(n):s = list(map(int,input().split()))for j in range(1,len(s)):a[s[j]] += 1if a[s[j]] > maxvalue:maxvalue = a[s[j]]maxkey = s[j]if maxvalue == a[s[j]]:maxkey = max(maxkey,s[j])
print(maxkey,maxvalue)
L1-035 情人节
s=[]
n = input()
while n != ".":s.append(n)n = input()
if len(s) < 2:print("Momo... No one is for you ...")
elif len(s) < 14:print(s[1],"is the only one for you...")
else:print(s[1],"and",s[13],"are inviting you to dinner...")
L1-036 A乘以B
a,b = map(int,input().split())
print(a*b)
L1-037 A除以B
a,b = map(int,input().split())
if b == 0:c = "Error"
else:c = "%.2f" % (a/b)
if b < 0:print("%d/(%d)=" %(a,b),end='')
else:print("%d/%d=" %(a,b),end='')
print(c)
L1-038 新世界
print("""Hello World
Hello New World""")
L1-039 古风排版
n = int(input())
l = [[] for i in range(n)]
s = input()
j = 0
for i in s:l[j].append(i)j = (j + 1) % n
while j != 0:l[j].append(' ')j = (j + 1) % n
for i in l:print("".join(i[::-1]))
L1-040 最佳情侣身高差
n = int(input())
for i in range(n):a = input().split()sex = a[0]h = float(a[1])if sex == "M":print("%.2f" % (h/1.09))else:print("%.2f" % (h*1.09))
Java
L1-031 到底是不是太胖了
import java.io.*;public class Main {static final PrintWriter print = new PrintWriter(System.out);static final StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));public static int nextInt() throws IOException {st.nextToken();return (int) st.nval;}public static void main(String[] args) throws IOException {int n = nextInt();for (int i = 0; i < n; i++) {int h = nextInt();int w = nextInt();double a = (h - 100) * 1.8;if (Math.abs(a - w) >= a * 0.1) {if (a > w) {print.println("You are tai shou le!");} else {print.println("You are tai pang le!");}} else {print.println("You are wan mei!");}}print.flush();}
}
L1-032 Left-pad
import java.io.*;public class Main {static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));public static void main(String[] args) throws IOException {String[] s1 = br.readLine().split(" ");int l = Integer.parseInt(s1[0]);String c = s1[1];String s = br.readLine();if (s.length() > l) {System.out.println(s.substring(s.length() - l));}else{System.out.println(c.repeat(l - s.length()) + s);}}
}
L1-033 出生年
import java.io.*;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;public class Main {static final StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));public static int nextInt() throws IOException {st.nextToken();return (int) st.nval;}public static void main(String[] args) throws IOException {int year = nextInt();int copy = year;int num = nextInt();while (true){if (check(year,num)){System.out.printf("%d %04d\\n",year - copy,year);break;}year++;}}private static boolean check(int year, int num) {int i = 0;Set<Integer> set = new HashSet<>();while (year > 0){set.add(year % 10);year /= 10;i++;}if (i != 4)set.add(0);return num==set.size();}
}
L1-034 点赞
map会超时
StreamTokenizer 的数字比readline的拆分要快。
printwriter没有必要。
BufferedReader超时
import java.io.*;public class Main {static final PrintWriter print = new PrintWriter(System.out);static final StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));public static int nextInt() throws IOException {st.nextToken();return (int) st.nval;}public static void main(String[] args) throws IOException {int n = nextInt();int max = 0;int[] arr = new int[1002];int maxKey = 0;for (int i = 0; i < n; i++) {int k = nextInt();for (int j = 0; j < k; j++) {int f = nextInt();arr[f]++;if (arr[f] > max){max = arr[f];maxKey = f;}else if (arr[f] == max){maxKey = Math.max(maxKey,f);}}}print.println(maxKey+" "+max);print.flush();}
}
L1-035 情人节
import java.io.*;
import java.util.ArrayList;public class Main {static final StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));public static String next() throws IOException {st.nextToken();return st.sval;}public static void main(String[] args) throws IOException {ArrayList<String> arr = new ArrayList<>();String s = next();while (s != null) {arr.add(s);s = next();}if (arr.size() < 2) {System.out.println("Momo... No one is for you ...");} else if (arr.size() < 14) {System.out.println(arr.get(1) + " is the only one for you...");} else {System.out.println(String.format("%s and %s are inviting you to dinner...", arr.get(1), arr.get(13)));}}}
L1-036 A乘以B
import java.io.*;public class Main {static final StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));public static int nextInt() throws IOException {st.nextToken();return (int) st.nval;}public static void main(String[] args) throws IOException {System.out.println(nextInt()*nextInt());}}
L1-037 A除以B
import java.io.*;public class Main {static final StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));public static int nextInt() throws IOException {st.nextToken();return (int) st.nval;}public static void main(String[] args) throws IOException {int a = nextInt();int b = nextInt();String c = b == 0 ? "Error" : String.format("%.2f", a * 1.0 / b);System.out.println(a + "/" + (b < 0 ? "(" + b + ")" : b)+"="+c);}
}
L1-038 新世界
import java.io.*;public class Main {public static void main(String[] args) throws IOException {System.out.println("Hello World");System.out.println("Hello New World");}
}
L1-039 古风排版
import java.io.*;public class Main {static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));public static void main(String[] args) throws IOException {int n = Integer.parseInt(br.readLine());char[] chars = br.readLine().toCharArray();StringBuilder[] ss = new StringBuilder[n];for (int i = 0; i < n; i++) {ss[i] = new StringBuilder();}int i = 0;for (char c : chars) {ss[i].append(c);i = (i + 1) % n;}if (i != 0) {while (i < n) {ss[i].append(' ');i++;}}for (StringBuilder s : ss) {System.out.println(s.reverse());}}
}
L1-040 最佳情侣身高差
import java.io.*;public class Main {static final StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));public static int nextInt() throws IOException {st.nextToken();return (int) st.nval;}public static String next() throws IOException {st.nextToken();return st.sval;}public static void main(String[] args) throws IOException {String man = "M";double rate = 1.09;int n = nextInt();for (int i = 0; i < n; i++) {String sex = next();st.nextToken();double h = st.nval;System.out.println(String.format("%.2f",man.equals(sex)?h/rate:h*rate));}}
}