文章目录
Python
L1-021 重要的话说三遍
print("""I'm gonna WIN!
I'm gonna WIN!
I'm gonna WIN!""")
L1-022 奇偶分家
n = int(input())
a = map(int,input().split())
count = 0
for i in a:
if i % 2 == 1:
count +=1
print(count,n-count)
L1-023 输出GPLT
python中可以对字典进行排序
items() 函数以列表返回可遍历的(键, 值) 元组数组
ss = input().upper()
map = {'G':0,'P':0,'L':0,'T':0}
for i in ss:
if map.get(i) != None:
map[i] += 1
map = sorted(map.items(), key=lambda x: x[1], reverse=False)
s = "GPLT"
ans = ""
j = 0
for i in map:
ans += s*(i[1]-j)
j = i[1]
s = s.replace(i[0],"")
print(ans)
L1-024 后天
n = int(input())
n = (2+n)%7
print(7 if n == 0 else n)
L1-025 正整数A+B
测试点3应该是多个空格,但是第二个是数字
测试点4,5,6应该是数字的范围判断
这个的判断优雅的应该抽出来方法来,java是这么弄的
s = input().split(" ")
f = True
try:
a = int(s[0])
if a < 1 or a > 1000:
f = False
a = "?"
except ValueError:
f = False
a = "?"
try:
if len(s) == 2:
b = int(s[1])
if b < 1 or b > 1000:
f = False
b = "?"
else:
f = False
b = "?"
except ValueError:
f = False
b = "?"
if f:
c = a + b
else:
c = "?"
print(a,"+",b,"=",c)
L1-026 I Love GPLT
print("""I
L
o
v
e
G
P
L
T""")
L1-027 出租
n = input()
a = set()
for i in n:
a.add(i)
arr = sorted(a, reverse=True)
index = []
for i in n:
index.append(str(arr.index(i)))
print("int[] arr = new int[]{"+",".join(arr)+"};")
print("int[] index = new int[]{"+",".join(index)+"};")
L1-028 判断素数
质数在以前文章有,比较简单。java可以直接调用api
from math import sqrt
def isPrime(n):
if n < 4:
return n > 1
if n % 6 != 1 and n % 6 != 5:
return False
for i in range(5, int(sqrt(n)) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
n = int(input())
for i in range(n):
m = int(input())
if isPrime(m):
print("Yes")
else:
print("No")
L1-029 是不是太胖了
n = int(input())
n = (n - 100)*1.8
print("%.1f" % n)
L1-030 一帮一
n = int(input())
a = list()
f = [True for i in range(n)]
for i in range(n):
s = input().split()
a.append(s)
l = int(n/2)
for i in range(l):
z = a[i][0]
for j in range(n-1, l-1, -1):
if f[j] and a[j][0] != z:
print(a[i][1],a[j][1])
f[j] = False
break
Java
L1-021 重要的话说三遍
public class Main {
public static void main(String[] args){
String s="I'm gonna WIN!\n".repeat(3);
System.out.printf(s);
}
}
L1-022 奇偶分家
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 n = nextInt();
int o = 0;
for (int i = 0; i < n; i++) {
if (nextInt() % 2 == 0)
o++;
}
System.out.println(String.format("%d %d",n-o,o));
}
}
L1-023 输出GPLT
这个题咋说呢,java我实在想不到怎么优雅的做出来了,其实可以用4个变量记录的然后顺序的if判断。
import java.io.*;
import java.util.*;
public class Main {
static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static final PrintWriter print = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
char[] chars = br.readLine().toUpperCase().toCharArray();
int[][] n = {{'G',0},{'P',0},{'L',0},{'T',0}};
for (char c : chars) {
for (int[] ints : n) {
if (c == ints[0])
ints[1]++;
}
}
Arrays.sort(n,(Comparator.comparingInt(o -> o[1])));
String s = "GPLT";
print.print(s.repeat(n[0][1]));
for (int i = 1; i < 4; i++) {
int count = n[i][1] - n[i-1][1];
s = s.replace((char)n[i-1][0]+"","");
print.print(s.repeat(count));
}
print.println();
print.flush();
}
}
L1-024 后天
import java.io.*;
import java.util.*;
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 n = nextInt()+2;
System.out.println(n>7?n-7:n);
}
}
L1-025 正整数A+B
import java.io.*;
import java.util.*;
public class Main {
static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static boolean f = true;
public static void main(String[] args) throws IOException {
String[] s = br.readLine().split(" ");
String n1 = get(s[0]);
String n2;
if (s.length == 2){
n2 = get(s[1]);
}else {
n2 = "?";
f = false;
}
StringBuilder sb = new StringBuilder();
String n3 = f ? String.valueOf(Integer.parseInt(n1) + Integer.parseInt(n2)) : "?";
sb.append(n1).append(" + ").append(n2).append(" = ").append(n3);
System.out.println(sb);
}
public static String get(String s) {
try {
int n = Integer.parseInt(s);
if (n < 1 || n > 1000)
throw new RuntimeException();
return String.valueOf(n);
} catch (Exception e) {
f = false;
return "?";
}
}
}
L1-026 I Love GPLT
jdk8没有3引号就是有点麻烦”“”好像是15才有,哎
import java.io.*;
import java.util.*;
public class Main {
static final PrintWriter print = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
String s = "I Love GPLT";
char[] chars = s.toCharArray();
for (char c : chars) {
print.println(c);
}
print.flush();
}
}
L1-027 出租
import java.io.*;
import java.util.*;
public class Main {
static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
char[] chars = br.readLine().toCharArray();
Set<Character> set = new HashSet<>();
for (char c : chars) {
set.add(c);
}
ArrayList<Character> arr = new ArrayList<>(set);
arr.sort((o1, o2) -> o2-o1);
System.out.println("int[] arr = new int[]{"+toStringArr(arr)+"};");
ArrayList<Integer> arr2 = new ArrayList<>();
for (char c : chars) {
arr2.add(arr.indexOf(c));
}
System.out.println("int[] index = new int[]{"+toStringArr(arr2)+"};");
}
public static String toStringArr(List arr) {
StringBuilder sb = new StringBuilder();
sb.append(arr.get(0));
for (int i = 1; i < arr.size(); i++) {
sb.append(',').append(arr.get(i));
}
return sb.toString();
}
}
L1-028 判断素数
import java.io.*;
import java.math.BigInteger;
import java.util.*;
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 long nextLong() throws IOException {
st.nextToken();
return (long) st.nval;
}
public static void main(String[] args) throws IOException {
int n = nextInt();
for (int i = 0; i < n; i++) {
System.out.println(BigInteger.valueOf(nextInt()).isProbablePrime(16)?"Yes" : "No");
}
}
}
L1-029 是不是太胖了
import java.io.*;
public class Main {
static final StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public static void main(String[] args) throws IOException {
st.nextToken();
System.out.println(String.format("%.1f",(st.nval-100)*1.8));
}
}
L1-030 一帮一
import java.io.*;
import java.util.LinkedList;
import java.util.Scanner;
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 {
int n = nextInt();
LinkedList<Student> list = new LinkedList<>();
for (int i = 0; i < n; i++) {
list.add(new Student(nextInt(),next()));
}
for (int i = 0; i < n / 2; i++) {
Student remove = list.remove(0);
Student part = null;
for (int j = list.size()-1; j >= 0; j--) {
if (list.get(j).sex != remove.sex){
part = list.remove(j);
break;
}
}
System.out.println(remove.name + " " + part.name);
}
}
static class Student{
int sex;
String name;
Student(int sex,String name){
this.sex = sex;
this.name = name;
}
}
}