天梯赛练习集--L1-001到L1-010--python - java

文章目录

  • python
    • L1-001 Hello World
    • L1-002 打印沙漏
    • L1-003 个位数统计
    • L1-004 计算摄氏温度
    • L1-005 考试座位号
    • L1-006 连续因子
    • L1-007 念数字
    • L1-008 求整数段和
    • L1-009 N个数求和
    • L1-010 比较大小
  • java
    • L1-001 Hello World
    • L1-002 打印沙漏
    • L1-003 个位数统计
    • L1-004 计算摄氏温度
    • L1-005 考试座位号
    • L1-006 连续因子
    • L1-007 念数字
    • L1-008 求整数段和
    • L1-009 N个数求和
    • L1-010 比较大小

python

L1-001 Hello World

print("Hello World!")

L1-002 打印沙漏

a = input().split()
n = int(a[0])
c = 1
while(2*c**2-1<=n):
    c+=1
c-=1
b = 2*c - 1
for i in range(c):
    print(" "*i+a[1]*(b-2*i))
for i in range(1,c):
    print(" "*(c-i-1)+a[1]*(1+2*i))
print(n-2*c**2+1)

L1-003 个位数统计

a = input()
b = [0]*10
for i in a:
    j = ord(i)-ord('0')
    b[j] += 1
for i in range(len(b)):
    if b[i] != 0:
        print("%d:%d" % (i,b[i]))
    

L1-004 计算摄氏温度

n = (int(input())-32)*5/9
print("Celsius = %d" % n)

L1-005 考试座位号

n = int(input())
a = dict()
for i in range(n):
    x,y,z = input().split()
    a[y] = [x,z]
m = int(input())
n = input().split()
for i in n:
    print(" ".join(a[i]))
    

L1-006 连续因子

import math 

n = int(input())
max_Len = 0
ans = []
for i in range(2,int(math.sqrt(n)+1)):
    z = n 
    j = i
    while z % j == 0:
        z = z / j
        j += 1
    if j - i  > max_Len:
        max_Len = j - i
        ans = [i for i in range(i,j)]
if max_Len == 0:
    max_Len = 1
    ans = [n]
print(max_Len)
print("*".join([str(i) for i in ans]))

L1-007 念数字

dict = {"-":"fu",'0': "ling",'1': "yi",'2': "er",'3': "san",'4': "si",'5': "wu",'6': 'liu','7': 'qi','8': "ba",'9': "jiu"}
a = list(map(lambda x:dict[x],input()))
print(" ".join(str(i) for i in a))

L1-008 求整数段和

a = list(map(int,input().split()))
sum = 0
j = 0
for i in range(a[0],a[1]+1):
    print("%5d" % i,end='')
    sum +=i
    j+=1
    if j % 5 == 0:
        print()
if j % 5 != 0:
    print()
print("Sum =",sum)

L1-009 N个数求和

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

def comm(x,y):
    if y == 0:
        return x
    return comm(y,x%y)
def solve(x,y,a,b):
    c = comm(max(y,b),min(y,b))
    c = b*y/c
    return (x*(c//y)+a*(c//b),c)

    
n = int(input())
a = input().split()
x = 0
y = 1
for i in a:
    b,c = list(map(int,i.split('/')))
    (x,y)=solve(x,y,b,c)
ans = int(x/y)
f = False
if x < 0:
    f = True
    x = -x
x = x%y
z = comm(x,y)
if ans == 0 and x == 0:
    print(0)
else:
    if ans != 0:
        print(ans,end='')
    if x != 0:
        if ans !=0:
            print(' ',end='')
        if f:
            print('-',end='')
        print('%d/%d'%(x/z,y/z),end='')
    print()


L1-010 比较大小

print(*sorted(map(int,input().split())),sep="->")

java

L1-001 Hello World

import java.io.*;
public class Main {
    private final static PrintWriter print = new PrintWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) throws IOException {
        print.println("Hello World!");
        print.flush();
    }
}

L1-002 打印沙漏

import java.io.*;
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 {
        String[] s = br.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        char c = s[1].charAt(0);
        int i = 1;
        if (i != n) {
            while (2 * i * i - 1 < n)
                i++;
            i--;
        }
        for (int j = i; j > -i + 1; j--) {
            if (j > 0) {
                print.println(" ".repeat(i - j) + String.valueOf(c).repeat(2 * j - 1));
            } else {
                print.println(" ".repeat(i + j -2) + String.valueOf(c).repeat(-2 * j + 3));
            }
        }
        print.println(n - 2 * i * i + 1);
        print.flush();
    }
}

