PTA函数题和编程题 您所在的位置:网站首页 裁判测试程序样例什么事意思 PTA函数题和编程题

PTA函数题和编程题

2024-07-12 20:26| 来源: 网络整理| 查看: 265

文章目录

前言:一个java菜鸟想过期中考试的代码整理,仅此而已一、pandas是什么?二、使用步骤 1.引入库2.读入数据总结

一个java菜鸟想过期中考试的代码整理,仅此而已

6-1 学生类 (20 分)

注:indexOf:返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

有一个学生类的结构如下:

class Student { private int no; private String name; private int score; public Student(int _no, String _name, int _score) { no = _no; name = _name; score = _score; } public int getNo() {return no;} public String getName() {return name;} public int getScore() {return score;} public void print(){ System.out.println(no + " "+name+" "+score); } }

请构造main函数完成如下功能: 从键盘中读入三个学生的信息,比较他们的成绩,按照成绩由高到低排列输出

裁判测试程序样例: /*你的代码被嵌在这里*/ class Student { private int no; private String name; private int score; public Student(int _no, String _name, int _score) { no = _no; name = _name; score = _score; } public int getNo() {return no;} public String getName() {return name;} public int getScore() {return score;} public void print(){ System.out.println(no + " "+name+" "+score); } } 我的代码: import java.util.Scanner; public class Main { public static void main(String []args) { Scanner in=new Scanner(System.in); Student a=new Student(in.nextInt(),in.next(),in.nextInt()); Student b=new Student(in.nextInt(),in.next(),in.nextInt()); Student c=new Student(in.nextInt(),in.next(),in.nextInt()); if(a.getScore()>b.getScore()&&a.getScore()>c.getScore()) if(b.getScore()>c.getScore()) {a.print();b.print();c.print();} else {a.print();c.print();b.print();} if(b.getScore()>a.getScore()&&b.getScore()>c.getScore()) if(a.getScore()>c.getScore()) {b.print();a.print();c.print();} else {b.print();c.print();a.print();} if(c.getScore()>a.getScore()&&c.getScore()>b.getScore()) if(a.getScore()>b.getScore()) {c.print();a.print();b.print();} else {c.print();b.print();a.print();} } } 6-2 人口统计 (10 分)

本题运行时要求键盘输入10个人员的信息(每一个人信息包括:姓名,性别,年龄,民族),要求同学实现一个函数,统计民族是“汉族”的人数。

裁判测试程序样例: import java.util.Scanner; public class Main { public static void main(String[] args) { final int HUMANNUM=10; String persons[]=new String[HUMANNUM]; Scanner in=new Scanner(System.in); for(int i=0;i= 0 ){ num ++; } } return num; }  7-1 java基本语法-整数四则运算 (10 分) 

输入2个整数,输出它们的和、差、乘积和准确的商。

import java.util.Scanner; public class Main{ public static void main(String[] args){ try(Scanner input=new Scanner(System.in)){ int a=input.nextInt(); int b=input.nextInt(); System.out.println(a+b); System.out.println(a-b); System.out.println(a*b); System.out.println(a*1.0/b); } } } 7-2 sdut-数据类型-2 应用勾股定理,了解世界灿烂文明 (10 分)

定义:在平面上的一个直角三角形中,两个直角边边长的平方加起来等于斜边长的平方。如果设直角三角形的两条直角边长度分别是和,斜边长度是,那么可以用数学语言表达:

a2+b2=c2

请编程程序,实现如下功能:输入直接三角形的两个直角边的边长,求它们的斜边边长,结果保留2位小数。

提示:在Java中利用Math类的方法——**Math.sqrt()**求平方根。

java.lang.Math.sqrt(double a) 返回一个double值的正平方根。

代码如下(示例):

import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner intput=new Scanner(System.in); while(intput.hasNext()){ //scanner.hasNext()判断系统输入是否结束 double a=intput.nextDouble(); double b=intput.nextDouble(); double c=Math.sqrt(a*a+b*b); System.out.printf("%.2f\n",c);//不可以用println } } }

printf主要是继承了C语言的printf的一些特性,可以进行格式化输出。 print就是一般的标准输出,但是不换行。 println和print基本没什么差别,就是最后会换行。

7-3 sdut-数据类型-3 计算飞行员到最近机场的距离 (10 分)

当飞机上的仪器出故障时,飞行员常常得估计他们到最近机场的距离。他们的方法是利用他们的高度和他们和机场之间的角度的估计。编写一个程序,帮助飞行员进行计算。

程序应以高度和角度作为输入,输出估计距离。公式为:距离distance=高度hight/ tan(degree)。

说明:tan(degree)为角度为degree的正切值。

提示:在JAVA中,Math类的静态方法tan(double degree),用于计算弧度为degree的角度的正切值。

在C语言中,函数名: tan, 头文件:, 函数原型: double tan(double x); 功能: 正切函数,参 数:double x 为要操作的弧度,返回值:返回x弧度的正切值。

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); while(scanner.hasNext()){//不要忘记括号 double h=scanner.nextDouble(); double d=scanner.nextDouble(); System.out.printf("%.2f\n",h/Math.tan(d)); } } } 6-2 设计一个三角形Triangle类 (10 分) 

设计一个三角形Triangle类。这个类包括: 两个名为width和height的double型数据域,它们分别表示三角形的底宽和高。一个为width和height指定初值的构造方法。 一个名为getArea()的方法返回这个三角形的面积。

类名和构造方法: 类名为:Triangle 构造方法为:Triangle ( double w, double h ); 裁判测试程序样例:  import java.util.Scanner; /* 你的代码将被嵌入到这里 */ public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); double w = input.nextDouble(); double h = input.nextDouble(); Triangle myTriangle = new Triangle(w, h); System.out.println(myTriangle.getArea()); input.close(); } } 我的代码:  class Triangle{ double w; double h; public Triangle(double w,double h){ this.w=w; this.h=h; } public double getArea(){ return 0.5*this.w*this.h; } } 6-3 设计一个Circle类(成员变量为私有) (10 分) 

