码题集OJ
MT1016-宽度对齐
输出455、-123、987654,宽度为5,分别左对齐和右对齐
import java.util.Scanner;
import java.util.*;class Main {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println(String.format("%-5d %5d",455,455));System.out.println(String.format("%-5d %5d",-123,-123));System.out.println(String.format("%-5d %5d",987654,987654));input.close();}
}
MT1017-左右对齐
输出3.1415926、22.3456,宽度为14,精度为6,分别左对齐和右对齐。
import java.util.Scanner;
import java.util.*;class Main {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println(String.format("-14.6f 14.6f",3.1415926,3.1415926));System.out.println(String.format("-14.6f 14.6f",22.3456,22.3456));input.close();}
}
MT1025-八、十六进制
输出202、117、70、130的十进制、八进制、十六进制数据形式,结果为0ddddd或0Xddddd。
import java.util.Scanner;
import java.util.*;
class Main {public static void main(String[] args) {Scanner input = new Scanner(System.in);int a = 202, b = 117, c = 70, d = 130;System.out.println(String.format("%1$d 0%1$o 0X%1$X",a).toUpperCase());System.out.println(String.format("%1$d 0%1$o 0X%1$X",b).toUpperCase());System.out.println(String.format("%1$d 0%1$o 0X%1$X",c).toUpperCase());System.out.println(String.format("%1$d 0%1$o 0X%1$X",d).toUpperCase());input.close();}
}
MT1051-四边形坐标
输入四边4个定点A,B,C,D的坐标(x,y),计算并输出四边形面积
static double getArea(double a, double b, double c){double p = (a+b+c) / 2.0;return Math.sqrt(p * (p-a)*(p-b)*(p-c));}public static void main(String[] args) {Scanner input = new Scanner(System.in);// code hereint x1 = input.nextInt();int y1 = input.nextInt();int x2 = input.nextInt();int y2 = input.nextInt();int x3 = input.nextInt();int y3 = input.nextInt();int x4 = input.nextInt();int y4 = input.nextInt();double p12 = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));double p13 = Math.sqrt(Math.pow(x3-x1,2)+Math.pow(y3-y1,2));double p14 = Math.sqrt(Math.pow(x4-x1,2)+Math.pow(y4-y1,2));double p23 = Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2));double p24 = Math.sqrt(Math.pow(x4-x2,2)+Math.pow(y4-y2,2));double p34 = Math.sqrt(Math.pow(x4-x3,2)+Math.pow(y4-y3,2));double area123 = getArea(p12,p13,p23);double area124 = getArea(p12,p14,p24);double area134 = getArea(p13,p14,p34);double area234 = getArea(p23,p24,p34);System.out.println(String.format("%.2f",(area123+area124+area134+area234)/2.0));input.close();}
MT1067-线段
有一条直线,线上有n个点(n >= 2),请问这条直线会被分割成多少个线段。n从键盘输入。不考虑负数,0或者其它特殊情况。
int n = input.nextInt();System.out.println(n * (n-1) / 2);
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
