PAT基础编程题目集----1

大家有什么疑问可以在评论区评论,一起交流进步啊o(∩_∩)o

厘米转换英尺英寸
import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        int read = scan.nextInt();
        double realFoot = (read * 0.01 / 0.3048);
        int foot = (int) realFoot;
        double realInch = (realFoot - foot)*12;
        int inch = (int) realInch;
        System.out.println(foot + " " + inch);
    }
}

这个题坑在哪里呢?公式里的英尺和英寸整个那个括号里表示的是英尺的值,英寸除以12表示的是英尺的小数部分,我以为英寸是一米等于多少英寸的英寸呢,后来才反应过来公式里的英寸指的是英尺的小数部分转换为英寸来表示!!

然后是几点
import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        String read = scan.nextLine();
        String[] s = read.split(" ");
        String s1 = s[0].substring(0, s[0].length()/2);
        String s2 = s[0].substring(s[0].length()/2);
        int hours = Integer.parseInt(s1);
        int mins = Integer.parseInt(s2)+hours*60;

        int min = Integer.parseInt(s[1]);
        int realMin = (min+mins)%60;
        int realHours = (min+mins)/60;
        System.out.println(realHours*100+realMin);
    }
}

思路就是先把原来的时间转化成分钟,再和新的分钟数运算,最后再换成小时和分钟即可,坑在哪里呢?分钟如果是个位数必须要显示两位才行,也就是零几或者零零,这个时候不要直接用字符串连接,用数学运算的形式即可解决。

逆序的三位数
import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        int read = scan.nextInt();
        System.out.println(read/100+read%100/10*10+read%100%10*100);
    }
}

这个挺简单的,没坑

BCD解密
import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        int read = scan.nextInt();
        System.out.println(Integer.toHexString(read));
    }
}

Integer类自带了一些进制转换的方法,当然你也可以自己根据进制转换规则自己做。

表格输出
public class Main {
    public static void main(String[] args) throws Exception {

        /**
         * ------------------------------------
         * Province      Area(km2)   Pop.(10K)
         * ------------------------------------
         * Anhui         139600.00   6461.00
         * Beijing        16410.54   1180.70
         * Chongqing      82400.00   3144.23
         * Shanghai        6340.50   1360.26
         * Zhejiang      101800.00   4894.00
         * ------------------------------------
         */
        System.out.printf("------------------------------------");
        System.out.println();
        System.out.printf("%-11s%12s%12s","Province","Area(km2)","Pop.(10K)");
        System.out.println();
        System.out.printf("------------------------------------");
        System.out.println();
        System.out.printf("%-11s%12s%10s","Anhui","139600.00","6461.00");
        System.out.println();
        System.out.printf("%-11s%12s%10s","Beijing","16410.54","1180.70");
        System.out.println();
        System.out.printf("%-11s%12s%10s","Chongqing","82400.00","3144.23");
        System.out.println();
        System.out.printf("%-11s%12s%10s","Shanghai","6340.50","1360.26");
        System.out.println();
        System.out.printf("%-11s%12s%10s","Zhejiang","101800.00","4894.00");
        System.out.println();
        System.out.printf("------------------------------------");
    }
}

没试过直接把他的那一段复制过来输出行不行,我估计是可以的,不过这次就不投机取巧了……

混合类型数据格式化输入
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        double d1 = Double.parseDouble(scan.next());
        int i = Integer.parseInt(scan.next());
        String c = scan.next();
        double d2 = Double.parseDouble(scan.next());
        System.out.printf("%s %d %.2f %.2f",c,i,d1,d2);
    }
}

Java好像不能和c似的控制输入格式啊,只能用next全部读取成字符串再输出……

12-24小时制
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String s = scan.next();
        String[] split = s.split(":");
        int hour = Integer.parseInt(split[0]);
        int min = Integer.parseInt(split[1]);
        String ass;
        if (hour>=12){
            ass = "PM";
            if (hour != 12)
                hour %= 12;
        }else {
            ass = "AM";
        }
        System.out.printf("%d:%d %s",hour,min,ass);
    }
}

这个题坑在12小时本身要单独处理,不然取余会变成0点,细心一点就好啦

超速判断
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        System.out.printf("Speed: %d - %s",i,i>60?"Speeding":"OK");
    }
}

这个太简单了……

用天平找小球
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        String[] split = s.split(" ");
        if(split[0].equals(split[1]))
            System.out.print("C");
        else if(split[0].equals(split[2]))
            System.out.print("B");
        else if(split[1].equals(split[2]))
            System.out.print("A");
    }
}

本来想用一些奇淫技巧,后来发现好像没什么特别好的,只好用最基础的方法了……

计算工资
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        String[] split = s.split(" ");
        int year = Integer.parseInt(split[0]);
        int hours = Integer.parseInt(split[1]);
        double salary;
        if (year < 5){
            salary = 30;
        }else {
            salary = 50;
        }
        if (hours >40){
            salary = salary*(hours-40)*1.5+40*salary;
        }else {
            salary = salary * hours;
        }
        System.out.printf("%.2f",salary);
    }
}

基础就是基础,又一个简单题……

你可能感兴趣的:(基础原理-数据结构)