设计一个Circle类,其半径其半径radius设置为私有double型,初始值为1.0。其中设计一个无参的构造方法和一个参数的构造方法,get/set方法分别获取半径值和设置半径值,getArea()和grLengt()分别计算Circle的面积和周长。

我的代码: class Circle{ private double radius=1.0; private final static double P=Math.PI; public Circle() { }//无参 public Circle (double radius){ this radius=radius; }//有参 public double getRadius() { return radius;//获取半径 } public void setRadius(double radius) { this.radius=radius;//设置半径 } public double getArea() { return radius*radius*PI; } public double getLength() { return radius*PI*2; } }

注: 引自Java大数字运算(BigInteger类和BigDecimal类) (biancheng.net)

1.BigInteger 类型的数字范围较 Integer 类型的数字范围要大得多。BigInteger 支持任意精度的整数,也就是说在运算中 BigInteger 类型可以准确地表示任何大小的整数值。

2.要使用 BigInteger 类,首先要创建一个 BigInteger 对象。BigInteger 类提供了很多种构造方法,其中最直接的一种是参数以字符串形式代表要处理的数字。

这个方法语法格式如下:

BigInteger(String val)

这里的 val 是数字十进制的字符串。例如,要将数字 5 转换为 BigInteger 对象,语句如下:

BigInteger bi = new BigInteger("5")

这里数字 5 的双引号是必需的,因为 BigInteger 类构造方法要求参数是字符串类型。 

7-1 大数整除 (10 分)

请编写程序,从键盘输入一个整数n,找出大于long.MAX_VALUE且能被n整除的前3个数字。

输入格式:

输入一个作为除数的整数n,例如: 17

输出格式:

输出大于long.MAX_VALUE且能被n整除的前3个数字,例如下列三个数能被17整除且大于long.MAX_VALUE: 9223372036854775816

9223372036854775833

9223372036854775850

我的程序: import java.math.BigInteger;//BigInteger 类型可以准确地表示任何大小的整数值。 import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner input=new Scanner(System.in); int n=input.nextInt(); int count=0; BigInteger j=new BigInteger(String.valueOf(Long.MAX_VALUE));//将Long.MAX_VALUE转化为字符串,String.valueOf()调用该对象的toString()方法 j=j.add(BigInteger.valueOf(1));//相加(为了找出满足题意的前三个数) while(count0) { jiecheng=jiecheng.multiply(new BigInteger(n+""));//new BigInteger(yuan+"")使用输入数字创建的对象 n--; } System.out.println(jiecheng); in.close(); } }

注:

1.next()和nextLine()两个都是用来接收用户的输入,区别在于:  next()从遇到第一个有效字符(非空格、换行符)开始扫描,遇到第一个分隔符或结束符(空格’ ‘或者换行符 ‘\n’)时结束。 nextLine()则是扫描剩下的所有字符串知道遇到回车为止。

2. str.length( )是string类对象str调用的成员函数,是求字符串的长度。

7-3 标识符命名方式转换:Camel2Snake (10 分) 

在程序设计语言中,标识符都不能包含空格。如果我们需要使用两个或两个以上的单词来命名标识符,目前的工程实际中有两种常用的命名方式:Snake方式和Camel方式。 Snake方式是指单词用小写字母,单词间下划线(“_”)代替空格;Camel方式是指相邻单词首字母用大写表示。

例如,你想定义一个变量表示一个数组数字之和,并且用英文“sum of array”。我们使用Snake方式的变量名为:sum_of_array;用Camel命名方式的变量名为:sumOfArray。

现在请你将一个Camel方式命名的变量,转换成Snake方式命名的变量。

【数据范围】 输入字符串长度不超过255,只包含小写字母和大写字母,第一个和最后一个字符不可能是大写字母,且保证没有两个连续的大写字母出现。

我的程序: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); char[] ch = new char[str.length()];//存在str.length()的长度的char数组里 for(int x = 0;x < str.length();x++) { ch[x] = str.charAt(x);//对数组中的字母进行索引 if(ch[x] >= 'A' && ch[x]


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有