数的进制
题目描述
数在生活中有各种各样的表示法。时间的小时是24进制,时间的分是60进制,计算机内的数用二进制,我们平时用十进制。请你把一个十进制的数转成八进制和十六进制。
输入格式
输入有若干行,每行一个整数代表一个十进制数。数字范围在int以内。
输出格式
按要求的格式输出对应的八进制数和十六进制数。
如输入10时,输出格式是:10 = 12(8) = a(16)
输入负数时,只需要将其相反数转化成对应进制,输出时在前面添加负号即可。
输入样例
10
-2
99
100
-100
输出样例
10 = 12(8) = a(16)
-2 = -2(8) = -2(16)
99 = 143(8) = 63(16)
100 = 144(8) = 64(16)
-100 = -144(8) = -64(16)
import java.util.Scanner;
public class p20 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
int i = sc.nextInt();
if (i < 0){
int x = Math.abs(i);
String octal = Integer.toOctalString(x);
String hexadecimal = Integer.toHexString(x);
System.out.println(i + " = -" + octal + "(8)" + " = -" + hexadecimal+"(16)");
}else{
String octal = Integer.toOctalString(i);
String hexadecimal = Integer.toHexString(i);
System.out.println(i + " = " + octal + "(8)" + " = " + hexadecimal+"(16)");
}
}
}
}
逗号运算符和逗号表达式
题目描述
游戏大王今天的作业是按序求下列逗号表达式的值。
a = 3+x,6+x;
a = (3+x,6+x);
a = ((x=35,x4),x+5);
a = x=3,63;
a = (x=3,63);
对不同的x,请编程求a。
输入格式
输入有若干个案例,每个案例一行,每行一个整数x。
输出格式
对于每一个案例,输出相应5个含逗号表示式的a的值,格式见样例,案例之间要空一行。
输入样例
0
2
输出样例
When x=0:
a=3
a=6
a=20
a=3
a=18
When x=2:
a=5
a=8
a=20
a=3
a=18
import java.util.Scanner;
public class p21 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
int i = sc.nextInt();
System.out.println(
"When x=" + i +":\n"+
"a=" +(3+i)+"\n"+
"a=" +(6+i)+"\n"+
"a=20\n" +
"a=3\n" +
"a=18\n");
}
}
}
排队
题目描述
有三个数a、b、c。请编程将他们按从小到大输出。
输入格式
输入有若干行,每行三个数,分别用空格隔开。
输出格式
每行都按从小到大输出。
输入样例
2 1 3
4 5 6
8 7 6
0 -1 -3
1 10 1
输出样例
1 2 3
4 5 6
6 7 8
-3 -1 0
1 1 10
import java.util.Scanner;
public class p12 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextDouble()){
double x1 = sc.nextDouble();
double y1 = sc.nextDouble();
double x2 = sc.nextDouble();
double y2 = sc.nextDouble();
double x3 = sc.nextDouble();
double y3 = sc.nextDouble();
double vector1x = x1-x2;
double vector1y = y1-y2;
double vector2x = x3-x2;
double vector2y = y3-y2;
//使用向量点积公式a·b = |a||b|cosθ
double t = (vector1x * vector2x + vector1y * vector2y)/
(Math.sqrt(Math.pow(vector1x,2) + Math.pow(vector1y,2))
* Math.sqrt(Math.pow(vector2x,2) + Math.pow(vector2y,2)));
//方法一:
//toDegrees该方法用于将弧度转换为度数,acos其作用是返回给定参数的反余弦值
System.out.printf("%.2f%n",Math.toDegrees(Math.acos(t)));
//方法二:
// System.out.printf("%.2f%n",Math.acos(t) * (180 / Math.PI));
}
}
}
求值
题目描述
有一个函数:
对于每一个输入的x,请编写程序计算y的值。
输入格式
输入有若干行,每行一个整数x(-1000000<=x<=1000000)。
输出格式
对应输出y的值。要求先输出“y = ”,再输出y的值。
输入样例
-100
0
30
输出样例
y = -100
y = 0
y = 10
import java.util.Scanner;
public class p23 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
int x = sc.nextInt();
if (x < 1){
System.out.println("y = " + x);
}else if (x < 10){
System.out.println("y = " + (2 * x -1));
}else{
System.out.println("y = " + 10);
}
}
}
}
谁是6的倍数
题目描述
很多人喜欢6,认为6是顺利的意思,就是6的倍数也喜欢,请你编程找出这样的数。
输入格式
输入只有一行,含若干个正整数,每个数之间用空格隔开。
输出格式
将6及6的倍数按原序输出,数之间用空格隔开。
输入样例
1 5 7 6 3 12 72 99 22 18
输出样例
6 12 72 18
import java.util.Scanner;
public class p24 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
String s = sc.nextLine();
String[] arr = s.split(" ");
int[] nums = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
nums[i] = Integer.parseInt(arr[i]);
}
for (int num : nums) {
if (num % 6 == 0) {
System.out.print(num + " ");
}
}
System.out.println();
}
}
}