天梯赛练习集--L1-011到L1-020--python - java

文章目录

  • python
    • L1-011 A-B
    • L1-012 计算指数
    • L1-013 计算阶乘和
    • L1-014 简单题
    • L1-015 跟奥巴马一起画方块
    • L1-016 查验身份证
    • L1-017 到底有多二
    • L1-018 大笨钟
    • L1-019 谁先倒
    • L1-020 帅到没朋友
  • java
    • L1-011 A-B
    • L1-012 计算指数
    • L1-013 计算阶乘和
    • L1-014 简单题
    • L1-015 跟奥巴马一起画方块
    • L1-016 查验身份证
    • L1-017 到底有多二
    • L1-018 大笨钟
    • L1-019 谁先倒
    • L1-020 帅到没朋友

python

L1-011 A-B

a = input()
b = set(input())
ans = ""
for i in a:
    if i not in b:
        ans += i
print(ans)

L1-012 计算指数

import math
a = int(input())
print("2^%d = %d" % (a,math.pow(2,a)))

L1-013 计算阶乘和

n = [1]
def de(b):
    if b == 1:
        return 1;
    n.append(b*de(b-1))
    return n[b-1]
sum = 0
a = int(input())
de(a)
for i in range(a):
    sum += n[i]
print(sum)

L1-014 简单题

print("This is a simple problem.")

L1-015 跟奥巴马一起画方块

a = input().split()
n = int(a[0])
b = a[1]*n
n = n*5
if n % 10 < 5:
    n /=10
else:
    n = n/10+1
for i in range(int(n)):
    print(b)

    

L1-016 查验身份证

n = int(input())
a = [7, 9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
b = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2','-1']
f = 0
for i in range(n):
    num = input()
    sum = 0
    for j in range(17):
        try:
            sum += int(num[j])*a[j]
            sum = sum % 11
        except:
            sum = 11
            break
    if b[sum] != num[17]:
        f = 1
        print(num)
if f == 0:
    print("All passed")
    

L1-017 到底有多二

a = input()
n = int(a)
count = 0
for i in a:
    if i == '2':
        count+=1
l = len(a)
if n % 2 ==0:
    count*=2
if n < 0:
    count*=1.5
    l -=1
count=count*100/l
print("%.2f%c" % (count,'%'))

L1-018 大笨钟

a = input()
b = a.split(':')
h = int(b[0])
m = int(b[1])
if h == 12 and m == 0:
    h -= 1
if h < 12:
    print("Only %s.  Too early to Dang." % a)
else:
    i = h -12
    if m > 0:
        i +=1
    print("Dang"*i)

L1-019 谁先倒

测试点3是长整型,不过python不用考虑
测试点4是结果为0,看看是否输出

a,b = map(int,input().split())
num = int(input())
m,n = 0,0
for i in range(num):
    x,y,z,j = map(int,input().split())
    all = x+ z
    if y == all and j != all:
        m+=1
    if j == all and y != all:
        n+=1
    if m > a:
        print('A')
        print(n)
        break
    if n > b:
        print('B')
        print(m)
        break

L1-020 帅到没朋友

n = int(input())
a = set()
for i in range(n):
    b = input().split()[1:]
    if len(b) != 1:
        a.update(b)
m = int(input())
b = input().split()

f = 0
c = []
for i in b:
    if i not in a:
        f = 1
        a.add(i)
        c.append(i)

if f == 0:
    print("No one is handsome",end='')
else:
    print(" ".join(c))

java

L1-011 A-B

我不是很理解为什么有时候过了,有时候非0返回,不过感觉加try的ac概率高。

import java.io.*;

public class Main {
    private final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException {
        
        try {
             String s= br.readLine();
             String b = String.format("[%s]", br.readLine());
             System.out.println(s.replaceAll(b, ""));
        } catch (Exception e) {
        }

    }
}

天梯赛练习集--L1-011到L1-020--python - java_第1张图片
天梯赛练习集--L1-011到L1-020--python - java_第2张图片

L1-012 计算指数

import java.util.Scanner;

public class Main {
    private final static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
            int i = sc.nextInt();
        System.out.printf("2^%d = %d%n",i,(int)Math.pow(2,i));
    }
}

L1-013 计算阶乘和

import java.util.Scanner;
public class Main {
    private final static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        int n = sc.nextInt();
        int ans = 0;
        int count = 1;
        for (int i = 1; i <= n; i++) {
            count *= i;
            ans += count;
        }
        System.out.println(ans);
    }
}

L1-014 简单题

