第一次博客
(1)前言
| 知识点 | 题量 | 难度 | |
|---|---|---|---|
| 训练集01 | 多分支if-else语句、一维数组、循环、String使用 | 12道 | 第十题★★其余题★ |
| 训练集02 | 多分支if-else语句、switch语句、循环、String使用、方法 | 9道 | 第八九题★★其余题★ |
| 训练集03 | 多分支if-else语句、一维数组、对象和类、 | 4道 | 第三题★★第四题★★★其余题★ |
总体题目不多,难度不大,循序渐进
(2)设计与分析
训练集01:
7-1基本年利率
- 如果一年以内利率给5折
- 如果三年以内利率为7折
- 如果五年以内利率为100%
- 如果五年以上利率为1.1倍
输入一个年份,计算这个年份下的实际利率是多少?
输入格式:
输入一个整数。
输出格式:
实际利率=x.xx%
输入样例1:
6
输出样例1:
实际利率=8.47%
输入样例2:
-1
输出样例2:
error
import java.util.Scanner;
public class Main {
public static void main(String[ ] args){
double y=7.7;
Scanner scan =new Scanner(System.in);
int flag=0;
int x= scan.nextInt();
if(x>5)y*=1.1;
else if(x>3);
else if(x>1)y*=0.7;
else if (x>=0)y*=0.5;
else flag=1;
if(flag==1)System.out.println("error");
else System.out.println("实际利率="+String.format("%.2f",y)+"%");
}
}
7-2 身体质量指数(BMI)测算
体重是反映和衡量一个人健康状况的重要标志之一,过胖和过瘦都不利于健康,BMI(身体质量指数)计算方法:体重(以千克为单位)除以身高(以米为单位)的平方。中国成人正常的BMI应在18.5-24之间,如果小于18.5为体重不足,如果大于等于24为超重,大于等于28为肥胖。请编写程序,测算身体状态。
输入格式:
两个数值:体重(以千克为单位),身高(以米为单位),数值间以空格分隔。例如:65.5 1.75。
注意:体重的世界纪录是727公斤,身高的世界纪录是2.72米。输入数据上限不得超过纪录,下限不得小于等于0;
输出格式:
输入数值超出范围 :输出“input out of range”。例如:-2 3或者125 5。
BMI小于18.5 :输出“thin”。
BMI大于等于18.5小于24 :输出“fit”。
BMI大于等于24小于28 :输出“overweight”。
BMII大于等于28 :输出“fat”。
import java.util.Scanner;
public class Main {
public static void main(String[ ] args){
Scanner scan =new Scanner(System.in);
double w= scan.nextDouble();
double h= scan.nextDouble();
if(h<=2.72&&h>0&&w<=727&&w>0) {
double bmi = w/h/h;
if (bmi >= 28) System.out.println("fat");
else if (bmi >= 24) System.out.println("overweight");
else if (bmi >= 18.5) System.out.println("fit");
else System.out.println("thin");
}
else System.out.println("input out of range");
}
}
7-3 九九乘法表(双重循环)
打印九九乘法表,乘法表格式如图。