L1-003 个位数统计

import java.io.*;
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 {
        String s = br.readLine();
        int[] a = new int[10];
        for (int i = 0; i < s.length(); i++) {
            a[s.charAt(i) - '0']++;
        }
        for (int i = 0; i < 10; i++) {
            if (a[i] != 0) {
                print.println(i + ":" + a[i]);
            }
        }
        print.flush();
    }
}
    

L1-004 计算摄氏温度

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.printf("Celsius = %d\n",(sc.nextInt()-32)*5/9);
    }
}

L1-005 考试座位号

超时,没办法的。或许多试试可能运气好能过

import java.util.*;
import java.io.*;
 
public class Main {
    private final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private final static PrintWriter print = new PrintWriter(new OutputStreamWriter(System.out));
	public static void main(String[] args) throws IOException {
		int n = Integer.parseInt(br.readLine());
        HashMap<String, String[]> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            String[] s = br.readLine().split(" ");
            map.put(s[1], s);
        }
        br.readLine();
        String[] a = br.readLine().split(" ");
        for (String s1 : a) {
            String[] data = map.get(s1);
            print.println(data[0] + " " + data[2]);
        }
        print.flush();
	}
 
}
    

L1-006 连续因子

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

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

    public static void main(String[] args) throws IOException {
        int n = sc.nextInt();
        int maxLen = 0;
        int max = 0;
        for (int i = 2; i <= Math.sqrt(n); i++) {
            int j = i;
            int z = n;
            while (z % j == 0) {
                z /= j;
                j++;
            }
            if (j - i > maxLen) {
                maxLen = j - i;
                max = i;
            }
        }
        if (maxLen == 0) {
            maxLen = 1;
            max = n;
        }

        print.println(maxLen);
        for (int i = 1; i < maxLen; i++) {
            print.printf("%d*", max++);
        }
        print.println(max);
        print.flush();
    }
}

L1-007 念数字

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

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


    public static void main(String[] args) throws IOException {
        String []a = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
        char[] chars = br.readLine().toCharArray();
        int i = 0;
        if(chars[i] == '-') {
            System.out.print("fu ");
            i++;
        }
        while (i < chars.length-1){
            System.out.print(a[chars[i] - '0'] + " ");
            i++;
        }
        System.out.println(a[chars[chars.length-1] - '0']);
    }

}

L1-008 求整数段和

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

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


    public static void main(String[] args) throws IOException {
        int a = sc.nextInt();
        int b = sc.nextInt();
        int count = 0;
        int sum = 0;
        while (a<=b){
            sum += a;
            System.out.printf("%5d",a++);
            count++;
            if (count%5==0)
                System.out.println();
        }
        if(count % 5 !=0)
            System.out.println();
        System.out.printf("Sum = %d\n",sum);
    }



}

L1-009 N个数求和

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

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

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    public long comm(long x, long y) {
        if (y == 0)
            return x;
        else
            return comm(y, x % y);
    }

    public void solve(long x, long y) {
        if (x == 0)
            return;
        long c = comm(Math.max(b, y), Math.min(b, y));
        long k = y * b / c;
        a = x * (k / y) + a * (k / b);
        b = k;
        if (a == 0)
            b = 1;
    }

    static long a = 0;
    static long b = 1;

    public void run() throws IOException {
        int n = Integer.parseInt(br.readLine());
        String[] s = br.readLine().split(" ");

        for (String ss : s) {
            String[] split = ss.split("/");
            long x = Long.parseLong(split[0]);
            long y = Long.parseLong(split[1]);
            solve(x, y);
        }
        boolean f = true;
        long c = a / b;
        if (c != 0)
            System.out.print(c);
        if (a < 0) {
            f = false;
            a = -a;
        }
        a = a % b;
        if (a != 0) {
            if (c != 0) {
                System.out.print(" ");
            }
            long i = comm(a, b);
            a /= i;
            b /= i;
            System.out.println((f ? "" : "-") + a + "/" + b);
        }
        if (a == 0 && c == 0)
            System.out.println(0);
    }

}

L1-010 比较大小

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

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

    public static void main(String[] args) throws IOException {
        printSort(sc.nextInt(),sc.nextInt(),sc.nextInt());
        print.flush();
    }

    public static void printSort(int... a){
        Arrays.sort(a);
        for (int i = 0; i < a.length-1; i++) {
            print.print(a[i]);
            print.print("->");
        }
        print.println(a[a.length-1]);
    }
}

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