PTA_天梯题集(1)

目录

前言

1.N个数求和

2.比较大小

3.计算指数

4.计算阶乘数

5.简单题

6.跟奥巴马一起画方块

7.查验身份证

8.树的遍历

9.最长对称字符串

总结


前言

PTA天梯题集

编写语言:JAVA


提示:以下是本篇文章正文内容,下面案例可供参考

1.N个数求和

package tianti;

import java.util.Scanner;

public class Pta1 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		//输入完n之后将光标移到一下行
		sc.nextLine();
		int a, b;
		
		String str=sc.nextLine();
//		System.out.println("This is:"+str);
		String[] split = str.split(" ");
		String s = split[0];
		//获取分子
		a = Integer.parseInt(s.substring(0, s.indexOf('/')));
		//获取分母
		b = Integer.parseInt(s.substring(s.indexOf('/') + 1));
		int x, y, common;
		
		common = gcd(a, b);
		a /= common;
		b /= common;
		
		for (int i = 1; i < n; i++) {
			s = split[i];
			x = Integer.parseInt(s.substring(0, s.indexOf('/')));
			y = Integer.parseInt(s.substring(s.indexOf('/') + 1));
			
			common = y * b / gcd(y, b);
			
			a *= common / b;
			x *= common / y;

			a += x;
			b = common;
			
			common = gcd(a, b);
			a /= common;
			b /= common;
			
		}
		
		if(a / b != 0) {
			if(a % b != 0) 
				System.out.println(a / b + " " + a % b + "/" + b);
			else
				System.out.println(a / b);
		} else {
			if(a % b != 0) 
				System.out.println(a % b + "/" + b);
			else
				System.out.println(0);
		}
	}
	
	private static int gcd(int a, int b) {
		if(b==0){
			return a;
		}else{
			return gcd(b,a%b);
		}
	}
}

2.比较大小

package tianti;

import java.util.Arrays;
import java.util.Scanner;

public class Pta2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		int N=3;
		int[] sort_result=new int[N];
		for(int i =0;i<N;i++){
			sort_result[i]=sc.nextInt();
		}
		//利用arrays的排序方法直接从小到大
		Arrays.sort(sort_result);
		for(int i=0;i<N;i++){
			//取消->
			if(i==N-1){
				System.out.print(sort_result[i]);
				break;
			}
			System.out.print(sort_result[i]+"->");
		}
	}

}

3.计算指数

package tianti;

import java.util.Arrays;
import java.util.Scanner;

public class Pta3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		//利用pow函数直接秒杀
		System.out.println("2^"+N+" = "+(int)Math.pow(2, N));
	}

}

4.计算阶乘数

package tianti;

import java.util.Arrays;
import java.util.Scanner;

public class Pta4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		System.out.println(fib(N));
	}

	public static int fib(int N) {
		int temp = 1;
		int sum = 0;
		for (int i = 1; i <= N; i++) {
			if (N == 0) {
				sum = 1;
			} else {
				temp *= i;
				sum += temp;
			}
		}
		return sum;
	}

}

5.简单题

package tianti;

import java.util.Scanner;

public class Pta5 {

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

}

6.跟奥巴马一起画方块

package tianti;

import java.util.Scanner;

public class Pta6 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		double n = sc.nextInt(); // 定义N个字符
		String s = sc.next(); 		// 定义字符样式
		if (3 <= n && n <= 21) {
			double h = n / 2; 		// 定义高度
			int x = (int) (n / 2);	//取整
			double y = h - x;
			if (y >= 0.5) {
				h++;
			}
			for (int i = 0; i < (int) h; i++) {  //通过高度来打印
				for (int j = 0; j < n; j++) {	 //通过长度来打印
					System.out.print(s);
				}
				System.out.println();
			}
		}

	}
}

7.查验身份证

package tianti;

import java.util.Scanner;

public class Pta7 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// 测试集
		// 350322111122334455
		Scanner sc = new Scanner(System.in);
		String[] Z = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
		char[] M = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
		//权重
		int[] pow = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };

		int N = sc.nextInt();
		char[][] id_spilt = new char[N][18];
		int allpassed = 1;
		// 将N个身份证号码存入二维表
		for (int i = 0; i < N; i++) {
			String id_card = sc.next();
			for (int j = 0; j < 18; j++) {
				id_spilt[i][j] = Character.valueOf(id_card.charAt(j));
			}
		}
		// 进行判断
		for (int i = 0; i < N; i++) {
			int z = 0;
			for (int j = 0; j < 17; j++) {
				z += ((id_spilt[i][j]) - '0') * pow[j];
			}
			//取模
			z %= 11;
			if (M[z] != id_spilt[i][17]) {
				allpassed = 0;
				for (int k = 0; k < 18; k++) {
					System.out.print(id_spilt[i][k]);
				}
				System.out.println();
			}
		}
		if (allpassed == 1) {
			System.out.println("All passed");
		}
	}

}

8.树的遍历

package tianti;

import java.util.*;
 
public class Pta8 {
	//存储后序遍历结点
    private static Vector<Integer> post = new Vector<>();
    //存储中序遍历结点
    private static Vector<Integer> in = new Vector<>();
    //有序/LinkedHashMap
    private static TreeMap<Integer,Integer> level = new TreeMap<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i = 0; i<n; i++){
        	post.add(sc.nextInt());
        }
        for(int i = 0; i<n; i++){
        	in.add(sc.nextInt());
        }
        pre(n-1,0,n-1,0);
        Set set = level.entrySet();//存储层序遍历结点
        Iterator it = set.iterator();//开始输出层序遍历结点
        Map.Entry entry = (Map.Entry)it.next();
        System.out.print((Integer)entry.getValue());
        while(it.hasNext()){
            entry = (Map.Entry)it.next();
            System.out.print(" "+(Integer)entry.getValue());
        }
    }
 
    private static void pre(int root, int start, int end, int index) {
        if(start > end)return;
        int i = start;
        while(i < end && in.get(i) != post.get(root)){
        	i++;
        }
        level.put(index,post.get(root));//根
        pre(root - 1 - end + i, start, i - 1, 2 * index + 1);//左
        pre(root - 1, i + 1, end, 2 * index + 2);//右
    }
}

9.最长对称字符串

package tianti;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Pta9 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String str = reader.readLine();
        //设置一个NN的二维数组
        int arr_i[][] = new int[str.length()][str.length()];
        int res_count =0;
        for(int i=0;i<str.length();i++){
            for(int j=0;j<=i;j++){
                if(i==j){
                	arr_i[j][i] = 1;
                }else if(i-j==1){
                    if(str.charAt(i)==str.charAt(j)){
                    	arr_i[j][i] = 2;
                    }
                }else if(i-j>=2){
                    if(str.charAt(i)==str.charAt(j)&&arr_i[j+1][i-1]>0){
                    	arr_i[j][i] = arr_i[j+1][i-1]+2;
                    }
                }
            }
        }
        for(int i=0;i<arr_i.length;i++){
            for(int j=0;j<arr_i[0].length;j++){
            	res_count = Math.max(res_count, arr_i[i][j]);
            }
        }
        System.out.println(res_count);
    }
}

总结

如果您需要直接引入到您的项目中,您可以在你的java项目创建一个tianti的package!!然后创建对应的class就okk

以上代码仅供参考,若对您有帮助那便是我最大的荣幸~

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值