public class Main {
    public static void main(String[] args) {
        System.out.println("This is a simple problem.");
    }
}

L1-015 跟奥巴马一起画方块

import java.util.Scanner;
import java.io.*;

public class Main {
    private final static Scanner sc = new Scanner(System.in);
        private final static PrintWriter print = new PrintWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) throws IOException {
        int n = sc.nextInt();
        String s = sc.next().repeat(n);
        for (int i = 0; i < n*1.0/2; i++) {
            print.println(s);
        }
        print.flush();
    }
}

L1-016 查验身份证

import java.io.*;

public class Main {
        private final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    static int[] num = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
    static char[] b = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

    public static void main(String[] args) throws IOException {
        int n = Integer.parseInt(br.readLine());
        boolean f = true;
        for (int i = 0; i < n; i++) {
            String s = br.readLine();
            if (!check(s)) {
                f = false;
                System.out.println(s);
            }
        }
        if (f) {
            System.out.println("All passed");
        }
    }

    private static boolean check(String s) {
        int sum = 0;
        for (int i = 0; i < s.length() - 1; i++) {
            int j = s.charAt(i) - '0';
            if (j < 0 || j > 9)
                return false;
            sum += num[i]*j;
        }
        return b[sum % 11] == s.charAt(s.length() - 1);
    }

}
    

L1-017 到底有多二

import java.io.*;

public class Main {
    private final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException {
        String s = br.readLine();
        String replace = s.replace("2", "");
        double i = 100;
        if (s.startsWith("-")){
            i *= 1.5;
            i *= 1-(replace.length()-1)*1.0/(s.length()-1);
        }else{
            i *= 1-replace.length()*1.0/s.length();
        }
        System.out.printf("%.2f%%\n",i *((s.charAt(s.length()-1)-'0')%2==0?2:1));
    }

}

L1-018 大笨钟

import java.io.*;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
    private final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException {
        String s = br.readLine();
        LocalTime time = LocalTime.parse(s, DateTimeFormatter.ofPattern("HH:mm"));
        if (time.isBefore(LocalTime.of(12,1))){
            System.out.printf("Only %s.  Too early to Dang.%n",s);
        }else{
            int h = time.getHour()-12;
            int m = time.getMinute();
            if (m>0){
                h++;
            }
            for (int i = 0; i < h; i++) {
                System.out.print("Dang");
            }
            System.out.println();
        }
    }

}

L1-019 谁先倒

import java.io.*;

public class Main {
    private final static 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 a = nextInt();
        int b = nextInt();
        int x = a, y = b;
        int n = nextInt();
        for (int i = 0; i < n; i++) {
            int x_han = nextInt(), x_hua = nextInt(), y_han = nextInt(), y_hua = nextInt();
            int he = x_han + y_han;
            if (he != x_hua && he != y_hua || he == x_hua && he == y_hua) {
                continue;
            }else if (he == x_hua) {
                x--;
            } else {
                y--;
            }
            if (x < 0) {
                System.out.println("A");
                System.out.println(b-y);
                break;
            }
            if (y < 0) {
                System.out.println("B");
                System.out.println(a-x);
                break;
            }
        }
    }
}


L1-020 帅到没朋友

这个题最后一个测试超时,也没啥办法试过很多了。
stream流啥的在这种小数据还慢些,哎。

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    private final static PrintWriter print = new PrintWriter(new OutputStreamWriter(System.out));
    private final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


    public static void main(String[] args) throws IOException {
        HashSet<String> set = new HashSet<>();
        int n = Integer.parseInt(br.readLine());
        for (int i = 0; i < n; i++) {
            String[] s = br.readLine().split(" ");
            if (s.length == 2)
                continue;            
            //for (int j = 1; j < s.length; j++) 
             //   set.add(s[j]);
            
            set.addAll(Arrays.asList(s));
            //set.addAll(Arrays.asList(s).subList(1, s.length)););
        }
        br.readLine();
        boolean f = false;
        String[] s = br.readLine().split(" ");
//        String[] array = Arrays.stream(s).filter(set::add).toArray(String[]::new);
//        if (array.length == 0)
//            print.print("No one is handsome");
//        else
//            print.print(String.join(" ", array));
        for (String x : s) {
            if (set.add(x)) {
                if (f)
                    print.print(" ");
                f = true;
                print.print(x);
            }
        }
        if (!f)
            print.print("No one is handsome");
        print.println();
        print.flush();
    }
}

你可能感兴趣的:(算法题,java,python,开发语言)