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

news/2024/6/18 21:13:10 标签: python, java, 开发语言

python_0">python

L1-001 Hello World

python">print("Hello World!")

L1-002 打印沙漏

python">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 个位数统计

python">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 计算摄氏温度

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

L1-005 考试座位号

python">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 连续因子

python">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 念数字

python">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 求整数段和

python">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,看看是否输出

python">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 比较大小

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

java_148">java

L1-001 Hello World

java">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 打印沙漏

java">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 个位数统计

java">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 计算摄氏温度

java">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 考试座位号

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

java">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 连续因子

java">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 念数字

java">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 求整数段和

java">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个数求和

java">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 比较大小

java">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]);
    }
}

http://www.niftyadmin.cn/n/177583.html

相关文章

动态规划专题(明天继续)

动态规划求最大值&#xff1a; 题目描述 小蓝在一个 nn 行 mm 列的方格图中玩一个游戏。 开始时&#xff0c;小蓝站在方格图的左上角&#xff0c;即第 11 行第 11 列。 小蓝可以在方格图上走动&#xff0c;走动时&#xff0c;如果当前在第 rr 行第 cc 列&#xff0c;他不能…

干货 | 开关电源的PCB布线设计技巧—如何降低EMI?

开关电源PCB排版是开发电源产品中的一个重要过程。许多情况下&#xff0c;一个在纸上设计得非常完美的电源可能在初次调试时无法正常工作&#xff0c;原因是该电源的PCB排版存在着许多问题。为了适应电子产品飞快的更新换代节奏&#xff0c;产品设计工程师更倾向于选择在市场上…

数字中国建设进行时:吉林大学党委常务副书记冯正玉一行调研实在智能

两会前夕&#xff0c;中共中央、国务院印发了《数字中国建设整体布局规划》&#xff0c;明确了加快数字中国建设的重点任务。《规划》强调&#xff0c;要加强整体谋划、统筹推进&#xff0c;把各项任务落到实处。在强化人才支撑的第四要点上&#xff0c;指出统筹布局一批数字领…

安卓递归获取文件夹以及子文件下所有文件

调用recursionAllFile方法获取(注意耗时操作放子线程)/*** 递归获取文件夹以及子文件夹下面所有文件* param path 路径*/ fun recursionAllFile(path: String): List<String>? {val allFiles: MutableList<String> ArrayList()if (TextUtils.isEmpty(path)) retu…

近几年NLP比较promising的方法

Contrastive Learning 对比学习的想法是&#xff0c;把不同类别的样本在特征空间推开&#xff0c;而让相似的样本距离更近&#xff0c;从而获得更好的样本表示 NLP中一个非常经典的是danqi女神的SimCSE&#xff1a;Raki的读paper小记&#xff1a;SimCSE: Simple Contrastive …

java学习笔记——抽象类

2.1 概述 由来 父类中的方法&#xff0c;被他的子类们重写&#xff0c;子类各自的实现都不尽相同。那么父类的方法声明和方法主体&#xff0c;只有声明还有意义&#xff0c;而方法主体则没有存在的意义了。我们把没有主体的方法称为抽象方法。java语法规定&#xff0c;包含抽象…

【Python】如何有效比较两个时间序列在图形上的相似度?

文章目录前言一、1.准备二、实操1.使用Matplotlib可视化比较两个时间序列2.计算两个时间序列的相关系数&#xff1a;3.使用Python实现动态时间规整算法&#xff08;DTW&#xff09;&#xff1a;总结前言 比较两个时间序列在图形上是否相似&#xff0c;可以通过以下方法&#x…

服务器机房(数据中心)是什么需要达到什么要求?企业级与数据中心级有什么区别?

服务器机房&#xff08;数据中心&#xff09;是什么需要达到什么要求&#xff1f;企业级与数据中心级有什么区别&#xff1f;我是艾西&#xff0c;最近很多小伙伴都对我发出过不少关于服务器机房&#xff08;数据中心&#xff09;是什么的疑问&#xff0c;以及自建机房和数据中…