接收用户从键盘输入的一个1到9(含边界)的整数,假设该整数是n,则打印乘法表的前n行。
说明:
(1)用户输入的整数不在1到9这个范围内,则固定输出下面信息:
INPUT ERROR.
(2)两个整数之间的乘号,是使用的大写字母X。同一行的多个乘法结果之间,用制表符\t分开,一行末尾没有多余的制表符。
输入格式:
一个整数n。
输出格式:
乘法表的前n行。
输入样例1:
如用户输入16。
16
输出样例1:
提示用户输入的数据有误。
INPUT ERROR.
输入样例2:
如用户输入3,打印乘法表前3行。
3
输出样例2:
提示用户输入的数据有误。
1X1=1
2X1=2 2X2=4
3X1=3 3X2=6 3X3=9
import java.util.Scanner;
public class Main {
public static void main(String[ ] args){
Scanner scan =new Scanner(System.in);
int n= scan.nextInt();
if(n>=1&&n<=9) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (j != i)
System.out.print(i + "X" + j + "=" + j * i + "\t");
else System.out.print(i + "X" + j + "=" + j * i);
}
System.out.print("\n");
}
}
else System.out.print("INPUT ERROR.");
}
}
7-4 快递运费
有一快递公司,运费计算规则如下:
首重(1.0kg)12.0元,续重2.0元/kg
首重(20.0kg)39.0元,续重1.9元/kg
首重(60.0kg)115.0元,续重1.3元/kg
输入物体的重量,计算应付的运费,四舍五入保留整数。
注:建议采用int(x+0.5)
输入格式:
输入物体的重量
输出格式:
输出运费,四舍五入保留整数
输入样例1:
在这里给出一组输入。例如:
2
输出样例1:
在这里给出相应的输出。例如:
14
输入样例2:
在这里给出一组输入。例如:
21
输出样例2:
在这里给出相应的输出。例如:
41
import java.util.Scanner;
public class Main {
public static void main(String[ ] args){
Scanner scan =new Scanner(System.in);
double n= scan.nextDouble();
double sum=0;
if(n>=60)sum=115+(n-60)*1.3;
else if(n>=20)sum=39+(n-20)*1.9;
else sum=12+(n-1)*2;
sum=Math.round(sum);
System.out.print(String.format("%.0f",sum));
}
}
7-5 去掉重复的字符
输入一个字符串,输出将其中重复出现的字符去掉后的字符串
输入格式:
一行字符串
输出格式:
去掉重复字符后的字符串
输入样例:
在这里给出一组输入。例如:
ofiweirowqrigu
输出样例:
在这里给出相应的输出。例如:
ofiwerqgu
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[ ] args){
Scanner scan =new Scanner(System.in);
String s=scan.next(),s1;
int a[]=new int[1000];
Arrays.fill(a,-1);
int cnt=0;
for (int i=0;i<s.length();i++){
for (int j=i+1;j<s.length();j++){
if(s.charAt(i)==s.charAt(j)){
a[cnt]=j;cnt++;//标记了得 不能输出
}
}
}
for (int i=0;i<s.length();i++){
int flag=0;
for (int j=0;j<cnt;j++){
if(i==a[j])flag=1;//no
}
if(flag!=1)System.out.print(s.charAt(i));
}
// System.out.print(cnt);
}
}
7-6 统计一个子串在整串中出现的次数
编写一个程序,统计一个子串在整串中出现的次数,例如子串“nba”在整串“nbaabcdefnbaxyzmba”中出现的次数为2。要求使用String或者StringBuffer类的常用方法来计算出现的次数。
请注意:含有main方法的类(class)的名字必须命名为Main,否则调试不成功。
输入格式:
输入两行,每行输入一个字符串。第一个当作整串,第二个当作子串。每个字符串的中间不要出现换行符(Enter)、空格、制表符(Tab)。
输出格式:
输出子串在整串中出现的次数,结果是一个大于或等于0的整数。
输入样例1:
在这里给出一组输入。例如:
吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮。
葡萄
输出样例1:
在这里给出相应的输出。例如:
4
输入样例2:
在这里给出一组输入。例如:
abcdefghijklmn
cdf
输出样例2:
在这里给出相应的输出。例如:
0
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[ ] args){
Scanner scan =new Scanner(System.in);
String s= scan.nextLine();
String s2= scan.nextLine();
int d=0;
int cnt=0;
while ((d=s.indexOf(s2))!=-1){
s=s.substring(d+s2.length());
cnt++;
}
System.out.print(cnt);
}
}
7-7 有重复的数据
在一大堆数据中找出重复的是一件经常要做的事情。现在,我们要处理许多整数,在这些整数中,可能存在重复的数据。
你要写一个程序来做这件事情,读入数据,检查是否有重复的数据。如果有,输出“YES”这三个字母;如果没有,则输出“NO”。
输入格式:
你的程序首先会读到一个正整数n,n∈[1,100000],然后是n个整数。
输出格式:
如果这些整数中存在重复的,就输出:
YES
否则,就输出:
NO
输入样例:
5
1 2 3 1 4
输出样例:
YES
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[ ] args){
Scanner cin =new Scanner(System.in);
int n= cin.nextInt();
cin.nextLine();
String s= cin.nextLine();
// cin.close();
String[] s1=s.split(" ");
Set<String> st = new HashSet<String>();
for(int i = 0; i < s1.length; ++ i) {
st.add(s1 [i]);
if( st.size() != i + 1) {
System.out.println("YES");
return;
}
}
System.out.println("NO");
}
}
7-8 从一个字符串中移除包含在另一个字符串中的字符
从一个字符串中移除包含在另一个字符串中的字符。输入两行字符串,将第一行字符串中包括第二行字符串中的所有字母去除。输出去除后保留的字符串。
输入格式:
第一行输入一个字符串
第二行输入一个字符串
输出格式:
输出移除后的字符串
输入样例:
在这里给出一组输入。例如:
abcdefghijklmnopqrstuvwxyz
secret
输出样例:
在这里给出相应的输出。例如:
abdfghijklmnopquvwxyz
import java.util.Scanner;
public class Main {
public static void main(String[ ] args){
Scanner cin =new Scanner(System.in);
String s1=cin.nextLine();
String s2=cin.nextLine();
for(int i=0;i<s1.length();i++){
int flag=0;
for (int j=0;j<s2.length();j++){
if(s1.charAt(i)==s2.charAt(j)){flag=1;}
}
if(flag!=1)System.out.print(s1.charAt(i));
}
}
}
7-9 Prime Numbers
Your program reads two natural numbers m and n in, and prints out the sum of all prime numbers within [m,n], where 1⩽m≤n⩽104.
Input Format:
Two positive whole numbers.
Output Format:
A number which is the sum of all the prime numbers within [m, n].
Sample Input:
10 100
Sample Output:
1043
import java.util.Scanner;
public class Main {
public static void main(String[ ] args){
Scanner cin =new Scanner(System.in);
int m= cin.nextInt();
int n= cin.nextInt();
int sum=0;
for (int i=m;i<=n;i++){
int flag=0;
if(i==1)flag=1;
for (int j=2;j<=Math.sqrt(i);j++){
if(i%j==0){flag=1;break;}
}
if(flag==0)sum+=i;
}
System.out.print(sum);
}
}
7-10 GPS数据处理
NMEA-0183协议是为了在不同的GPS(全球定位系统)导航设备中建立统一的BTCM(海事无线电技术委员会)标准,由美国国家海洋电子协会(NMEA-The National Marine Electronics Associa-tion)制定的一套通讯协议。GPS接收机根据NMEA-0183协议的标准规范,将位置、速度等信息通过串口传送到PC机、PDA等设备。
NMEA-0183协议是GPS接收机应当遵守的标准协议,也是目前GPS接收机上使用最广泛的协议,大多数常见的GPS接收机、GPS数据处理软件、导航软件都遵守或者至少兼容这个协议。
NMEA-0183协议定义的语句非常多,但是常用的或者说兼容性最广的语句只有$GPGGA、$GPGSA、$GPGSV、$GPRMC、$GPVTG、$GPGLL等。
其中$GPRMC语句的格式如下:
$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
这里整条语句是一个文本行,行中以逗号“,”隔开各个字段,每个字段的大小(长度)不一,这里的示例只是一种可能,并不能认为字段的大小就如上述例句一样。
- 字段0:
$GPRMC,语句ID,表明该语句为Recommended Minimum Specific GPS/TRANSIT Data(RMC)推荐最小定位信息 - 字段1:UTC时间,hhmmss.sss格式
- 字段2:状态,A=定位,V=未定位
- 字段3:纬度ddmm.mmmm,度分格式(前导位数不足则补0)
- 字段4:纬度N(北纬)或S(南纬)
- 字段5:经度dddmm.mmmm,度分格式(前导位数不足则补0)
- 字段6:经度E(东经)或W(西经)
- 字段7:速度,节,Knots
- 字段8:方位角,度
- 字段9:UTC日期,DDMMYY格式
- 字段10:磁偏角,(000 - 180)度(前导位数不足则补0)
- 字段11:磁偏角方向,E=东W=西
- 字段16:校验值
这里,*为校验和识别符,其后面的两位数为校验和,代表了$和*之间所有字符(不包括这两个字符)的异或值的十六进制值。上面这条例句的校验和是十六进制的50,也就是十进制的80。
提示:^运算符的作用是异或。将$和*之间所有的字符做^运算(第一个字符和第二个字符异或,结果再和第三个字符异或,依此类推)之后的值对65536取余后的结果,应该和*后面的两个十六进制数字的值相等,否则的话说明这条语句在传输中发生了错误。注意这个十六进制值中是会出现A-F的大写字母的。另外,在Java语言中,如果你需要的话,可以用Integer.parseInt(s)从String变量s中得到其所表达的整数数字;而Integer.parseInt(s, 16)从String变量s中得到其所表达的十六进制数字
现在,你的程序要读入一系列GPS输出,其中包含$GPRMC,也包含其他语句。在数据的最后,有一行单独的
END
表示数据的结束。
你的程序要从中找出$GPRMC语句,计算校验和,找出其中校验正确,并且字段2表示已定位的语句,从中计算出时间,换算成北京时间。一次数据中会包含多条$GPRMC语句,以最后一条语句得到的北京时间作为结果输出。
你的程序一定会读到一条有效的$GPRMC语句。
输入格式:
多条GPS语句,每条均以回车换行结束。最后一行是END三个大写字母。
输出格式:
6位数时间,表达为:
hh:mm:ss
其中,hh是两位数的小时,不足两位时前面补0;mm是两位数的分钟,不足两位时前面补0;ss是两位数的秒,不足两位时前面补0。
输入样例:
$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
END
输出样例:
10:48:13
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int hh = 0, mm = 0, ss = 0;
String s = in.nextLine();
while (!s.equals("END")) {
int idx = s.indexOf(',');
idx = s.indexOf(',', idx + 1);
if (s.substring(0, 6).compareTo("$GPRMC") == 0 && s.charAt(idx + 1) == 'A') {
int checkNum = s.charAt(1);
for (int i = 2; s.charAt(i) != '*'; i++) {
checkNum ^= s.charAt(i);
}
checkNum %= 65536;
int givenNum = Integer.parseInt(s.substring(s.length() - 2, s.length()), 16);
if (checkNum == givenNum) {
hh = (s.charAt(7) - '0') * 10 + (s.charAt(8) - '0');
mm = (s.charAt(9) - '0') * 10 + (s.charAt(10) - '0');
ss = (s.charAt(11) - '0') * 10 + (s.charAt(12) - '0');
hh = (hh + 8) % 24;
}
}
s = in.nextLine();
}
if (hh < 10)
System.out.print(0);
System.out.print(hh + ":");
if (mm < 10)
System.out.print(0);
System.out.print(mm + ":");
if (ss < 10)
System.out.print(0);
System.out.print(ss);
}
}
7-11 求定积分
一、定积分的概念




