目录
B2002 Hello,World!
题目描述
编写一个能够输出
Hello,World!
的程序。提示:
- 使用英文标点符号;
Hello,World!
逗号后面没有空格。H
和W
为大写字母。
输入格式
输出格式
Hello,World!
样例
样例输入
无
样例输出
Hello,World!
Java题解
import java.util.*;
public class Main{
public static void main(String[] args){
System.out.print("Hello,World!");
}
}
- 编写所有的Java代码,都要导入util包,类名都是Main
- 注意类名Main之后没有小括号
- 题目没有要求换行,就不要写println
B2025 输出字符菱形
题目描述
用
*
构造一个对角线长 5 5 5 个字符,倾斜放置的菱形。
输入格式
没有输入要求。
输出格式
如样例所示。用
*
构成的菱形。
样例 #1
样例输入 #1
样例输出 #1
*
***
*****
***
*
Java题解
import java.util.*;
public class Main{
public static void main(String[] args){
System.out.println(" *");
System.out.println(" ***");
System.out.println("*****");
System.out.println(" ***");
System.out.println(" *");
}
}
- 当没有要求输入,直接输出图形时,直接用字符画出图形并打印即可
P1000 超级玛丽游戏
题目描述
超级玛丽是一个非常经典的游戏。请你用字符画的形式输出超级玛丽中的一个场景。
********
************
####....#.
#..###.....##....
###.......###### ### ###
........... #...# #...#
##*####### #.#.# #.#.#
####*******###### #.#.# #.#.#
...#***.****.*###.... #...# #...#
....**********##..... ### ###
....**** *****....
#### ####
###### ######
##############################################################
#...#......#.##...#......#.##...#......#.##------------------#
###########################################------------------#
#..#....#....##..#....#....##..#....#....#####################
########################################## #----------#
#.....#......##.....#......##.....#......# #----------#
########################################## #----------#
#.#..#....#..##.#..#....#..##.#..#....#..# #----------#
########################################## ############
输入格式
无
输出格式
如描述
Java8题解:
import java.util.*;
public class Main{
public static void main(String[] args) {
System.out.println(" ******** ");
System.out.println(" ************ ");
System.out.println(" ####....#. ");
System.out.println(" #..###.....##.... ");
System.out.println(" ###.......###### ### ### ");
System.out.println(" ........... #...# #...#");
System.out.println(" ##*####### #.#.# #.#.#");
System.out.println(" ####*******###### #.#.# #.#.#");
System.out.println(" ...#***.****.*###.... #...# #...#");
System.out.println(" ....**********##..... ### ###");
System.out.println(" ....**** *****.... ");
System.out.println(" #### #### ");
System.out.println(" ###### ###### ");
System.out.println("##########################################");
System.out.println("#...#......#.##...#......#.##...#......#.##------------------#");
System.out.println("###########################################------------------#");
System.out.println("#..#....#....##..#....#....##..#....#....#####################");
System.out.println("########################################## #----------#");
System.out.println("#.....#......##.....#......##.....#......# #----------#");
System.out.println("########################################## #----------#");
System.out.println("#.#..#....#..##.#..#....#..##.#..#....#..# #----------#");
System.out.println("########################################## ############");
}
}
- 当没有要求输入,直接输出图形时,直接用字符画出图形并打印即可
P1001 A+B Problem
题目描述
输入两个整数 a , b a, b a,b,输出它们的和( ∣ a ∣ , ∣ b ∣ ≤ 10 9 |a|,|b| \le {10}^9 ∣a∣,∣b∣≤109)。
输入格式
两个以空格分开的整数。
输出格式
一个整数。
样例 #1
样例输入 #1
20 30
样例输出 #1
50
Java题解
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin=new Scanner(System.in);
int a = cin.nextInt(), b = cin.nextInt();
System.out.println(a+b);
}
}
B2005 字符三角形
题目描述
给定一个字符,用它构造一个底边长 5 5 5 个字符,高 3 3 3 个字符的等腰字符三角形。
输入格式
输入只有一行,包含一个字符。
输出格式
该字符构成的等腰三角形,底边长 5 5 5 个字符,高 3 3 3 个字符。
样例 #1
样例输入 #1
*
样例输出 #1
*
***
*****
提示
对于 100 % 100 \% 100% 的数据,输入的字符是 ASCII 中的可见字符。
Java题解
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char inputChar = sc.next().charAt(0);
System.out.println(" " + inputChar);
System.out.println(" " + inputChar + inputChar + inputChar);
System.out.println(""+inputChar + inputChar + inputChar + inputChar + inputChar);
}
}
P5703 【深基2.例5】苹果采购
题目描述
现在需要采购一些苹果,每名同学都可以分到固定数量的苹果,并且已经知道了同学的数量,请问需要采购多少个苹果?
输入格式
输入两个不超过 1 0 9 10^9 109 正整数,分别表示每人分到的数量和同学的人数。
输出格式
一个整数,表示答案。保证输入和答案都在 int 范围内的非负整数。
样例 #1
样例输入 #1
5 3
样例输出 #1
15
Java题解
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int a=sc.nextInt(),b=sc.nextInt();
System.out.print(a*b);
}
}
P5704 【深基2.例6】字母转换
题目描述
输入一个小写字母,输出其对应的大写字母。例如输入 q[回车] 时,会输出 Q。
输入格式
无
输出格式
无
样例 #1
样例输入 #1
q
样例输出 #1
Q
Java题解
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
char input=sc.next().charAt(0);
char output=Character.toUpperCase(input);
System.out.println(output);
}
}
- char input=sc.next().charAt(0);//获取输入的第一个字符
- 小写字母转大写字母:使用Character类中的toUpperCase()方法
- char output=Character.toUpperCase(input);//用output接收转换后的大写字母
P5705 【深基2.例7】数字反转
题目描述
输入一个不小于 100 100 100 且小于 1000 1000 1000,同时包括小数点后一位的一个浮点数,例如 123.4 123.4 123.4 ,要求把这个数字翻转过来,变成 4.321 4.321 4.321 并输出。
输入格式
一行一个浮点数
输出格式
一行一个浮点数
样例 #1
样例输入 #1
123.4
样例输出 #1
4.321
Java题解
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
double input=sc.nextDouble();
if(input>=100&&input<1000){
String str=String.format("%.1f",input);
String reverseNum=new StringBuffer(str).reverse().toString();
double reversed=Double.parseDouble(reverseNum);
System.out.print(reversed);
}
}
}
①先把数字串转化为字符串:
String str=String.format(“%.1f”,input);
- 把input格式化成小数点后一位,用的是String类的format方法.
②把字符串翻转过来
String reverseNum=new StringBuffer(str).reverse().toString();
- 把str字符串传到新建的StringBuffer对象里
- 用StringBuffer里的reverse方法翻转字符串
- 再将StringBuffer对象转化为String类型
③把翻转过来的字符串转化为数字串
double reversed=Double.parseDouble(reverseNum);
- 把字符串类型的reverseNum转换为数字double类型
- 用的是Double类中的parseDouble方法
P5706【深基2.例8】再分肥宅水
题目描述
现在有 t t t 毫升肥宅快乐水,要均分给 n n n 名同学。每名同学需要 2 2 2
个杯子。现在想知道每名同学可以获得多少毫升饮料(严格精确到小数点后 3 3 3 位),以及一共需要多少个杯子。
输入格式
输入一个实数 t t t 和一个正整数 n n n,使用空格隔开。
输出格式
输出两行。
第一行输出一个三位小数,表示可以获得多少毫升饮料。第二行输出一个正整数,表示一共需要多少个杯子。
样例 #1
样例输入 #1
500.0 3
样例输出 #1
166.667
6
提示
对于所有数据, 0 ≤ t ≤ 10000 0\leq t\leq 10000 0≤t≤10000 且小数点后不超过 3 3 3 位, 1 ≤ n ≤ 1000 1\leq n\leq 1000 1≤n≤1000。
Java题解
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
double t=sc.nextDouble();
int n=sc.nextInt();
double water=t/n;
int cup=n*2;
System.out.printf("%.3f%n",water);
System.out.println(cup);
}
}
为什么不能这样输出:System.out.println(“%.3f”,water);?
不能这样输出的原因是,System.out.println()
方法不支持使用格式化字符串。如果你想在输出中保留小数点后三位,可以使用String.format()
方法或者DecimalFormat
类来实现。
例如,使用String.format()
方法:
System.out.println(String.format("%.3f", water));
或者使用DecimalFormat
类:
import java.text.DecimalFormat;
DecimalFormat df = new DecimalFormat("#.###");
System.out.println(df.format(water));
P5708 【深基2.习2】三角形面积
题目描述
一个三角形的三边长分别是 a a a、 b b b、 c c c,那么它的面积为 p ( p − a ) ( p − b ) ( p − c ) \sqrt{p(p-a)(p-b)(p-c)} p(p−a)(p−b)(p−c),其中
p = 1 2 ( a + b + c ) p=\frac{1}{2}(a+b+c) p=21(a+b+c)。输入这三个数字,计算三角形的面积,四舍五入精确到 1 1 1 位小数。
输入格式
第一行输入三个实数 a , b , c a,b,c a,b,c,以空格隔开。
输出格式
输出一个实数,表示三角形面积。精确到小数点后 1 1 1 位。
样例 #1
样例输入 #1
3 4 5
样例输出 #1
6.0
提示
数据保证能构成三角形, 0 ≤ a , b , c ≤ 1000 0\leq a,b,c\leq 1000 0≤a,b,c≤1000,每个边长输入时不超过 2 2 2 位小数。
Java题解
P5707 【深基2.例12】上学迟到
题目描述
学校和 yyy 的家之间的距离为 s s s 米,而 yyy 以 v v v 米每分钟的速度匀速走向学校。
在上学的路上,yyy 还要额外花费 10 10 10 分钟的时间进行垃圾分类。
学校要求必须在上午 8:00 \textrm{8:00} 8:00 到达,请计算在不迟到的前提下,yyy 最晚能什么时候出门。
由于路途遥远,yyy 可能不得不提前一点出发,但是提前的时间不会超过一天。
输入格式
一行两个正整数 s , v s,v s,v,分别代表路程和速度。
输出格式
输出一个 24 24 24 小时制下的时间,代表 yyy 最晚的出发时间。
输出格式为 HH:MM \texttt{HH:MM} HH:MM,分别代表该时间的时和分。必须输出两位,不足前面补 0 0 0。
样例 #1
样例输入 #1
100 99
样例输出 #1
07:48
提示
对于 100 % 100\% 100% 的数据, 1 ≤ s , v ≤ 1 0 4 1 \le s,v \le 10^4 1≤s,v≤104。
Java题解