原码
题目描述
输入有若干行,每行一个整数,其绝对值小于2^15。
输入格式
输入有若干行,每行有6个整数,a1,a2,b1,b2,c1,c2。
表示A(a1,a2),B(b1,b2),C(c1,c2)。
输出格式
每行对应输出一个16位的原码。
输入样例
7
-7
16384
输出样例
0000000000000111
1000000000000111
0100000000000000
import java.util.Scanner;
public class p25 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
int num = sc.nextInt();
short[] bits = new short[16];
if (num >= 0){
//整数的原码即为其二进制表示
for (int i = 15; i >= 0; i--) {
bits[i] = (short) (num % 2);
num /= 2;
}
} else {
//负数的代码用其绝对值的二进制表示,最高位为1
num = Math.abs(num);
for (int i = 15; i >= 0; i--) {
bits[i] = (short) (num % 2);
num /= 2;
}
bits[0] = 1;
}
//输出原码
for (short bit : bits){
System.out.print(bit);
}
System.out.println();
}
}
}
排序
题目描述
输入N个数字,进行从小到大排序。
输入格式
输入第一行为正整数N(1<=N<=100000)
第二行包含N个数字,表示要排序的数字。
输出格式
输出N个从小到大排好序的整数,以空格分隔。
输入样例
5
3 2 1 4 5
输出样例
1 2 3 4 5
import java.util.Arrays;
import java.util.Scanner;
public class p26 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < nums.length; i++) {
nums[i] = sc.nextInt();
}
Arrays.sort(nums);
for (int num : nums) {
System.out.print(num + " ");
}
System.out.println();
}
}
三角形的面积(2)
题目描述
输入三个非负整数a、b、c,如果能构成三角形就求出它的面积。如果不能构成三角形就输出“No”。请编程实现。
输入格式
输入有若干行,每行三个非负整数a、b、c。以0 0 0表示输入结束。
输出格式
对于每一行,如果能构成三角形就输出面积(结果保留2位小数),如果不能构成三角形就输出“No”。
输入样例
3 4 5
2 2 2
1 2 3
0 0 0
输出样例
6.00
1.73
No
import java.util.Scanner;
public class p27 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if (a == 0 && b == 0 && c == 0){
break;
}
//判断是否能否构成三角形
if (a + b > c && b + c >a && a + c >b){
//计算半周长
double p = (a + b +c)/2.0;
/*计算面积,三角形面积公式:sqrt(p * (p - a) * (p - b) * (p - c)),其中
p为半周长:p = a + b +c*/
double area = Math.sqrt(p * (p - a) * (p - b) * (p - c));
System.out.printf("%.2f%n",area);
}else{
System.out.println("No");
}
}
}
}
三角函数
题目描述
判断点是否在矩形上
输入格式
在平面直角坐标系中有一矩形左下角坐标是(a,b),右上角坐标是(c,d),矩形的边平行于坐标轴。
现在有一系列点(xi,yi),i=0,…,n-1,请你判断它们与矩形的位置关系,分为在矩形内、在矩形上或在矩形外。
输出格式
输入有多组测试数据
每组测试数据第一行是一个整数n,第二行是矩形的左下角和右上角的坐标,接下来n行是要判断的点。
输入坐标均为整数点,数字范围在[-1000,1000]之间
输入样例
2
0 0 4 5
1 1
5 8
3
1 2 5 7
0 6
3 4
5 5
输出样例
YES
NO
NO
YES
YES
import java.util.Scanner;
public class p28 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
int n = sc.nextInt();//输入坐标点的个数
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
if (x < a || x > c || y < b || y > d){
System.out.println("NO");
}
else{
System.out.println("YES");
}
}
System.out.println();
}
}
}
顺时针逆时针
题目描述
平面上的三个点A(0,0),B(1,0),C(0,2)。按A->B->C是逆时针,按C-B-A是顺时针。
给你平面三点的顺序,请你判断是逆时针、顺时针还是共线。
输入格式
输入有若干行,每行有6个整数:X1、Y1、X2、Y2、X3、Y3表示你要顺序经过的三个点。
输出格式
每行输出一个结果,顺时针就输出“Clockwise”,逆时针就输出“Anticlockwise”,共线输出"Collineation"。
输入样例
0 0 1 0 0 2
0 2 1 0 0 0
输出样例
Anticlockwise
Clockwise
import java.util.Scanner;
public class p29 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
/*可以使用向量叉积的方法判断三个点的顺序。向量叉积的定义为:对于向量a=(x1,y1)和向量b=(x2,y2),
它们的叉积为axb=x1y2-x2y1。如果向量叉积结果为正数,说明a在b的顺时针方向;如果为负数,说明a在b的逆时针方向;
如果为0,说明a和b共线。*/
while (sc.hasNextInt()){
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int x3 = sc.nextInt();
int y3 = sc.nextInt();
int sum;
sum = (x2 - x1) * (y3 - y2) - (x3 - x2) * (y2 - y1);
if (sum > 0){
System.out.println("Anticlockwise");
}else if (sum < 0){
System.out.println("Clockwise");
}else{
System.out.println("Collineation");
}
}
}
}