根据以上理论,求定积分:

输入格式:
输入定积分下限,定积分上限,区间[a,b]被分割的份数。
输出格式:
输出定积分的值,保留4位小数。
输入样例:
1 2 100
输出样例:
2.3334
import java.util.Scanner;
public class Main {
public static void main(String[ ] args){
Scanner cin =new Scanner(System.in);
double a = cin.nextDouble();
double b = cin.nextDouble();
double n = cin.nextDouble();
double sum=0;
double d=(b-a)/n;
for (double i=1;i<=n;i++){
double x=(a+(i-1)*d+a+i*d)/2;
sum=sum+x*x;
}
// System.out.print(sum/n);
System.out.printf("%.4f",sum/n);
}
}
7-12 列出最简真分数序列*
按递增顺序依次列出所有分母为N(10 <= N <= 40),分子小于N的最简分数。
输入格式:
分母 N。
输出格式:
分数之间用逗号分开(含最末逗号)
输入样例:
10
输出样例:
1/10,3/10,7/10,9/10,
import java.util.Scanner;
public class Main {
public static void main(String[ ] args){
Scanner cin =new Scanner(System.in);
int n=cin.nextInt();
for(int i=1;i<n;i++){
if(gcd(i ,n)==1 )System.out.print(i+"/"+n+',');
}
}
public static int gcd(int x,int y){
int t=y;
while (t!=0){
t=x%y;
x=y;
y=t;
}
return x;
}
}
训练集02:
7-1 长度质量计量单位换算
长度、质量的计量有多重不同的计算体系,有标准的国际单位制:千克与米,也有各个国家自己的计量方法如:磅、英寸;1磅等于0.45359237千克,1英寸等于0.0254米,请编写程序实现国际单位制与英制之间的换算。
输入格式:
两个浮点数,以空格分隔,第一个是质量(以千克为单位)、第二个是长度(以米为单位)。例如:0.45359237 0.0254。
输出格式:
两个浮点数,以空格分隔,第一个是质量(以磅为单位)、第二个是长度(以英寸为单位)。例如:1.0 1.0。
输入样例:
在这里给出一组输入。例如:
0.45359237 0.0254
输出样例:
在这里给出相应的输出。例如:
1.0 1.0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
double kg = cin.nextDouble();
double m = cin.nextDouble();
double pound = kg / 0.45359237;
double inch = m / 0.0254;
System.out.printf((float)pound+" "+(float)inch);
}
}
7-2 奇数求和
计算一个数列中所有奇数的和。
输入格式:
十个整数,以空格分隔。例如:1 2 3 4 5 6 7 8 9 0。
输出格式:
输入数列中所有奇数之和。例如:25。
输入样例:
在这里给出一组输入。例如:
1 2 3 4 5 6 7 8 9 0
输出样例:
在这里给出相应的输出。例如:
25
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin =new Scanner(System.in);
int sum=0;
int []a;
a=new int [10];
for (int i=0;i<10;i++){
a[i]=cin.nextInt();
if(a[i]%2!=0)sum+=a[i];
}
System.out.print(sum);
}
}
7-3 房产税费计算2022
房屋交易在日常生活中非常常见的事情,房屋交易时要额外支付各种税费,按2022年房产交易新政策的规定买房人应缴纳税费包括:
1、契税:首次购房评估额90平(含)内1%、90平-144平(含)内1.5%,超过144平或非首 次3%,买方缴纳。
2、印花税:房款的0.05%。
3、交易费:3元/平方米。
4、测绘费:1.36元/平方米。
5、权属登记费及取证费:一般情况是在200元内。
输入格式:
四个数据,以空格分隔:
1、第几次购房(整数)
2、房款(整数/单位万元)
3、评估价(整数/单位万元)
4、房屋面积(浮点数/单位平方米)。
例如:1 100 100 90。
输出格式:
契税、印花税、交易费、测绘费(以元为单位),以空格分隔。例如:10000.0 500.0 270.0 122.4
输入样例:
在这里给出一组输入。例如:
1 100 100 90
输出样例:
在这里给出相应的输出。例如:
10000.0 500.0 270.0 122.4
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin =new Scanner(System.in);
int n=cin.nextInt() ;
int m=cin.nextInt();
int g=cin.nextInt();
float s=cin.nextFloat( );
double a=0,b=m*5,c=3*s;
float d=1.36f*s;
if(n>1){
a=g*10000*0.03;
}
else if(s>144){
a=g*10000*0.03;
}
else if(s>90){
a=g*10000*0.015;
}
else a=g*10000*0.01;
System.out.print(a+" "+b+" "+c+" "+d);
}
}
7-4 游戏角色选择
一款网游中包括4个种族:人类、精灵、兽人、暗精灵,每个种族包含三种角色:战士、法师、射手。玩家新建人物时需要选择种族和角色。请编写角色选择程序。
输入格式:
两个整数:游戏种族、角色的选项,以空格分隔。例如:1 2。
种族选项设定为:1、人类 2、精灵 3、兽人 4、暗精灵
角色选项设定为:1、战士 2、法师 3、射手
输出格式:
所选择的种族、角色的名称,以空格分隔。例如:人类 法师
若输入数值超出选项范围,输出“Wrong Format”
输入样例1:
在这里给出一组输入。例如:
1 2
输出样例1:
在这里给出相应的输出。例如:
人类 法师
输入样例2:
在这里给出一组输入。例如:
1 6
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin =new Scanner(System.in);
int a=cin.nextInt();
int b=cin.nextInt();
String x="0",y="0";
if(a<1||a>4||b<1||b>3){
System.out.print("Wrong Format");
}
else {
switch (a) {
case 1:
x = "人类";
break;
case 2:
x = "精灵";
break;
case 3:
x = "兽人";
break;
case 4:
x = "暗精灵";
break;
default:
break;
}
switch (b) {
case 1:
y = "战士";
break;
case 2:
y = "法师";
break;
case 3:
y = "射手";
break;
default:
break;
}
System.out .print(x+" "+y);
}
}
}
7-5 学号识别
学校的学号由8位数字组成,前两位是入学年份(省略了20);第3、4位是学院编号,01代表材料学院,02代表机械学院,03代表外语学院,20代表软件学院;第5、6位是学院内部班级编号,最后两位是班级内部学号。如:18011103,入学年份是2018年,材料学院,11班,03号
输入格式:
8位数字组成的学号。例如:18011103
注意:输入学号不是8位或者学院编号不是01、02、03、20其中之一,属于非法输入
输出格式:
学号每一项的完整说明。例如:
入学年份:2018年
学院:材料学院
班级:11
学号:03
注意:如非法输入,输出“Wrong Format"
输入样例:
在这里给出一组输入。例如:
18011103
输出样例:
在这里给出相应的输出。例如:
入学年份:2018年
学院:材料学院
班级:11
学号:03
输入样例1:
在这里给出一组输入。例如:
18013
输出样例1:
在这里给出相应的输出。例如:
Wrong Format
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin =new Scanner(System.in);
String s=cin.next() ;
String xueyuan ="0" ;
int flag=0;
if(s.length()==8){
if(s.charAt(0)>='0'&&s.charAt(0)<='9'&&s.charAt(1)>='0'&&s.charAt(1)<='9') {
if (s.substring(2, 4).equals("01") || s.substring(2, 4).equals("02") || s.substring(2, 4).equals("03") || s.substring(2, 4).equals("20")) {
switch (s.substring(2,4)){
case "01":xueyuan="材料学院";break;
case "02":xueyuan="机械学院";break;
case "03":xueyuan="外语学院";break;
case "20":xueyuan="软件学院";break;
default:break;
}
if(Character.isDigit(s.charAt(4))&&Character.isDigit(s.charAt(5))&&Character.isDigit(s.charAt(6))&&Character.isDigit(s.charAt(7))){
flag =1;
System.out.println("入学年份:20"+s.charAt(0)+s.charAt(1)+"年");
System.out.println("学院:"+xueyuan);
System.out.println("班级:"+s.substring(4,6));
System.out.println("学号:"+s.substring(6));
}
}
}
}
if(flag==0)System.out.println("Wrong Format");
}
}
7-6 巴比伦法求平方根近似值
巴比伦法求n的近似值可以用以下公式:
nextGuess = (lastGuess+n/lastGuess)/2
程序初始运行时lastGuess可赋予一个最初的猜测值。当由公式求得的nextGuess和lastGuess相差较大时,把nextGuess的值赋给lastGuess,继续以上过程,直至nextGuess和lastGuess几乎相同,此时lastGuess或者nextGuess就是平方根的近似值。
本题要求:nextGuess和lastGuess的差值小于0.00001时认为两者几乎相同
输入格式:
1、两个浮点数,以空格分隔,第一个是n,第二个是lastGuess最初的猜测值。例如:2 1。
2、若输入的两个数中包含负数或者lastGuess初始输入为0,认定为非法输入
输出格式:
1、输出n的平方根近似值:lastGuess。例如:1.4142157
2、非法输入时输出:"Wrong Format"
输入样例:
在这里给出一组输入。例如:
2 1
输出样例:
在这里给出相应的输出。例如:
1.4142157
输入样例1:
在这里给出一组输入1。例如:
2 -1
输出样例:
在这里给出相应的输出。例如:
Wrong Format
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
double eps=0.00001;
float n= cin.nextFloat();
float lastGuess= cin.nextFloat();
if(n<0||lastGuess<=0){
System.out.print("Wrong Format");return;
}
float nextGuess = (lastGuess+n/lastGuess)/2;
while (Math.abs(nextGuess-lastGuess)>=eps) {
lastGuess = nextGuess;
nextGuess = (lastGuess+n/lastGuess)/2;
}
System.out.print((float)lastGuess);
}
}
7-7 二进制数值提取
在一个字符串中提取出其中的二进制数值序列,。
输入格式:
一个由0、1构成的序列,以-1为结束符,非0、1字符视为正常输入,但忽略不计,未包含结束符的序列视为非法输入。例如:abc00aj014421-1
输出格式:
将输入的序列去掉非0、1字符以及结尾符的数据内容,
注:结束符-1之后的0\1字符忽略不计。
例如:00011。
输入样例:
在这里给出一组输入。例如:
abc00aj014421-1
输出样例:
在这里给出相应的输出。例如:
00011
输入样例1:
在这里给出一组输入。例如:
a0571-1k001y
输出样例1:
在这里给出相应的输出。例如:
01
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String s=cin.nextLine() ;
int x=s.indexOf("-1");
if(x!=-1) {
s = s.substring(0, x);
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '0' || s.charAt(i) == '1') System.out.print(s.charAt(i));
}
}
else System.out.print("Wrong Format");
}
}
7-8 判断三角形类型
输入三角形三条边,判断该三角形为什么类型的三角形。
输入格式:
在一行中输入三角形的三条边的值(实型数),可以用一个或多个空格或回车分隔,其中三条边的取值范围均为[1,200]。
输出格式:
(1)如果输入数据非法,则输出“Wrong Format”;
(2)如果输入数据合法,但三条边不能构成三角形,则输出“Not a triangle”;
(3)如果输入数据合法且能够成等边三角形,则输出“Equilateral triangle”;
(3)如果输入数据合法且能够成等腰直角三角形,则输出“Isosceles right-angled triangle”;
(5)如果输入数据合法且能够成等腰三角形,则输出“Isosceles triangle”;
(6)如果输入数据合法且能够成直角三角形,则输出“Right-angled triangle”;
(7)如果输入数据合法且能够成一般三角形,则输出“General triangle”。
输入样例1:
在这里给出一组输入。例如:
50 50 50.0
输出样例1:
在这里给出相应的输出。例如:
Equilateral triangle
输入样例2:
在这里给出一组输入。例如:
60.2 60.2 80.56
输出样例2:
在这里给出相应的输出。例如:
Isosceles triangle
输入样例3:
在这里给出一组输入。例如:
0.5 20.5 80
输出样例3:
在这里给出相应的输出。例如:
Wrong Format
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a, b, c;
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
if (a <= 0 || b <= 0 || c <= 0 || a > 200 || b > 200 || c > 200) {
System.out.println("Wrong Format");
return;
}
if (a + b <= c || b + c <= a || a + c <= b) {
System.out.println("Not a triangle");
return;
}
if (a == b && b == c) {
System.out.println("Equilateral triangle");
} else if (a == b || b == c || a == c) {
if (a * a + b * b -c*c<1e-6|| b * b + c * c-a*a <1e-6 || a * a + c * c-b*b<1e-6) {
System.out.println("Isosceles right-angled triangle");
} else {
System.out.println("Isosceles triangle");
}
} else if (a * a + b * b == c * c || b * b + c * c == a * a || a * a + c * c == b * b) {
System.out.println("Right-angled triangle");
} else {
System.out.println("General triangle");
}
}
}
7-9 求下一天
输入年月日的值(均为整型数),输出该日期的下一天。 其中:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。
注意:不允许使用Java中和日期相关的类和方法。
要求:Main类中必须含有如下方法,签名如下:
public static void main(String[] args);//主方法
public static boolean isLeapYear(int year) ;//判断year是否为闰年,返回boolean类型
public static boolean checkInputValidity(int year,int month,int day);//判断输入日期是否合法,返回布尔值
public static void nextDate(int year,int month,int day) ; //求输入日期的下一天
输入格式:
在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。
输出格式:
- 当输入数据非法及输入日期不存在时,输出“Wrong Format”;
- 当输入日期合法,输出下一天,格式如下:Next date is:年-月-日
输入样例1:
在这里给出一组输入。例如:
2020 3 10
输出样例1:
在这里给出相应的输出。例如:
Next date is:2020-3-11
输入样例2:
在这里给出一组输入。例如:
2025 2 10
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year, month, day;
year = sc.nextInt();
month = sc.nextInt();
day = sc.nextInt();
if (!checkInputValidity(year, month, day)) {
System.out.println("Wrong Format");
return;
}
nextDate(year, month, day);
}
public static boolean isLeapYear(int year) {
if (year %4 == 0) {
return year %100 != 0;
} else {
return year %400 == 0;
}
}
public static boolean checkInputValidity(int year, int month, int day) {
if (year < 1820 || year > 2020) {
return false;
}
if (month < 1 || month > 12) {
return false;
}
if (day < 1 || day > 31) {
return false;
}
if (month == 2) {
if (isLeapYear(year)) {
return day <= 29;
} else {
return day <= 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return day <= 30;
} else {
return true;
}
}
public static void nextDate(int year, int month, int day) {
int nextYear = year;
int nextMonth = month;
int nextDay = day + 1;
if (nextDay > 31) {
nextDay = 1;
nextMonth++;
}
if (nextMonth > 12) {
nextMonth = 1;
nextYear++;
}
while (!checkInputValidity(nextYear, nextMonth, nextDay)) {
nextDay++;
if (nextDay > 31) {
nextDay = 1;
nextMonth++;
}
if (nextMonth > 12) {
nextMonth = 1;
nextYear++;
}
}
System.out.println("Next date is:" + nextYear + "-" + nextMonth + "-" + nextDay);
}
}
训练集03
7-1 创建圆形类
编写一个圆形类Circle,一个私有实型属性半径,要求写出带参数构造方法、无参构造方法、属性的getter、setter方法以及求面积、输出数据等方法,具体格式见输入、输出样例。
输入格式:
在一行内输入一个实型数作为圆的半径(半径数值要求不能为负值)
输出格式:
-
如果半径输入非法,则直接输出
Wrong Format -
如果输入半径合法,则输出如下两行数据
The circle's radius is:圆的半径值The circle's area is:圆的面积值要求输出数据均保留2位小数,PI的取值使用Math.PI。
输入样例:
在这里给出一组输入。例如:
-5
输出样例:
在这里给出相应的输出。例如:
Wrong Format
输入样例:
在这里给出一组输入。例如:
2.5
输出样例:
在这里给出相应的输出。例如:
The circle's radius is:2.50
The circle's area is:19.63
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
class Circle {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius() {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public void print() {
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("The circle's radius is:" + df.format(radius));
System.out.println("The circle's area is:" + df.format(getArea()));
}
}
double radius = sc.nextDouble();
if (radius<0){
System.out.println("Wrong Format");
}
else {
Circle c = new Circle(radius);
c.print();
}
}
}
7-2 创建账户类Account
设计一个名称为Account的类,具体包括:
- id:账号,私有属性,整型,默认值为0;
- balance:余额,私有属性,实型,默认值为0;
- annualInterestRate:当前利率,私有属性,实型,默认值为0,假设所有帐户均有相同的利率;
- dateCreated:账户开户时间,私有属性,LocalDate类型,默认为2020年7月31日;
- 一个能创建默认账户的无参构造方法;
- 一个能创建带特定id和初始余额的账户的构造方法;
- id、balance、annualInterstRate的getter及setter方法;
- dateCreated的getter方法;
- 一个名为getMonthlyInterestRate()的方法返回月利率(月利率计算公式:余额*(年利率/1200));
- 一个名为withDraw的方法从账户提取特定数额,当提取数额大于余额或为负数系统返回
WithDraw Amount Wrong提示; - 一个名为deposit的方法向账户存储特定数额,当存储数额大于20000元或为负数系统返回
Deposit Amount Wrong提示。
编写一个测试程序:
- 创建一个账户,其账户id、余额及利率分别有键盘输入,账户开户时间取系统当前时间;
- 输入取钱金额,系统进行取钱操作,如果取钱金额有误,则输出提示信息后系统继续运行;
- 输入存钱金额,系统进行存钱操作,如果存钱金额有误,则输出提示信息后系统继续运行;
- 系统输出,以如下格式分别输出该账户余额、月利息以及开户日期(输出实型数均保留两位小数)
输入格式:
在一行内分别输入账户id、初始余额、当前利率、提取金额、存储金额,数据间采用一个或多个空格分隔。
输出格式:
共分三行输出,分别为约、计算的月利息以及开户日期,格式如下:
-
`The Account'balance:余额` -
The Monthly interest:月利息 -
`The Account'dateCreated:年-月-日`
输入样例1:
在这里给出一组输入。例如:
1122 20000 0.045 800 600
输出样例1:
在这里给出相应的输出。例如:
The Account'balance:19800.00
The Monthly interest:0.74
The Account'dateCreated:2020-07-31
输入样例2:
在这里给出一组输入。例如:
1122 20000 0.045 8000 30000
输出样例2:
在这里给出相应的输出。例如:
Deposit Amount Wrong
The Account'balance:12000.00
The Monthly interest:0.45
The Account'dateCreated:2020-07-31
import java.time.LocalDate;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();
double balance = sc.nextDouble();
double annuallnterestRate = sc.nextDouble();
Account account = new Account(id, balance);
account.setAnnuallnterestRate(annuallnterestRate);
int withDrawAmount = sc.nextInt();
int depositAmount = sc.nextInt();
account.withDraw(withDrawAmount);
account.deposit(depositAmount);
account.print();
}
}
class Account{
private int id;
private double balance ;
private double annualInterestRate;
private LocalDate dateCreated=LocalDate.of(2020, 7, 31);
public Account() {
}
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return this.balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return this.annualInterestRate;
}
public void setAnnuallnterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public LocalDate getDateCreated() {
return this.dateCreated;
}
public double getMonthlyInterestRate() {
return this.balance * (1.0*annualInterestRate / 1200);
}
public void withDraw(int amount) {
if (amount >balance || amount < 0) {
System.out.println("WithDraw Amount Wrong");
} else {
this.balance -= amount;
}
}
public void deposit(int amount ){
if(amount>20000||amount<0){
System.out.println("Deposit Amount Wrong");
} else {
this.balance+=amount;
}
}
public void print(){
System.out.printf("The Account'balance:%.2f\n", balance);
System.out.printf("The Monthly interest:%.2f\n", getMonthlyInterestRate());
System.out.printf("The Account'dateCreated:2020-07-31");
}
}
7-3 定义日期类
定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。
注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。
要求:Date类结构如下图所示:

