天梯赛练习集-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.8
    if 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))
        break
    i += 1
    y = 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]] += 1
        if 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));
        }
    }
}
根据提供的引用内容,这段代码是用来统计输入数字中奇数和偶数的个数的。代码中使用循环遍历输入的数字,通过对2取模运算来判断数字是奇数还是偶数,然后分别对奇数和偶数的计数变量进行加一操作。循环结束后,输出奇数计数变量和偶数计数变量的值,中间用空格隔开。\[1\]\[2\]\[3\] 这段代码可以用来解决团体程序设计天梯赛-练习 L1-022 奇偶分家的问题。 #### 引用[.reference_title] - *1* [PTA团队天梯赛L1-022 奇偶分家](https://blog.csdn.net/m0_46492118/article/details/114481127)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [PAT团队程序设计天梯赛-习题L1-022 奇偶分家](https://blog.csdn.net/qq_38234015/article/details/81291913)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [团体程序设计天梯赛-练习 L1-022 奇偶分家 (10分)(C语言)](https://blog.csdn.net/Baridhu/article/details/109899606)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只小余

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值