输入格式:
在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。
输出格式:
- 当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;
- 当输入日期合法,输出下一天,格式如下:Next day is:年-月-日
输入样例1:
在这里给出一组输入。例如:
1912 12 25
输出样例1:
在这里给出相应的输出。例如:
Next day is:1912-12-26
输入样例2:
在这里给出一组输入。例如:
2001 2 30
输出样例2:
在这里给出相应的输出。例如:
Date Format is Wrong
分析
定义Date类,包含所述方法,与上一训练集题目类似,不过采用类得方式实现。难点在于getNextDate方法。定义一个数组用来表示对应月份得天数,先判断年份是否为闰年,看是否要改变a[2]即二月份天数。然后day+1判断月数和年是否要进位。
类图:

代码实现:
import java.time.LocalDate;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
Date myDate = new Date(year,month,day);
if (!myDate.checkInputValidity(year, month, day)) {
System.out.println("Date Format is Wrong");
return;
}
myDate.getNextDate(year,month,day);
}
}
class Date{
private int year;
private int month;
private int day;
public Date(){
}
public Date(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public int getYear(){
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public boolean isLeapYear(int year){
if(year%4== 0&&year%100!=0 ||year%400==0) {
return true;
}else {
return false;
}
}
public boolean checkInputValidity(int year, int month, int day){
if (year < 1900 || year > 2000) {
return false;
}
if (month < 1 || month > 12) {
return false;
}
if (day < 1 || day > 31) {
return false;
}
if (month == 2) {
if (isLeapYear(year)) {
return day <= 29;
} else {
return day <= 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return day <= 30;
} else {
return true;
}
}
public void getNextDate(int year, int month, int day){
int []ar=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year)){
ar[2]=29;
}
int nextYear = year;
int nextMonth = month;
int nextDay = day + 1;
if (nextDay > ar[month]) {
nextDay = 1;
nextMonth++;
if (nextMonth > 12) {
nextMonth = 1;
nextYear++;
}
}
System.out.println("Next day is:" + nextYear + "-" + nextMonth + "-" + nextDay);
}
}
7-4 日期类设计
参考题目3和日期相关的程序,设计一个类DateUtil,该类有三个私有属性year、month、day(均为整型数),其中,year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 除了创建该类的构造方法、属性的getter及setter方法外,需要编写如下方法:
public boolean checkInputValidity();//检测输入的年、月、日是否合法
public boolean isLeapYear(int year);//判断year是否为闰年
public DateUtil getNextNDays(int n);//取得year-month-day的下n天日期
public DateUtil getPreviousNDays(int n);//取得year-month-day的前n天日期
public boolean compareDates(DateUtil date);//比较当前日期与date的大小(先后)
public boolean equalTwoDates(DateUtil date);//判断两个日期是否相等
public int getDaysofDates(DateUtil date);//求当前日期与date之间相差的天数
public String showDate();//以“year-month-day”格式返回日期值
应用程序共测试三个功能:
- 求下n天
- 求前n天
- 求两个日期相差的天数
注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)
程序主方法如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;
int choice = input.nextInt();
if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());
DateUtil date = new DateUtil(year, month, day);
if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}
m = input.nextInt();
if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
} else if (choice == 2) { // test getPreviousNDays method
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());
DateUtil date = new DateUtil(year, month, day);
if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}
n = input.nextInt();
if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
System.out.print(
date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
} else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());
int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());
DateUtil fromDate = new DateUtil(year, month, day);
DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println("The days between " + fromDate.showDate() +
" and " + toDate.showDate() + " are:"
+ fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}
输入格式:
有三种输入方式(以输入的第一个数字划分[1,3]):
- 1 year month day n //测试输入日期的下n天
- 2 year month day n //测试输入日期的前n天
- 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
输出格式:
-
当输入有误时,输出格式如下:
Wrong Format -
当第一个数字为1且输入均有效,输出格式如下:
year1-month1-day1 next n days is:year2-month2-day2 -
当第一个数字为2且输入均有效,输出格式如下:
year1-month1-day1 previous n days is:year2-month2-day2 -
当第一个数字为3且输入均有效,输出格式如下:
The days between year1-month1-day1 and year2-month2-day2 are:值
输入样例1:
在这里给出一组输入。例如:
3 2014 2 14 2020 6 14
输出样例1:
在这里给出相应的输出。例如:
The days between 2014-2-14 and 2020-6-14 are:2312
输入样例2:
在这里给出一组输入。例如:
2 1834 2 17 7821
输出样例2:
在这里给出相应的输出。例如:
1834-2-17 previous 7821 days is:1812-9-19
输入样例3:
在这里给出一组输入。例如:
1 1999 3 28 6543
输出样例3:
在这里给出相应的输出。例如:
1999-3-28 next 6543 days is:2017-2-24
输入样例4:
在这里给出一组输入。例如:
0 2000 5 12 30
输出样例4:
在这里给出相应的输出。例如:
Wrong Format
分析
根据输入分四种况
| 1 | 2 | 3 | 其他 |
|---|---|---|---|
| 求下n天 | 求前n天 | 求两个日期相差的天数 | Wrong Format |
重点在于3种情况对应的方法:
1.求下n天:nextDay =day+n 当nextDay>对应月份的天数 月进位1,nextDay-=当前月份天数,当月份大于12,年进位1,月份-=12,再在每一次循环中判断是否是闰年
2.求前n天:nextDay =day-n 当nextDay<1 nextDay+=对应上一个月的天数,月退位1,当月份小于1,年份退位,月份-=12,再在每一次循环中判断是否是闰年
3.求两个日期相差天数:先写一个判断这两个日期是否为同一日期的方法,即年月日是否全部相等。如果不相等,根据compareDates方法,判断哪个日期大哪个日期小,返回日期小的。然后循环,让日期小的一天天的相加,直到两个日期相等,统计次数即相差天数
类图

(3)踩坑心得
训练集01:
7-10 要仔细读题,理解题意,不要鲁莽
7-11 按照题意理论 得到的结果会有浮点误差 导致的情况就是 自测样例 输出的是2.333与预期2.334不符合 但是提交答案却能过
训练集02:
7-1 输出时要转化为float的形式
7-3 7-6 也存在double不行 而要用float才能过得情况
7-8 在判断直角三角形时 不能直接 a2+b2==c^2 因为存在浮点误差
训练集03:
7-2 在输出Deposit Amount Wrong上卡了半天,后来才发现是一个字母大小写的问题,再次提醒我们检查代码时不要仅仅看语法逻辑,还要看具体输出 所以复制粘贴为什么会存在一个字母不同
7-4 一开始忘了输入其他情况是Wrong Format
(4)改进建议
训练集01
7-5 去掉重复的字符 在用遍历然后输出不重复的方法写完后 在想用StringBuffer 删除重复出现字符
7-7 判断是否有重复的字符 可以用set 因为set里元素是不重复的,如果set的大小不等于字符串大小说明有重复的
在很多有多重分支if-else时 可以用if...return的形式
(5)总结
经过这三次题目集的练习,对java有了从0到1的了解。例如:java怎么输入输出,java的字符串的很多引用如获取字串substring,查找字符下标indexOf等等,对于类的定义也有了一定了解。还需要进一步了解类的使用和其他的数据结构。

浙公网安备 33010602011771号