1题
public static void main(String[] args) {
int a =3;
double b =9.5;
double c = 45.5;
double d = 2.5;
double e = 3.5;
double g = 4.5;
double f = ((b*g)-(d*a))/(c-e);
System.out.println(f);
2题
double sum=0;
double snm=0;
double a = 0;
double b = 1.0;
for(int i =1;i<=13;i=i+4){
sum+=4*(b/i);
}
for(int i =3;i<=13;i=i+4){
snm+=4*(b/i);
}
a=sum-snm;
System.out.println(a);
3题
double π =3.14;
double S =0;
double C = 0;
double r = 5.5;
C =2*π*r;
S=π*r*r;
System.out.println(S);
System.out.println(C);
4题
int s =14;
double S = (s/1.6);
double t = 45.5/60;
System.out.println(S/t);
5题
int s =24;
double S = (s*1.6);
double t =(6035/3600);
double v = S/t;
System.out.println(v);
6题
double a =3.4;
double b = 50.2;
double c =2.1;
double d = 0.55;
double e = 44.5;
double f = 5.9;
double x = (e*d-b*f)/(a*d-b*c);
double y = (a*f-e*c)/(a*d-b*c);
System.out.println("x="+x);
System.out.println("y="+y);
7题
int totalSeconds = 1203183068;
int totalMinutes = totalSeconds/60;
int totalHours = totalMinutes/60;
int nowSeconds =totalSeconds%60;
int nowMinutes = totalMinutes%60;
int nowHours =totalHours%60;
System.out.println(totalMinutes);
System.out.println(totalHours);
System.out.println(nowMinutes);
System.out.println(nowSeconds);
System.out.println(nowHours);
8题
Scanner input = new Scanner(System.in);
System.out.println("请输入钱的数量");
double money = input.nextDouble();
int Dollar = (int)money;
int Dime =(int )(money*10)%10;
int Penny =(int)(money*10)%100;
int dollar = Dollar;
double quarter = Dollar/0.2511+Dime/2.5;
double dime = Dollar*10+Dime;
double penny = Dollar*100+Dime*10+Penny;
System.out.println("1美元的数量"+dollar);
System.out.println("二角五分的数量"+quarter);
System.out.println("一角的数量"+dime);
System.out.println("一分的数量"+penny);
input.close();
9题
Scanner input = new Scanner(System.in);
System.out.println("请输入摄氏度:");
double S = input.nextDouble();
double H =(9.0/5)*S+32;
System.out.println(H);
10题
Scanner input = new Scanner(System.in);
System.out.println("请输入半径和高:");
double radius = input.nextDouble();
double length = input.nextDouble();
double area = Math.pow(radius, 2) * Math.PI;
double volume = area * length;
System.out.println("面积是:"+area);
System.out.println("体积是: "+ volume);
11题
Scanner input = new Scanner(System.in);
System.out.println("请输一个0~1000的整数:");
int number = input.nextInt();
int a =number%10;
int b = number/10/10;
int c = number/10%10;
int sum = 0;
sum=(a+b+c);
System.out.println("各位数之间的和为:"+sum);
12题
Scanner input = new Scanner(System.in);
System.out.println("请输入时区偏移量:");
long totalMilliseconds = input.nextInt();
long totalSeconds = totalMilliseconds / 1000;
totalSeconds += totalMilliseconds * 60 * 60;
long currentSecond = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long currentHour = totalHours % 24;
System.out.println("The Current time is " + currentHour + ":"
+ currentMinute + ":" + currentSecond + " GMT");
13题
Scanner input = new Scanner(System.in);
System.out.println("请输入水的重量(以千克为单位)以及水的初始温度和最终温度:");
double M = input.nextDouble();
System.out.println("请输入水的初始温度:");
double initial =input.nextDouble();
System.out.println("请输入水的最终温度:");
double finaliy = input.nextDouble();
double Q = 0;
Q= M*(finaliy-initial)*4184;
System.out.println("The energy needed is : "+Q);
14题
Scanner input = new Scanner(System.in);
System.out.println("请输入你每个月向银行存入的钱:");
int money =input.nextInt();
double sum = 0;
for(int i=0;i<=6;i++){
sum =(money+sum)*(1+0.00417);
}
System.out.println("六个月后账户上的钱是:"+sum);
15题
Scanner input = new Scanner(System.in);
System.out.println("请输入两个点:");
double x1,y1 ,x2,y2;
x1=input.nextDouble();
x2=input.nextDouble();
y1=input.nextDouble();
y2=input.nextDouble();
final double a = ((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
double z = Math.pow(a,0.5);
System.out.println("两点之间的距离是:"+z);
16题
Scanner input = new Scanner(System.in);
System.out.println("请输入三个点:");
double x1,y1 ,x2,y2,x3,y3;
x1=input.nextDouble();
x2=input.nextDouble();
x3=input.nextDouble();
y1=input.nextDouble();
y2=input.nextDouble();
y3=input.nextDouble();
final double s = (z+x+w)/2;
double k = (s(s-z)*(s-x)*(s-w));
double a = ((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
double b = ((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
double c = ((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
double z = Math.pow(a,0.5);
double x = Math.pow(b,0.5);
double w = Math.pow(c,0.5);
double S = Math.pow(k, 0.5);
System.out.println(" 三角形的面积是:"+S);
16题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入你的身高和体重值:");
double a = dr.nextDouble();
double b = dr.nextDouble();
double s = jiankang(a, b);
System.out.println(s);
}
public static double jiankang(double a,double b ) {
double BIM =a/Math.pow(b, 2);
if( BIM<18.5){
System.out.println("偏瘦");}
else if( 18.5<=BIM&&BIM<25.5){
System.out.println("正常");}
else if(25.5<=BIM&&BIM<30.0){
System.out.println("超重");}
else if(30.0<= BIM){
System.out.println("过胖");}
else{
System.out.println("输入错误");
}
return BIM;
}
18题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入一个年份:");
int year=dr.nextInt();
int s =years(year);
System.out.println();
}
public static int years(int year) {
if(year%400==0||(year%4==0&&year%100!=0)){
System.out.println("是闰年!");
}else{
System.out.println("不是闰年!");
}
return year;
}
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入一个两位数:");
int ss = dr.nextInt();
chaipiao(ss);
}
19题
public static void chaipiao(int ss) {
for(int i=1;i<+9;i++){
for(int j =0;j<=9;j++){
int sum = i*10+j;
int num = i+j*10;
if(sum==ss){
System.out.println("奖金10000元");
break;
}else if(ss==num){
System.out.println("奖金3000元");
break;
}
else if(ss/10==i||ss%10==j){
System.out.println("奖金1000元");
break;
}
else if(ss/10==j||ss%10==i){
System.out.println("奖金1000元");
break;
}else{
System.out.println("没有中奖");
}
}
}
}
20题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入a,b,c的值;");
double a = dr.nextDouble();
double b = dr.nextDouble();
double c = dr.nextDouble();
jisuan(a, b, c);
}
public static void jisuan(double a, double b,double c) {
double t =(b*b-4*a*c);
double r1 = (-b+Math.pow(t, 0.5))/2*a;
double r2 = (-b-Math.pow(t, 0.5))/2*a;
if(t>0){
System.out.println("r1="+r1+","+"r2="+r2);
}else if(t==0){
System.out.println("r="+r1);
}else{
System.out.println("the equation has no real root");
}
}
21题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入a,b,c,d,e,f的值");
int a =dr.nextInt();
int b =dr.nextInt();
int c =dr.nextInt();
int d =dr.nextInt();
int e =dr.nextInt();
int f =dr.nextInt();
xianxi(a, b, c, d, e, f);
}
public static int xianxi(int a, int b, int c, int d ,int e, int f) {
int s = (a*d-b*c);
double x =(e*d-b*f)/s;
double y =(a*f-e*c)/s;
if(s==0){
System.out.println("this equation has no solution ");
}
System.out.println("x="+x+","+"y="+y);
return s;
}
public static void main(String[] args) {
Scanner dr =new Scanner(System.in);
System.out.println("输入代表今天日期的数字:");
int S = dr.nextInt();
xijian(S);
}
22题
public static void xijian(int S ) {
switch(S){
case 0:
System.out.println("星期天");
break;
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
default:
System.out.println("输入错误,请重新输入!");
}
}
23题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入一个数:");
String S = dr.nextLine();
huiwen(S);
}
public static void huiwen(String S) {
int i = 0;
int j =S.length()-1;
while(i<=j){
if(S.charAt(i)!=S.charAt(j)){
System.out.println(S+"不是回文数");
break;
}
j--;
i++;
System.out.println(S+"是回文数");
break;
}
}
24题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请随机输入0~2一个整数:");
int a = dr.nextInt();
caiquan(a);
}
public static void caiquan(int a ) {
int b =(int)Math.floor((Math.random()*3));
if(a==b){
System.out.println("平局");
}else if(a>b){
System.out.println("用户赢了");
}else if(a<b){
System.out.println("系统赢了");
}else if(a>3){
System.out.println("输入错误请重新输入:");
}
}
25 题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入年份:");
int year = dr.nextInt();
System.out.println("请输入月份:");
int month =dr.nextInt();System.out.println("请输入日期:");
int day = dr.nextInt();
siji(year, month, day);
}
public static void siji(int year ,int month, int day) {
double j =Math.abs((year/100));
int k = year%100;
if(month==1){
month=13;
year-=1;
}else if(month ==2){
month= 14;
year-=1;
}
int h =(int)((day+(26*(month+1)/10)+k+(k/4)+(j/4)+5*j)%7);
switch(h){
case 0:
System.out.println("星期六");
break;
case 1:
System.out.println("星期天");
break;
case 2:
System.out.println("星期一");
break;
case 3:
System.out.println("星期二");
break;
case 4:
System.out.println("星期三");
break;
case 5:
System.out.println("星期四");
break;
case 6:
System.out.println("星期五");
break;
}
}
26题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入一个点位(x,y)");
double x1,y1;
x1 =dr.nextDouble();
y1 =dr.nextDouble();
juli(x1, y1);
}
public static void juli(double x1 ,double y1) {
int r =10;
double x2 =0;
double y2 =0;
double S =Math.pow(((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)), 0.5);
if(S>r){
System.out.println("("+x1+","+y1+")"+"在圆外");
}else if(S==r){
System.out.println("("+x1+","+y1+")"+"在圆上");
}else{
System.out.println("("+x1+","+y1+")"+"在圆内");
}
}
27题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入一个点位(x,y)");
double x1,y1;
x1 =dr.nextDouble();
y1 =dr.nextDouble();
juli(x1, y1);
}
public static void juli(double x1 ,double y1) {
double r =Math.pow((5*5+10*10), 0.5);
double S =Math.pow(x1*x1+y1*y1, 0.5);
if(x1<0||y1<0){
System.out.println("输入错误!请重新输入");
}else if(x1>0&&y1>0){
if(S<r){
System.out.println("("+x1+","+y1+")"+"在矩形内");
}else{
System.out.println("("+x1+","+y1+")"+"不在矩形内");
}
}
}
28题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入一个点位(x,y)");
double x1,y1;
x1 =dr.nextDouble();
y1 =dr.nextDouble();
juli(x1, y1);
}
public static void juli(double x1 ,double y1) {
if(x1>=0&&x1<=200&&y1<=-0.5*x1+100){
System.out.println("点在三角形内");
}else{
System.out.println("点在三角形外");
}
}
29题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入点x1,y1的值:");
double x1,y1;
x1 =dr.nextDouble();
y1 =dr.nextDouble();
System.out.println("请输入点x2,y2的值:");
double x2,y2;
x2 =dr.nextDouble();
y2 =dr.nextDouble();
System.out.println("请输入大矩形的宽和高:");
double i1,j1;
i1 =dr.nextDouble();
j1 =dr.nextDouble();
System.out.println("请输入小矩形的宽和高:");
double i2,j2;
i2 =dr.nextDouble();
j2 =dr.nextDouble();
juli(x1, y1, x2, y2, i1, j1, i2, j2);
}
public static void juli(Double x1,Double y1, Double x2,Double y2, Double i1, Double j1, Double i2,Double j2) {
if(Math.abs(x1-x2)<i1/2&&Math.abs(y1-y2)<j1/2){
System.out.println("小矩形在大矩形内");
}
else if(Math.abs(x1-x2)<=(i1+i2)/2&&Math.abs(y1-y2)<=(j1+j2)/2){
System.out.println("小矩形与大矩形相交");
}else if(Math.abs(x1-x2)>(i1+i2)/2&&Math.abs(y1-y2)>(j1+j2)/2){
System.out.println("小矩形与大矩形相离");
}
}
30题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入点圆1的中心点x1,y1的值:");
double x1,y1;
x1 =dr.nextDouble();
y1 =dr.nextDouble();
System.out.println("请输入圆2的中心点x2,y2的值:");
double x2,y2;
x2 =dr.nextDouble();
y2 =dr.nextDouble();
System.out.println("请输入大圆的半径:");
double i1;
i1 =dr.nextDouble();
System.out.println("请输入小圆的半径:");
double i2;
i2 =dr.nextDouble();
juli(x1, y1, x2, y2, i1, i2 );
}
public static void juli(Double x1,Double y1, Double x2,Double y2, Double i1, Double i2) {
if(Math.pow((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2),0.5)<Math.abs(i1-i2)){
System.out.println("两圆重叠"); }
else if(Math.pow((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2), 0.5)<i1+i2){
System.out.println("两圆形相交");
}else if(Math.pow((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2), 0.5)>i1+i2){
System.out.println("两圆形相离");
}
}
31题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入一个整数值:");
int s = dr.nextInt();
hanshu(s);
}
public static void hanshu(int s) {
boolean temp = true;
if(s%5==0&&s%6==0){
System.out.println(temp);
}else{
temp =false;
System.out.println(temp);
}
if(s%5==0||s%6==0){
temp =true;
System.out.println(temp);
}else{
temp =false;
System.out.println(temp);
}
if((s%5==0||s%6==0)&&(s%5==0&&s%6==0)){
temp =false;
System.out.println(temp);
}else{
temp =true;
System.out.println(temp);
}
}
32题
public static void main(String[] args) {
int j=(int)Math.ceil(Math.random()*100);
System.out.println(j);
Scanner dr = new Scanner(System.in);
System.out.println("请连续输入一个数:");
int a =dr.nextInt();
for(int k =1;k<10;k++){
if(a<j){
System.out.println("输入小了!请再次输入");
a = dr.nextInt();
}else if(a>j){
System.out.println("输入大了!请再次输入");
a= dr.nextInt();
}
}
System.out.println("你找到该数了");
}
33题
public static void main(String[] args) {
Scanner dr = new Scanner(System.in);
System.out.println("请输入两个整数:");
int n1 =dr.nextInt();
int n2 = dr.nextInt();
int s = jisuan(n1, n2 );
System.out.println(s);
}
public static int jisuan(int n1 ,int n2 ) {
int gcd=1;
if(n2>n1){
for(int k =2;k<=n1;k++){
if(n1%k==0&&n2%k==0){
gcd=k;
}
}
}else if(n1>n2){
for(int k =2;k<=n2;k++){
if(n1%k==0&&n2%k==0){
gcd=k;
}
}
}
return gcd;
}
34题
35题
String str = "";
System.out.println("请输入一个字符串:");
Scanner in = new Scanner(System.in);
str = in .nextLine();
point(str);
}
public static void point(String str){
StringBuffer sb = new StringBuffer(str);
sb.reverse();
int n = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == sb.charAt(i))
n++;
}
if (n == str.length()){
System.out.println(str + "是回文!");
}
else
System.out.println(str + "不是回文!");
public static void main(String[] args){
int count =0;
panduan(count);
}
public static void panduan(int count){
int sum =0;
for(int i=2;i<100;i++){
if(i==2||i%2!=0){
sum =i;
System.out.print(sum+",");
count++;
switch(count){
case 10:
System.out.println(sum);
break;
case 20:
System.out.println(sum);
break;
case 30:
System.out.println(sum);
break;
case 40:
System.out.println(sum);
break;
case 50:
System.out.println(sum);
}
}
}
}
36题
public static void mian(String[] args){
Scanner dr = new Scanner(System.in);
System.out.println("请输入十个整数:");
int[] arr = new int[10];
for(int i=0;i<arr.length;i++){
arr[i]=dr.nextInt();
}
double avge =0;
jisuan(arr,avge);
System.out.println("平均值:"+avge);
}
public static void jisuan(int[] arr,double avge){
int count=0;
int sum =0;
for(int k =0;k<arr.length;k++){
if(arr[k]>0){
count+=arr[k];
}else if(arr[k]<0){
sum+=arr[k];
}else if(arr[k]==0){
System.out.println("No number ar enter except 0");
break;
}
avge =(sum+count)/10;
}
}
}
37题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入若干个整数:");
int a, countPositive = 0, countNegative = 0;
int sum = 0;
while (true){
a = scan.nextInt();
if (a < 0) {
countNegative++;
}else if (a > 0){
countPositive++;
}else {
break;
}
sum += a;
}
double aver = 0;
if (countNegative + countPositive != 0) {
aver = sum / (double)(countNegative + countPositive);
System.out.println("正数个数为:" + countPositive);
System.out.println("负数个数为:" + countNegative);
System.out.println("输入值的总和为:" + sum);
System.out.println("输入值的平均值为:" + aver);
}else {
System.out.println("没有输入有效值");
}
scan.close();
}
}
38题
public static void main(String[] args) {
printNum();
}
private static void printNum() {
int count = 0;
for (int i = 100; i <= 1000; i++) {
if (i % 5 == 0 && i % 6 == 0) {
System.out.print(i + " ");
count++;
if (count % 10 == 0) {
System.out.println();
}
if (count == 10) {
System.out.println();
count = 0;
}
}
}
}
}
39题
public static void main(String[] args) {
printNum1();
}
private static void printNum1() {
int count = 0;
for (int i = 100; i <= 200; i++) {
if ((i % 5 == 0 || i % 6 == 0) && !(i % 5 == 0 && i % 6 == 0)) {
System.out.print(i + " ");
count++;
if (count % 10 == 0) {
System.out.println();
}
if (count == 10) {
System.out.println();
count = 0;
}
}
}
}
}
40题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num = scan.nextInt();
printFactor(num);
}
private static void printFactor(int num) {
int n = num;
for (int i = 2; i <= num; i++) {
while (num % i == 0) {
num = num / i;
System.out.print(i + " ");
n /= i;
}
}
}
}
41题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个整数(1-15):");
int num = scan.nextInt();
pyramid(num);
}
public static void pyramid(int num) {
for (int i = 1; i <= num; i++) {
for (int j = 0; j < 2 * (num - i); j++) {
System.out.print(" ");
}
for (int k = 0; k < i; k++) {
System.out.print(i-k + " ");
}
for (int k = 2; k <= i; k++) {
System.out.print(k + " ");
}
System.out.println("");
}
}
}
42题
public static void main(String[] args) {
printPattern1();
System.out.println();
printPattern2();
System.out.println();
printPattern3();
System.out.println();
printPattern4();
}
public static void printPattern1() {
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
public static void printPattern2() {
for (int i = 0; i < 6; i++) {
for (int j = 1; j <= 6 - i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
public static void printPattern3() {
for (int i = 1; i <= 6; i++) {
for (int k = 0; k < 2 * (6 - i); k++) {
System.out.print(" ");
}
for (int j = i; j > 0; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
public static void printPattern4() {
for (int i = 1; i <= 6; i++) {
for (int k = 0; k < 2 * (i - 1); k++) {
System.out.print(" ");
}
for (int j = 1; j <= 7 - i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
43题
public static void main(String[] args) {
numPyramid();
}
public static void numPyramid() {
for (int i = 0; i < 8; i++) {
for (int k = 1; k <= (8 - i - 1); k++) {
System.out.print("\t");
}
for (int j = 1; j <= Math.pow(2, i); j *= 2) {
System.out.print(j + "\t");
}
for (int j = (int) (Math.pow(2, i - 1)); j > 0; j /= 2) {
System.out.print(j + "\t");
}
System.out.println();
}
}
}
44题
public static void main(String[] args) {
primeNumber();
}
private static void primeNumber() {
int count = 0;
for (int i = 2; i <= 1000; i++) {
boolean isFlag = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isFlag = false;
}
}
if (isFlag) {
System.out.print(i + "\t");
count ++;
if (count % 8 == 0) {
System.out.println();
}
}
}
}
}
45题
public static void main(String[] args) {
System.out.println(sumSequence());
System.out.println(sumSequence1(49));
}
private static double sumSequence() {
int n = 0;
double sum = 0;
for (int i = 1; i < 50; i++) {
sum += (double)(2 * i - 1) / (2 * i + 1);
}
return sum;
}
private static double sumSequence1(int i) {
int n = 0;
if (i == 1) {
return (double)(2 * i - 1) / (2 * i + 1);
}
return (double)(2 * i - 1) / (2 * i + 1) + sumSequence1(i - 1);
}
}
46题
public static void main(String[] args) {
System.out.println(countPI());
double result = countPI1(1000);
double PI = result * 4;
System.out.println(PI);
}
private static double countPI() {
double sum = 0;
for (int i = 1; i < 100000000; i++) {
sum += (Math.pow(-1, i + 1) / (2 * i - 1));
}
sum *= 4;
return sum;
}
private static double countPI1(int n) {
if (n == 1) {
return (Math.pow(-1, n + 1) / (2 * n - 1));
}
return (Math.pow(-1, n + 1) / (2 * n - 1)) + countPI1(n - 1);
}
}
47题
public static void main(String[] args) {
System.out.println(countE());
}
private static double countE() {
double sum = 1;
for (int i = 1; i < 14; i++) {
int factorial = 1;
for (int j = 1; j <= i; j++) {
factorial *= j;
}
sum += 1.0 / factorial;
}
return sum;
}
}
48题
public static void main(String[] args) {
completeNum();
}
private static void completeNum() {
for (int i = 6; i < 10000; i++) {
int sum = 0;
for (int j = 1; j <= i / 2; j++) {
if (i % j == 0) {
sum += j;
}
}
if (i == sum) {
System.out.println(i);
}
}
}
}
49题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int countPlay1 = 0, countPlay2 = 0;
while (true) {
int play1 = (int) (Math.random() * 3);
System.out.println("请输入0、1或者2,分别表示石头、剪刀和布:");
int play2 = scan.nextInt();
int result = guessHand(play1, play2);
if (result == 1) {
countPlay2++;
System.out.println("你赢了 " + countPlay2 + " 局了");
} else if (result == 2) {
countPlay1++;
System.out.println("电脑赢了" + countPlay1 + "局了");
} else if (result == 3) {
System.out.println("打成平手");
} else {
System.out.println("你出的有误!");
}
if (countPlay1 > 2) {
System.out.println("游戏结束,很遗憾你输了!电脑赢了");
break;
}
if (countPlay2 > 2) {
System.out.println("游戏结束,恭喜你赢了!战胜了电脑");
break;
}
}
}
private static int guessHand(int play1, int play2) {
if (play2 < 0 || play2 > 2) {
return 0;
} else {
(剪刀可以剪布,石头可以砸剪刀,而布可以包石头。)
0 石头,1 剪刀,2 布
if (play1 == 0 && play2 == 1) {
System.out.println("恭喜你赢了!战胜了电脑");
return 1;
} else if (play1 == 0 && play2 == 2) {
System.out.println("很遗憾你输了!电脑赢了");
return 2;
} else if (play1 == 1 && play2 == 0) {
System.out.println("很遗憾你输了!电脑赢了");
return 2;
} else if (play1 == 1 && play2 == 2) {
System.out.println("恭喜你赢了!战胜了电脑");
return 1;
} else if (play1 == 2 && play2 == 0) {
System.out.println("恭喜你赢了!战胜了电脑");
return 1;
} else if (play1 == 2 && play2 == 1) {
System.out.println("很遗憾你输了!电脑赢了");
return 2;
} else { play1 == play2
return 3;
}
}
}
}
50题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num = scan.nextInt();
toBinary(num);
}
private static void toBinary(int num) {
String binary = "";
while (num / 2 != 0) {
binary = (num % 2) + binary;
num /= 2;
}
binary = num + binary;
System.out.println(binary);
}
}
51题
System.out.print("请输入一个十进制的整数 : ");
int num=input.nextInt();
int k = 0,i = 0;
while(num != 0)
{
k += num % 8 *Math.pow(10,i);
num /= 8;
i++;
}
System.out.print("其对应的八进制为 : "+k);
52题
System.out.print("Enter numbers: ");
int a=input.nextInt();
int max=a;
int count=1;
int i=1;
while(a!=0)
{
if(max<a)
{
max=a;
count=1;
}
else if(max==a)
{
if(i==1)
count=1;
else
count++;
}
a=input.nextInt();
i++;
}
System.out.println("The largest number is "+max);
System.out.println("The occurrence count of the largest number is "+count);
53题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入三角形的三条边:");
double a = scan.nextInt();
double b = scan.nextInt();
double c = scan.nextInt();
double A = countAngle(a, b, c);
double B = countAngle(b, a, c);
double C = countAngle(c, b, a);
System.out.println(String.format("A角%f, B角%f, C角%f", A, B, C));
System.out.printf("A角%.2f, B角%.2f, C角%.2f%n", A, B, C);
}
private static double countAngle(double a, double b, double c) {
double angle = 0;
angle = Math.toDegrees(Math.acos((a * a - b * b - c * c) / (-2 * b * c)));
return angle;
}
54题
public static void main(String[] args) {
String set1 = " 1 3 5 7\n" + " 9 11 13 15\n"
+ "17 19 21 23\n" + "25 27 29 31";
String set2 = " 2 3 6 7\n" + "10 11 14 15\n"
+ "18 19 22 23\n" + "26 27 30 31";
String set3 = " 4 5 6 7\n" + "12 13 14 15\n"
+ "20 21 22 23\n" + "28 29 30 31";
String set4 = " 8 9 10 11\n" + "12 13 14 15\n"
+ "24 25 26 27\n" + "28 29 30 31";
String set5 = "16 17 18 19\n" + "20 21 22 23\n"
+ "24 25 26 27\n" + "28 29 30 31";
System.out.println("集合1:\n" + set1);
System.out.println("集合2:\n" + set2);
System.out.println("集合3:\n" + set3);
System.out.println("集合4:\n" + set4);
System.out.println("集合5:\n" + set5);
guessBirth();
private static void guessBirth() {
Scanner scan = new Scanner(System.in);
int birth = 0;
for (int i = 1; i <= 5; i++){
System.out.printf("生日是否是数字集合%d中的一个(请回答是或否):", i);
String answer = scan.next();
if (answer.equals("是")) {
birth += Math.pow(2, i-1);
}
}
System.out.println("生日是:" + birth);
scan.close();
}
}
55题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个十六进制数字:");
String hex = scan.next();
int decimal = hexToDecimal(hex);
System.out.println(decimal);
}
private static int hexToDecimal(String hex) {
int decimal = 0;
int j = 0; 位数-1
for (int i = hex.length() - 1; i >= 0; i--) {
if (hex.charAt(i) <= '9') {
decimal += (hex.charAt(i) - '1' + 1) * (int) (Math.pow(16, j++));
} else {
decimal += (hex.charAt(i) - 'A' + 10) * (int) (Math.pow(16, j++));
}
}
return decimal;
}
56题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入从五边形中心到顶点的距离:");
double r = scan.nextDouble();
double area = opentagonArea(r);
System.out.println(area);
}
private static double opentagonArea(double r) {
double area;
double s = 2 * r * Math.sin(Math.PI / 5);
area = (5 * Math.pow(s, 2)) / (4 * Math.tan(Math.PI / 5));
return area;
}
57题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入多边形边的个数:");
double n = scan.nextDouble();
System.out.println("正多边形的边长:");
double s = scan.nextDouble();
double area = polygonArea(n, s);
System.out.println(area);
}
private static double polygonArea(double n, double s) {
return (n * Math.pow(s, 2)) / (4 * Math.tan(Math.PI / n));
}
58题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个0~127之间的整数:");
int num = scan.nextInt();
asciiToChar(num);
scan.close();
}
private static void asciiToChar(int num) {
System.out.println((char) num);
}
59题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入一个0~15之间的整数:");
int num = scan.nextInt();
if (num < 0 || num > 15) {
System.out.println(num + " 是一个无效输入");
}else if (num < 10) {
System.out.println("十六进制值为:" + num);
}else {
System.out.println("十六进制值为:" + (char)(num - 10 + 'A'));
}
scan.close();
}
60题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入第一个字符串:");
String s1 = scan.next();
System.out.print("请输入第二个字符串:");
String s2 = scan.next();
judgeStringSub(s1, s2);
}
private static void judgeStringSub(String s1, String s2) {
String maxStr = s1.length() > s2.length() ? s1: s2;
String minStr = s1.length() < s2.length() ? s1: s2;
int j = 0;
for (int i = 0; i <= maxStr.length(); i++) {
if (i > maxStr.length() - minStr.length() && j == 0) {
break;
}
if (j < minStr.length() && minStr.charAt(j) == maxStr.charAt(i)) {
j++;
}else if (j < minStr.length() && minStr.charAt(j) != maxStr.charAt(i)) {
j = 0;
}
}
if (j == minStr.length()) {
System.out.println(minStr + "是" + maxStr + "的" + "字串");
} else {
System.out.println(minStr + "不是" + maxStr + "的" + "字串");
}
}
61题
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter SSN:");
String ssn = input.nextLine();
System.out.println(isValidSSN(ssn));
}
public static boolean isValidSSN(String ssn) {
if (ssn.length() != 11) {
return false;
}
for (int i = 0; i < ssn.length(); i++) {
char c = ssn.charAt(i);
if (i == 3 || i == 6) {
if (c != '-') {
return false;
}
} else {
if (c < '0' || c > '9') {
return false;
}
}
}
return true;
}
62题
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("输入字符串:");
String s = in.nextLine();
System.out.println(reverse(s));
}
public static String reverse (String s) {
String res = "";
for (int i = s.length()-1; i >= 0; i--) {
res += s.charAt(i);
}
return res;
}
63题
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter ISBN:");
String s = input.nextLine();
if (isValid(s)) {
System.out.println(getISBN(s));
} else {
System.out.println("is invalid!");
}
}
public static boolean isValid(String s) {
if (s.length() != 12) {
return false;
}
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c < '0' || c > '9') {
return false;
}
}
return true;
}
public static String getISBN(String s) {
String isbn = s;
int sum = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i); '0' ~ 0 '9' ~ 9
int num = getNumber(c);
if (i % 2 == 1) {
sum = sum + 3 * num;
} else {
sum = sum + num;
}
}
int d13 = 10 - sum % 10;
if (d13 == 10) {
isbn = isbn + 0;
} else {
isbn = isbn + d13;
}
return isbn;
}
public static int getNumber(char c) {
return c - '0'; '2' - '0' 50-48 = 2
}
64题
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("输入字符串:");
String s = in.nextLine();
System.out.println(jishuwei(s));
}
public static String jishuwei (String s) {
String res = "";
for (int i = 0; i < s.length(); i += 2) {
res += s.charAt(i);
}
return res;
}
65题
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("输入字符串:");
String s = in.nextLine();
System.out.println(getUpperCase(s));
}
public static int getUpperCase (String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (isUpprtCase(s.charAt(i))) {
count++;
}
}
return count;
}
public static boolean isUpprtCase (char c) {
return c >= 'A' && c <= 'Z';
}
66题
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("输入字符串1:");
String s1 = in.nextLine();
System.out.print("输入字符串2:");
String s2 = in.nextLine();
System.out.print(getCommonPrefix(s1,s2));
}
public static String getCommonPrefix(String s1, String s2) {
String minStr = "";
String maxStr = "";
if (s1.length() <= s2.length()) {
minStr = s1;
maxStr = s2;
} else {
minStr = s2;
maxStr = s1;
}
for (int i = 0; i < minStr.length(); i++){
if (minStr.charAt(i) != maxStr.charAt(i)) {
return minStr.substring(0, i);
}
}
return minStr;
}
67题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个整数:");
long num = scan.nextLong();
int sum = sumDigits(num);
System.out.println(sum);
}
private static int sumDigits(long n) {
int sum = 0;
while (n > 0) {
sum += (int)(n % 10);
n /= 10;
}
return sum;
}
private static int sumDigits1(long n) {
int sum = 0;
if (n > 0 && n < 10) {
return (int)(n % 10);
}
return (int)(n % 10) + sumDigits1(n / 10);
}
68题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入一个整数:");
int num = scan.nextInt();
if (isPalindrome(num)) {
System.out.println(num + " 是回文数");
}else {
System.out.println(num + " 不是回文数");
}
scan.close();
}
public static int reverse(int number){
int reverseNum = 0;
while (number > 0) {
reverseNum *= 10;
if (number % 10 == number) {
return number + reverseNum;
}else {
reverseNum += (number % 10);
}
number /= 10;
}
return reverseNum;
}
public static boolean isPalindrome(int number) {
return reverse(number) == number;
}
69题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入要打印一个n x n的矩阵的n:");
int n = scan.nextInt();
printMatrix(n);
scan.close();
}
public static void printMatrix(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print((int)(Math.random() * 2) + " ");
}
System.out.println();
}
}
70题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入密码:");
String password = scan.next();
if (judgePassword(password)) {
System.out.println("valid Password!");
} else {
System.out.println("Invalid Password!");
}
scan.close();
}
private static boolean judgePassword(String password) {
if (password.length() < 8) {
return false;
} else {
int count = 0;
for (int i = 0; i < password.length(); i++) {
if (!(Character.isDigit(password.charAt(i)) || Character.isLetter(password.charAt(i)))) {
return false;
} else if (Character.isDigit(password.charAt(i))) {
count++;
}
}
return count >= 2;
}
}
71题
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入三角形的三边长:");
double side1 = scanner.nextInt();
double side2 = scanner.nextInt();
double side3 = scanner.nextInt();
if (isValid(side1, side2, side3)) {
double area = area(side1, side2, side3);
System.out.println("三角形的面积为:" + area);
} else {
System.out.println("输入无效!");
}
}
public static boolean isValid(double side1, double side2, double side3) {
return side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1;
}
public static double area(double side1, double side2, double side3) {
double s = (side1 + side2 + side3) / 2;
return Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
}
72题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入一个字符串:");
String str = scan.nextLine();
System.out.println(str + " 中的字母个数:" + countLetters(str));
scan.close();
}
public static int countLetters(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (Character.isLetter(s.charAt(i))) {
count++;
}
}
return count;
}
73题
public static void main(String[] args) {
System.out.println(sqrt(5));
}
public static double sqrt(long n) {
double lastGuess = 1;
double nextGuess = (lastGuess + n / lastGuess) / 2;
while (true) {
if (Math.abs(lastGuess - nextGuess) < 0.0001) {
break;
} else {
lastGuess = nextGuess;
nextGuess = (lastGuess + n / lastGuess) / 2;
}
}
return nextGuess;
}
74题
public static void main(String[] args) {
isPalindromeAndPrimeNum();
}
public static void isPalindromeAndPrimeNum() {
int count = 0;
for (int i = 2; i < 1000000; i++) {
boolean isFlag = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isFlag = false;
break;
}
}
if (isFlag && reverse(i) == i) {
count++;
System.out.print(i + " \t ");
if (count % 10 == 0) {
System.out.println();
}
}
if (count == 100) {
break;
}
}
}
public static int reverse(int number) {
int reverseNum = 0;
while (number > 0) {
reverseNum *= 10;
reverseNum += number % 10;
number /= 10;
}
return reverseNum;
}
75题
public static void main(String[] args) {
isEmirp();
}
private static void isEmirp() {
int count = 0;
for (int i = 2; i < 10000; i++) {
boolean isFlag = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isFlag = false;
break;
}
}
if (isFlag && reverse(i) != i && isPrimeNum(reverse(i))) {
count++;
System.out.print(i + " \t");
if (count % 10 == 0) {
System.out.println();
}
}
if (count == 100) {
break;
}
}
}
public static boolean isPrimeNum(int number) {
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
private static int reverse(int number) {
int reverseNum = 0;
while (number > 0) {
reverseNum = reverseNum * 10 + number % 10;
number /= 10;
}
return reverseNum;
}
76题
public static void main(String[] args) {
printMersennePrime();
}
private static void printMersennePrime() {
System.out.printf("%s\t\t%s%n","p", "2^p-1");
System.out.println("________________");
for (int p = 2; p <= 31; p++) {
if (isPrimeNum((int)((Math.pow(2, p)) - 1))) {
System.out.printf("%s\t\t%s%n",p, (int)((Math.pow(2, p)) - 1));
}
}
}
private static boolean isPrimeNum(int number) {
for (int i = 2; i < number / 2; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
77题
public static void main(String[] args) {
for (int i = 2; i < 1000; i++) {
if (isPrimeNum(i) && isPrimeNum(i + 2)) {
System.out.printf("(%s, %s)%n", i, i + 2);
}
}
}
private static boolean isPrimeNum(int num) {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
78题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入从五边形的边长:");
double side = scan.nextDouble();
double area = area(side);
System.out.println(area);
scan.close();
}
public static double area(double side) {
double area;
area = (5 * Math.pow(side, 2)) / (4 * Math.tan(Math.PI / 5));
return area;
}
79题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入正多边形边的个数:");
int n = scan.nextInt();
System.out.println("请输入正多边形的边长:");
double side = scan.nextDouble();
double area = area(n, side);
System.out.println("正多边形的面积为:" + area);
scan.close();
}
public static double area(int n, double side) {
return (n * Math.pow(side, 2)) / (4 * Math.tan(Math.PI / n));
}
80题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入项数:");
int n = scan.nextInt();
System.out.println(fibonacci(n));
scan.close();
}
private static int fibonacci(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
81题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入有几个方块:");
int n = scan.nextInt();
System.out.println("最少的步数:" + towerHanoi(n));
System.out.println("最少的步数:" + towerHanoi1(n));
scan.close();
}
private static int towerHanoi1(int n) {
if (n == 0) {
return 0;
}
return towerHanoi(n - 1) + (int)Math.pow(2, n - 1);
}
private static int towerHanoi(int n) {
if (n == 0) {
return 0;
}
return (int)Math.pow(2, n) - 1;
}
82题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the integers between 1 and 100:");
int[] arr = {};
while (true) {
int num = scan.nextInt();
if (num == 0) {
break;
}
arr = Arrays.copyOf(arr, arr.length + 1);
arr[arr.length - 1] = num;
}
System.out.println(Arrays.toString(arr));
scan.close();
countNum(arr);
}
private static void countNum(int[] arr) {
bubbleSort(arr);
int count = 1, num = arr[0];
for (int i = 1; i < arr.length; i++) {
if (num == arr[i]) {
count++;
} else {
System.out.println(num + " occurs " + count + (count == 1 ? " time" : " times"));
num = arr[i];
count = 1;
}
}
System.out.println(num + " occurs " + count + (count == 1 ? " time" : " times"));
}
private static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
83题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter ten number:");
int i = 1;
int[] nums = {scan.nextInt()};
while (i <= 9) {
int num = scan.nextInt();
boolean isFlag = true;
for (int j : nums) {
if (j == num) {
isFlag = false;
break;
}
}
if (isFlag) {
nums = Arrays.copyOf(nums, nums.length + 1);
nums[nums.length - 1] = num;
}
i++;
}
System.out.println("The number of distinct number is " + nums.length);
System.out.print("The distinct number are:");
for (int j: nums) {
System.out.print(j + " ");
}
}
84题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入10个小数:");
double[] nums = new double[10];
for (int i = 0; i < nums.length; i++) {
double num = scan.nextDouble();
nums[i] = num;
}
double aver = average(nums);
System.out.println("平均值为:" + aver);
}
public static int average(int[] array){
int sum = 0;
for (int i : array) {
sum += i;
}
return sum /array.length;
}
public static double average(double[] array){
double sum = 0;
for (double i : array) {
sum += i;
}
return sum /array.length;
}
85题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入5个数:");
int[] nums = new int[5];
for (int i = 0; i < nums.length; i++) {
int num = scan.nextInt();
nums[i] = num;
}
System.out.println(gcd(nums));
}
public static int gcd(int... numbers) {
int min = numbers[0];
for (int i : numbers){
if (min > i) {
min = i;
}
}
for (int i = min; i > 0; i--) {
boolean isFlag = true;
for (int j : numbers) {
if (j % i != 0) {
isFlag = false;
break;
}
}
if (isFlag) {
return i;
}
}
return 1;
}
86题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入10个数:");
int[] nums = new int[10];
for (int i = 0; i < nums.length; i++) {
int num = scan.nextInt();
nums[i] = num;
}
nums = eliminateDuplicates(nums);
for (int i : nums) {
System.out.print(i + " ");
}
scan.close();
}
public static int[] eliminateDuplicates(int[] list){
int[] arr = {list[0]};
for (int j : list) {
boolean isFlag = true;
for (int i = 0; i < arr.length; i++) {
if (j == arr[i]) {
isFlag = false;
break;
}
}
if (isFlag) {
arr = Arrays.copyOf(arr, arr.length + 1);
arr[arr.length - 1] = j;
}
}
return arr;
}
87题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入数字元素的个数和每一个元素:");
int n = scan.nextInt();
int[] list = new int[n];
for (int i = 0; i < n; i++) {
list[i] = scan.nextInt();
}
if (isSorted(list)) {
System.out.println("The list is already sorted");
} else {
System.out.println("The list is not sorted");
}
}
public static boolean isSorted(int[] list){
for (int i = 0; i < list.length - 1; i++) {
if (list[i] > list[i + 1]) {
return false;
}
}
return true;
}
88题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入球的个数:");
int ballNum = scan.nextInt();
System.out.print("请输入机器的槽数:");
int slotNum = scan.nextInt();
int[] slots = new int[slotNum];
simulator(ballNum, slotNum, slots);
}
private static void simulator(int ballNum, int slotNum, int[] slots) {
for (int i = 0; i < ballNum; i++) {
int count = 0;
for (int j = 1; j < slotNum; j++) {
String way = Math.random() >= 0.5 ? "L" : "R";
if (way.equals("L")) {
count++;
}
System.out.print(way + " ");
}
slots[slots.length - 1 - count] += 1;
System.out.println();
}
System.out.println();
int max = findMax(slots);
while (max > 0) {
for (int i = 0; i < slots.length; i++) {
if (slots[i] == max) {
System.out.print("0 ");
slots[i]--;
} else {
System.out.print(" ");
}
}
max--;
System.out.println();
}
}
private static int findMax(int[] slots) {
int max = slots[0];
for (int i : slots) {
if (max < i) {
max = i;
}
}
return max;
}
89题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入第1个列表的长度和每一个元素:");
int len1 = scan.nextInt();
int[] list1 = new int[len1];
for (int i = 0; i < list1.length; i++) {
list1[i] = scan.nextInt();
}
System.out.println(Arrays.toString(list1));
System.out.print("请输入第2个列表的长度和每一个元素:");
int len2 = scan.nextInt();
int[] list2 = new int[len2];
for (int i = 0; i < list2.length; i++) {
list2[i] = scan.nextInt();
}
System.out.println(Arrays.toString(list2));
if (equals(list1, list2)) {
System.out.println("列表1和列表2完全相同");
}else{
System.out.println("列表1和列表2不完全相同");
}
scan.close();
}
public static boolean equals(int[] list1,int[] list2) {
if (list1.length == list2.length) {
for (int i = 0; i < list1.length; i++) {
if (list1[i] != list2[i]) {
return false;
}
}
return true;
}
return false;
}
90题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of values:");
int len = scan.nextInt();
int[] values = new int[len];
System.out.print("Enter the values:");
for (int i = 0; i < values.length; i++) {
values[i] = scan.nextInt();
}
if (isConsecutiveFour(values)) {
System.out.println("This list has consecutive fours");
} else {
System.out.println("This list has no consecutive fours");
}
}
public static boolean isConsecutiveFour(int[] values) {
int count = 1, maxCount = count, currNum = values[0];
for (int i = 1; i < values.length; i++) {
if (currNum == values[i]) {
count++;
} else {
currNum = values[i];
count = 1;
}
if (maxCount < count) {
maxCount = count;
}
}
return maxCount >= 4;
}
91题
public static void main(String[] args) {
System.out.print("Enter list1:");
Scanner scanner = new Scanner(System.in);
int len1 = scanner.nextInt();
int[] list1 = new int[len1];
for (int i = 0; i < list1.length; i++) {
list1[i] = scanner.nextInt();
}
System.out.print("Enter list2:");
int len2 = scanner.nextInt();
int[] list2 = new int[len2];
for (int i = 0; i < list2.length; i++) {
list2[i] = scanner.nextInt();
}
System.out.println("The merged list is :" + Arrays.toString(merge(list1, list2)));
scanner.close();
}
public static int[] merge(int[] list1, int[] list2) {
int[] list = new int[list1.length + list2.length];
int i1 = 0, i2 = 0;
for (int i = 0; i < list.length; i++) {
if (i1 < list1.length && i2 < list2.length) {
if (list1[i1] < list2[i2]) {
list[i] = list1[i1++];
}else {
list[i] = list2[i2++];
}
} else if (i1 == list1.length) {
list[i] = list2[i2++];
} else if (i2 == list2.length) {
list[i] = list1[i1++];
}
}
return list;
}
91题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] words = {"write", "that", "where"};
while (true) {
String word = words[new Random().nextInt(words.length)];
String str = word;
for (int i = 0; i < word.length(); i++) {
str = str.replace(str.charAt(i), '*');
}
StringBuilder gsWord = new StringBuilder(str);
int count = 0;
while (true) {
System.out.print("(Guess) Enter a letter in word " + gsWord + "> ");
char letter = scan.next().charAt(0);
if (gsWord.toString().contains(letter + "")) {
System.out.println(letter + " is already in the word");
continue;
}
boolean change = false;
for (int i = 0; i < word.length(); i++) {
if (letter == word.charAt(i)) {
change = true;
gsWord.replace(i, i + 1, letter + "");
}
}
if (!change) {
count++;
System.out.println(letter + " is not in the word");
continue;
}
if (gsWord.toString().equals(word)) {
System.out.printf("This word is %s. You missed %d time%n", word, count);
System.out.println("Dou you want to guess another word? Enter y or n:");
if ("y".equals(scan.next())) {
break;
} else {
System.exit(1);
}
}
}
}
}
93题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a 3-by-4 matrix row by row:");
double[][] m = new double[3][4];
int len = 0;
while (len < m.length) {
for (int i = 0; i < m[len].length; i++) {
m[len][i] = scan.nextDouble();
}
len++;
}
for (int i = 0; i < 4; i++) {
System.out.println("Sum of the elements at column " + i + " is " + sumColumn(m, i));
}
scan.close();
}
public static double sumColumn(double[][] m, int columnIndex) {
double sum = 0.0;
for (double[] doubles : m) {
sum += doubles[columnIndex];
}
return sum;
}
94题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a 4-by-4 matrix row by row:");
double[][] m = new double[4][4];
int len = 0;
while (len < m.length) {
for (int i = 0; i < m[len].length; i++) {
m[len][i] = scan.nextDouble();
}
len++;
}
System.out.println("Sum of the elements in major diagonal is " + sumMajorDiagonal(m));
}
public static double sumMajorDiagonal (double[][] m) {
double sum = 0;
for (int i = 0; i < m.length; i++) {
sum += m[i][i];
}
return sum;
}
95题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter matrix1:");
double[][] a = new double[3][3];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = scan.nextDouble();
}
}
double[][] b = new double[3][3];
System.out.print("Enter matrix2:");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
b[i][j] = scan.nextDouble();
}
}
System.out.println("The matrices are added as follow:");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.print(i == 1 ? "\t+\t" : "\t\t");
for (int j = 0; j < a[0].length; j++) {
System.out.print(b[i][j] + " ");
}
System.out.print(i == 1 ? "\t=\t" : "\t\t");
for (int j = 0; j < a[0].length; j++) {
System.out.print(addMatrix(a, b)[i][j] + " ");
}
System.out.println();
}
scan.close();
}
public static double[][] addMatrix(double[][] a, double[][] b) {
double[][] c = new double[3][3];
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[i].length; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
return c;
}
96题
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[][] matrix1 = new double[3][3];
double[][] matrix2 = new double[3][3];
System.out.println("Enter matrix1:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++){
matrix1[i][j] = input.nextDouble();
}}
System.out.println("Enter matrix2:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++){
matrix2[i][j] = input.nextDouble();
}}
double[][] nums = multiplyMatrix(matrix1, matrix2);
System.out.println(matrix1[0][0] + " " + matrix1[0][1] + " " + matrix1[0][2] + " " + matrix1[0][0] + " " + matrix2[0][1] + " "
+ matrix2[0][2] + " " + nums[0][0] + " " + nums[0][1] + " " + nums[0][2]);
System.out.println(matrix1[1][0] + " " + matrix1[1][1] + " " + matrix1[1][2] + " * " + matrix1[1][0] + " " + matrix1[1][1] + " "
+ matrix2[1][2] + " = " + nums[1][0] + " " + nums[1][1] + " " + nums[1][2]);
System.out.println(matrix1[2][0] + " " + matrix1[2][1] + " " + matrix1[2][2] + " " + matrix2[2][0] + " " + matrix2[2][1] + " "
+ matrix2[2][2] + " " + nums[2][0] + " " + nums[2][1] + " " + nums[2][2]);
}
public static double[][] multiplyMatrix(double[][] a, double[][] b) {
double[][] nums = new double[a.length][b[0].length];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b[0].length; j++) {
for (int k = 0; k < a.length; k++){
nums[i][j] += a[i][k] * b[k][j];
nums[i][j] = Math.round(nums[i][j] * 10) / 10.0; 截取小数点后1位
}}
}
return nums;
}
97题
public static void main(String[] args) {
double[][] points = {
{ -1, 0, 3},
{ -1, -1, -1},
{ 4, 1, 1},
{ 2, 0.5, 9},
{ 3.5, 2, -1},
{ 3, 1.5, 3},
{-1.5, 4, 2},
{ 5.5, 4, -0.5}
};
double temp_ij = 0;
double temp_min = distance(points[0], points[1]);
double[] min_point1 = new double[3];
double[] min_point2 = new double[3];
for (int i = 0 ; i < points.length - 1; i++){
for (int j = i + 1 ; j < points.length ; j++){
temp_ij = distance(points[i], points[j]);
if ( temp_min > temp_ij ){
temp_min = temp_ij;
min_point1 = points[i];
min_point2 = points[j];
}
}
}
System.out.println(Arrays.toString(min_point1) + "\n" + Arrays.toString(min_point2));
}
public static double distance(double[] a, double[] b){
double temp_distance = 0;
for (int i = 0; i < a.length ; i++){
temp_distance += Math.pow(b[i] - a[i], 2);
}
return Math.sqrt(temp_distance);
}
98题
public static void main(String[] args){
int[][] nums = new int[4][4];
for(int i = 0; i < 4; ++i){
for(int j = 0; j < 4; ++j){
nums[i][j] = Math.abs((((int)System.currentTimeMillis()) / (i + j + 1)) % 2);
}
}
int rowIndex = 0;
int columnIndex =0 ;
int rcount = 0;
int rmaxCount = 0;
int ccount = 0, cmaxCount = 0;
for(int i = 0; i < 4; ++i){
rcount = 0;
ccount = 0;
for(int j = 0; j < 4; ++j){
if(nums[i][j] == 1)
++rcount;
}
if(rcount > rmaxCount){
rmaxCount = rcount;
rowIndex = i;
}
for(int j = 0; j < 4; ++j){
if(nums[j][i] == 1)
++ccount;
}
if(ccount > cmaxCount){
cmaxCount = ccount;
columnIndex = i;
}
}
for(int i = 0; i < 4; ++i){
for(int j = 0; j < 4; ++j){
System.out.print(nums[i][j] + " ");
}
System.out.println();
}
System.out.println("The largest row index:" + rowIndex);
System.out.println("The largest column index:" + columnIndex);
}
99题
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number between 0 and 511: ");
int number = scanner.nextInt();
if (number < 511 && number > 0) {
char[] binaryChars = toBinaryChars(number);
for (int i = 1; i <= binaryChars.length; i++) {
System.out.print(((binaryChars[i - 1] == '0') ? "H" : "T") + " ");
if (i % 3 == 0) {
System.out.println();
}
}
}else {
System.out.print("Please enter Numbers from 0 to 511");
}
}
public static char[] toBinaryChars(int number) {
char[] result = new char[9];
int i = result.length - 1;
while (number != 0) {
if (number % 2 == 0) {
result[i--] = '0';
} else {
result[i--] = '1';
}
number /= 2;
}
for (int k = i; k >= 0; k--) {
result[k] = '0';
}
return result;
}
100题
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入数组行数、列数:");
int array_row = input.nextInt();
int array_col = input.nextInt();
int[][] array = new int[array_row][array_col];
System.out.println("请输入数组元素:");
for (int i = 0 ; i < array_row ; i++){
for (int j = 0 ; j < array_col ; j++){
array[i][j] = input.nextInt();
}
}
if ( isConsecutiveFour(array) ){
System.out.println("true");
} else {
System.out.println("false");
}
}
public static boolean isConsecutiveFour(int[][] values){
int row = values.length;
int col = values[0].length;
for (int i = 0 ; i < row ; i++){
for (int j = 0 ; j < col - 3 ; j++){
if ( values[i][j] == values[i][j+1] && values[i][j+1] == values[i][j+2]
&& values[i][j+2] == values[i][j+3])
return true;
}
}
for (int j = 0 ; j < col ; j++){
for (int i = 0 ; i < row - 3 ; i++){
if ( values[i][j] == values[i+1][j] && values[i+1][j] == values[i+2][j]
&& values[i+2][j]== values[i+3][j])
return true;
}
}
for (int i = 3 ; i < row ; i++){
for (int j = 0 ; j < col - 3 ; j++){
if (values[i][j] == values[i-1][j+1] && values[i-1][j+1] == values[i-2][j+2]
&& values[i-2][j+2]== values[i-3][j+3])
return true;
}
}
for (int i = 0 ; i < row - 3 ; i++){
for (int j = 0 ; j < col - 3 ; j++){
if (values[i][j] == values[i+1][j+1] && values[i+1][j+1] == values[i+2][j+2]
&& values[i+2][j+2] == values[i+3][j+3])
return true;
}
}
8. 方法isConsecutiveFour:如果找到返回true,找不到返回false
return false;
}
101题
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
char[][] chess = new char[6][15];
for(int i = 0; i < chess.length; i++)
{
for(int j = 0; j < chess[i].length; j++)
{
if(j % 2 == 0)
chess[i][j] = '|';
else
chess[i][j] = ' ';
}
}
beginGame(chess);
in.close();
}
public static void beginGame(char[][] chess)
{
char disk;
for(int i = 0; i < 42; i++)
{
if(i % 2 == 0)
disk = 'R';
else
disk = 'Y';
printChessBoard(chess);
System.out.println("——————————————");
if(dropDisk(chess,disk) == 0)
{
i--;
continue;
}
if(judge(chess) == 0)
{
System.out.println("The red player won");
return;
}
else if(judge(chess) == 1)
{
System.out.println("The yellow player won");
return;
}
}
System.out.println("No winner");
}
public static int dropDisk(char[][] chess,char disk)
{
if(disk == 'R')
System.out.print("Drop a red disk at column(0-6):");
else
System.out.print("Drop a yellow disk at column(0-6):");
int key = in.nextInt()*2+1;
if(key < 0 || key > 6)
{
System.out.println("Enter error!");
return 0;
}
for(int row = chess.length - 1; row >= 0; row--)
{
if(chess[row][key] == ' ')
{
chess[row][key] = disk;
return 1;
}
}
System.out.println("This colum is no more position!");
return 0;
}
public static void printChessBoard(char[][] chess)
{
for(int i = 0; i < chess.length; i++)
{
for(int j = 0; j < chess[i].length; j++)
System.out.print(chess[i][j]);
System.out.println();
}
}
public static int judge(char[][] chess)
{
if(judgeRow(chess) != 2)
return judgeRow(chess);
if(judgeCol(chess) != 2)
return judgeCol(chess);
if(judgeMainDiagonal(chess) != 2)
return judgeMainDiagonal(chess);
if(judgeSubDiagonal(chess) != 2)
return judgeSubDiagonal(chess);
return 2;
}
public static int judgeRow(char[][] chess)
{
for(int i = 0; i < chess.length; i++)
for(int j = 1; j < chess[i].length - 6; j+=2)
{
if(chess[i][j] == chess[i][j+2] && chess[i][j] == chess[i][j+4] && chess[i][j] == chess[i][j+6] && chess[i][j] != ' ')
if(chess[i][j] == 'R')
return 0;
else
return 1;
}
return 2;
}
public static int judgeCol(char[][] chess)
{
for(int i = 0; i < chess.length - 3; i++)
for(int j = 1; j < chess[i].length; j+=2)
{
if(chess[i][j] == chess[i+1][j] && chess[i][j] == chess[i+2][j] && chess[i][j] == chess[i+3][j] && chess[i][j] != ' ')
if(chess[i][j] == 'R')
return 0;
else
return 1;
}
return 2;
}
public static int judgeMainDiagonal(char[][] chess)
{
for(int i = 0; i < chess.length - 3; i++)
for(int j = 1; j < chess[i].length - 6; j+=2)
{
if(chess[i][j] == chess[i+1][j+2] && chess[i][j] == chess[i+2][j+4] && chess[i][j] == chess[i+3][j+6] && chess[i][j] != ' ')
if(chess[i][j] == 'R')
return 0;
else
return 1;
}
return 2;
}
public static int judgeSubDiagonal(char[][] chess)
{
for(int i = 3; i < chess.length; i++)
for(int j = 1; j < chess[i].length - 6; j+=2)
{
if(chess[i][j] == chess[i-1][j+2] && chess[i][j] == chess[i-2][j+4] && chess[i][j] == chess[i-3][j+6] && chess[i][j] != ' ')
if(chess[i][j] == 'R')
return 0;
else
return 1;
}
return 2;
}
102题
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter a 3-by-3 matrix row bay row:");
double[][] m =new double[3][3];
for(int i = 0; i < m.length; i++)
for(int j = 0; j < m[i].length; j++)
m[i][j] = in.nextDouble();
if(isMarkovMatrix(m))
System.out.println("It is a Markov matrix");
else
System.out.println("It is not a Markov matrix");
in.close();
}
public static boolean isMarkovMatrix(double[][] m)
{
for(int i = 0; i < m.length; i++)
{
double sum = 0;
for(int j = 0; j < m[i].length; j++)
{
if(m[j][i] < 0)
return false;
sum += m[j][i];
}
if(sum != 1)
return false;
}
return true;
}
103题
public static void main(String[] args) {
areaOfTriangle();
}
public static void areaOfTriangle() {
try(Scanner input = new Scanner(System.in);) {
final int ROW = 3, COLUMN = 2;
double[][] points = new double[ROW][COLUMN];
System.out.print("Enter x1, y1, x2, y2, x3, y3: ");
inputData(input, points);
double area = getTriangleArea(points);
if(Math.abs(area) < 0.0001)
System.out.println("The three points are on the same line");
else
System.out.printf("The area of the triangle is %.2f\n", area);
}
}
public static void inputData(Scanner input, double[][] points) {
for (int i = 0; i < points.length; i++)
for (int j = 0; j < points[i].length; j++)
points[i][j] = input.nextDouble();
}
public static double getTriangleArea(double[][] points) {
Point2D[] point = new Point2D[3];
for (int i = 0; i < point.length; i++)
point[i] = new Point2D(points[i][0], points[i][1]);
return isOnTheSameLine(point[0], point[1], point[2]) ? 0 : getArea(point);
}
private static double getArea(Point2D...points) {
double side1 = points[0].distance(points[1]);
double side2 = points[0].distance(points[points.length - 1]);
double side3 = points[1].distance(points[points.length - 1]);
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
public static boolean isOnTheSameLine(Point2D p1, Point2D p2, Point2D p0) {
double k = (p1.getX() - p0.getX()) * (p2.getY() - p0.getY()) -
(p2.getX() - p0.getX()) * (p1.getY() - p0.getY());
return Math.abs(k) < 0.00001;
}
104题
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter x1,y1,x2,y2,x3,y3,x4,y4");
double[][] points=new double[4][2];
for(int i=0;i<points.length;i++){
for(int j=0;j<points[i].length;j++){
points[i][j]=input.nextDouble();
}
}
double x1=points[0][0];
double y1=points[0][1];
double x2=points[1][0];
double y2=points[1][1];
double x3=points[2][0];
double y3=points[2][1];
double x4=points[3][0];
double y4=points[3][1];
double[] intersection=new double[2];
double a=y1-y3;
double b=-(x1-x3);
double c=y2-y4;
double d=-(x2-x4);
double e=(y1-y3)*x1-(x1-x3)*y1;
double f=(y2-y4)*x2-(x2-x4)*y2;
intersection[0]=(e*d-b*f)/(a*d-b*c);
intersection[1]=(a*f-e*c)/(a*d-b*c);
double[] area=new double[4];
double triangle1L1=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
double triangle1L2=Math.sqrt((x1-intersection[0])*(x1-intersection[0])+(y1-intersection[1])*(y1-intersection[1]));
double triangle1L3=Math.sqrt((x2-intersection[0])*(x2-intersection[0])+(y2-intersection[1])*(y2-intersection[1]));
double triangle1S=(triangle1L1+triangle1L2+triangle1L3)/2;
area[0]=Math.sqrt(triangle1S*(triangle1S-triangle1L1)*(triangle1S-triangle1L2)*(triangle1S-triangle1L3));
double triangle2L1=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
double triangle2L2=Math.sqrt((x3-intersection[0])*(x3-intersection[0])+(y3-intersection[1])*(y3-intersection[1]));
double triangle2L3=Math.sqrt((x2-intersection[0])*(x2-intersection[0])+(y2-intersection[1])*(y2-intersection[1]));
double triangle2S=(triangle2L1+triangle2L2+triangle2L3)/2;
area[1]=Math.sqrt(triangle2S*(triangle2S-triangle2L1)*(triangle2S-triangle2L2)*(triangle2S-triangle2L3));
double triangle3L1=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4));
double triangle3L2=Math.sqrt((x3-intersection[0])*(x3-intersection[0])+(y3-intersection[1])*(y3-intersection[1]));
double triangle3L3=Math.sqrt((x4-intersection[0])*(x4-intersection[0])+(y4-intersection[1])*(y4-intersection[1]));
double triangle3S=(triangle3L1+triangle3L2+triangle3L3)/2;
area[2]=Math.sqrt(triangle3S*(triangle3S-triangle3L1)*(triangle3S-triangle3L2)*(triangle3S-triangle3L3));
double triangle4L1=Math.sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4));
double triangle4L2=Math.sqrt((x1-intersection[0])*(x1-intersection[0])+(y1-intersection[1])*(y1-intersection[1]));
double triangle4L3=Math.sqrt((x4-intersection[0])*(x4-intersection[0])+(y4-intersection[1])*(y4-intersection[1]));
double triangle4S=(triangle4L1+triangle4L2+triangle4L3)/2;
area[3]=Math.sqrt(triangle4S*(triangle4S-triangle4L1)*(triangle4S-triangle4L2)*(triangle4S-triangle4L3));
double temp=0;
for(int i=0;i<area.length-1;i++){
for(int j=0;j<area.length-1-i;j++){
if (area[j]>area[j+1]){
temp=area[j];
area[j]=area[j+1];
area[j+1]=temp;
}
}
}
System.out.print("The areas are ");
for(int i=0;i<area.length;i++)
System.out.printf("%.2f ",area[i]);
}
105题
public static void main(String args[]){
System.out.print("Enter number n:");
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
char[][] matrix = new char[n][n];
System.out.println("Enter " + n + " rows of letters separated by spaces:");
String temp = null;
for(int i = 0; i < n; ++i){
int count = 0;
for(int j = 0; j < n; ++j){
temp = cin.next();
matrix[i][j] = temp.charAt(0);
if (matrix[i][j] > ('A' + n) || matrix[i][j] < 'A')
count++;
}
if (count != 0) {
System.out.println("Wrong input: the letters must be from A to C");
return;
}
}
int[] record = new int[n];
Arrays.fill(record, 0);
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
++record[(int)(matrix[i][j]-'A')];
}
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
++record[(int)(matrix[j][i] - 'A')];
}
}
boolean flag = true;
for(int i = 0; i < n; ++i){
if(record[i] != 2 * n){
flag = false;
}
}
if (flag)
System.out.println("The input array is Latin squre");
}
106题
public static void main(String[] args) {
Rectangle1 rectangle = new Rectangle1(4, 40);
System.out.println("宽为4高为40的矩形:");
System.out.print("周长为:" + rectangle.getPerimeter() + ";");
System.out.println("面积为:" + rectangle.getArea());
System.out.println("宽为3.5高为35.9的矩形:");
Rectangle1 rectangle1 = new Rectangle1(3.5,35.9);
System.out.print("周长为:" + rectangle1.getPerimeter() + ";");
System.out.print("面积为:" + rectangle1.getArea());
}
}
public class Rectangle1 {
double width = 1;
double height = 1;
Rectangle1(double newwidth, double newheight){
width = newwidth;
height = newheight;
}
double getPerimeter() {
return 2 * (width + height);
}
double getArea() {
return width * height;
}
107题
public static void main(String[] args){
StopWatch time = new StopWatch();
int[] nums = new int[100000];
for(int i = 1; i <= 100000; ++i)
nums[i - 1] = (int)((System.currentTimeMillis() / i) % 1000);
time.start();
for(int i = 0; i < 99999; ++i){
for(int j = i+1; j < 100000; ++j){
if(nums[i] > nums[j]){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
time.stop();
System.out.println(time.getElapsedTime());
}
}
class StopWatch{
private long startTime;
private long endTime;
public void setStartTime(long newStartTime){
startTime = newStartTime;
}
public void setEndTime(long newEndTime){
endTime = newEndTime;
}
public StopWatch(){
startTime = System.currentTimeMillis();
}
public void start(){
startTime = System.currentTimeMillis();
}
public void stop(){
endTime = System.currentTimeMillis();
}
public long getElapsedTime(){
return endTime - startTime;
}
108题
public static void main(String[] args) {
Fan fan1=new Fan();
Fan fan2=new Fan();
fan1.setSpeed(Fan.FAST);
fan1.setRadius(10);
fan1.setColor("yellow");
fan1.setOn(true);
fan2.setSpeed(Fan.MEDIUM);
fan2.setRadius(5);
fan2.setColor("blue");
fan2.setOn(false);
System.out.println(fan1.toString());
System.out.println(fan2.toString());
}
}
class Fan{
static final int SLOW=1;
static final int MEDIUM=2;
static final int FAST=3;
private int speed=SLOW;
private boolean on=false;
private double radius=5;
private String color="blue";
public Fan() {}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
if(!on) {
return "fan is off";
}
return "风扇的速度:"+speed+"\n颜色:"+color+"\n半径:"+radius;
}
109题
private int n=3;
private double side=1;
private double x=0;
private double y=0;
Regularpolygon(){
}
Regularpolygon(int newN,int newS){
n=newN;
side=newS;
x=0;
y=0;
}
Regularpolygon(int newN,int newS, double newX, double newY){
n=newN;
side= newS;
x=newX;
y=newY;
}
public void setN(int newN){
n=newN;
}
public void setSide(double newS){
side=newS;
}
public void setX(double newX){
x=newX;
}
public void setY(double newY){
y=newY;
}
public int getN(){
return n;
}
public double getSide(){
return side;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
public double getPerimeter(){
return n*side;
}
public double getArea(){
return (n* side* side)/(4*Math.tan(getPerimeter()/n));
}
public class XiTi89 {
public static void main(String[] args) {
Regularpolygon r1=new Regularpolygon();
System.out.println(r1);
System. out.println("对象一周 长: "+r1.getPerimeter()+" 面 积: "+r1.getArea());
Regularpolygon r2=new Regularpolygon(6,4);
System.out.println("对象二周长: "+r2.getPerimeter()+"面积: "+r2.getArea());
Regularpolygon r3=new Regularpolygon(10,4,5.6,7.8);
System.out.println("对象三周长: "+r3.getPerimeter()+"面积: "+r3.getArea());
}
}
110题
private double a = 0;
private double b = 0;
private double c = 0;
public Equation() {
this.a = a;
this.b = b;
this.c = c;
}
public double getA() {
return a;
}
public double getB() {
return b;
}
public double getC() {
return c;
}
public double getDiscriminant(double a,double b,double c) {
return (b*b - 4*a*c);
}
public double getRoot1(double a,double b,double c) {
double r1 = (-b + Math.sqrt(b*b - 4*a*c)) / 2 * a;
return r1;
}
public double getRoot2(double a,double b,double c) {
double r2 = (-b - Math.sqrt(b*b - 4*a*c)) / 2 * a;
return r2;
}
}
public class QuadraticEquation {
public static void main(String[] args) {
Equation equation = new Equation();
Scanner input = new Scanner(System.in);
System.out.print("Please enter three number a,b,c: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
System.out.print("要求解的方程为:" + a + "*x*x + " +
b + "*x + " + c + " = 0");
System.out.println("\n方程的判别式为:" + equation.getDiscriminant(a,b,c));
if(equation.getDiscriminant(a,b,c) >= 0) {
System.out.println("\n方程的根为:" + equation.getRoot1(a,b,c) +
" " + equation.getRoot2(a,b,c));
}else {
System.out.println("The equation has no roots...");
}
}
111题
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of rows and columns in the array:");
int rows = input.nextInt(), cols = input.nextInt();
double[][] arr = new double[rows][cols];
System.out.println("Enter the array:");
for (int a = 0 ; a < rows ; a++){
for (int b = 0 ; b < cols ; b++){
arr[a][b] = input.nextDouble();
}
}
Test13_Location l = new Test13_Location();
l = locateLargest(arr);
System.out.println("The location of the largest elements is " + Test13_Location.maxValue +
" at (" + Test13_Location.row + ", " + Test13_Location.column + ")");
}
public static Test13_Location locateLargest(double[][] a){
Test13_Location l = new Test13_Location();
for (int m = 0 ; m < a.length ; m++){
for (int n = 0 ; n < a[0].length ; n++){
if (Test13_Location.maxValue < a[m][n]){
Test13_Location.maxValue = a[m][n];
Test13_Location.row = m;
Test13_Location.column = n;
}
}
}
return l;
}
112题
public Time() {
time = System.currentTimeMillis() + 8 * 60 * 60 * 1000;
hour = time / 1000 / 3600 % 24;
minute = time / 1000 % 3600 / 60;
second = time / 1000 % 3600 % 60;
}
public long getHour() {
return hour;
}
public void setHour(long hour) {
this.hour = hour;
}
public long getMinute() {
return minute;
}
public void setMinute(long minute) {
this.minute = minute;
}
public long getSecond() {
return second;
}
public void setSecond(long second) {
this.second = second;
}
private long hour;
private long minute;
private long second;
private long time;
public Time(long time) {
hour = time / 1000 / 3600 % 24;
minute = time / 1000 % 3600 / 60;
second = time / 1000 % 3600 % 60;
}
public Time(long hour, long minute, long second) {
this.hour=hour;
this.second=second;
this.minute=minute;
}
public void setTime(long elapseTime) {
TODO implement here
time = elapseTime;
}
}
public static void main(String[] args) {
Time time=new Time();
Time tim1=new Time(555550000);
System.out.println("The current time is "+time.getHour()+" : "+time.getMinute()+" : "+time.getSecond());
System.out.print("The input millisecond conversion time is "+tim1.getHour()+" : "+tim1.getMinute()+" : "+tim1.getSecond());
}
113题
public MyInteger() {
}
private int value;
public MyInteger(int value) {
this.value = value;
}
public int get() {
return this.value;
}
public boolean isEven() {
TODO implement here
if (get() % 2 == 0)
return true;
else
return false;
}
public boolean isOdd() {
TODO implement here
if (get() % 2 == 1)
return true;
else
return false;
}
public boolean isPrime() {
boolean is = true;
for (int i = 2; i < get(); i++) {
if (get() % i == 0) {
is = false;
break;
}
}
return is;
}
public static boolean isEven(int value) {
if (value % 2 == 0)
return true;
else
return false;
}
public static boolean isOdd(int value) {
if (value % 2 == 1)
return true;
else
return false;
}
public static boolean isPrime(int value) {
boolean is = true;
for (int i = 2; i < value; i++) {
if (value % i == 0) {
is = false;
break;
}
}
return is;
}
public static boolean isEven(MyInteger value) {
if (value.get() % 2 == 0)
return true;
else
return false;
}
public static boolean isOdd(MyInteger value) {
if (value.get() % 2 == 1)
return true;
else
return false;
}
public static boolean isPrime(MyInteger value) {
boolean is = true;
for (int i = 2; i < value.get(); i++) {
if (value.get() % i == 0) {
is = false;
break;
}
}
return is;
}
public boolean equals(int value) {
return value==get();
}
public boolean equals(MyInteger value) {
return value.value==get();
}
public static int parseInt(char[] c) {
return Integer.parseInt(new String(c));
}
public static int parseInt(String str) {
return Integer.parseInt(str);
}
}
public class Test113 {
public static void main(String[] args) {
char[] a = {'1', '2', '3'};
String b = "123";
MyInteger myInteger=new MyInteger(6);
MyInteger myInteger1=new MyInteger(7);
System.out.println("The "+myInteger.get()+" is odd : "+myInteger.isOdd());
System.out.println("The "+myInteger.get()+" is even : "+myInteger.isEven());
System.out.println("The "+myInteger.get()+" is prime : "+myInteger.isPrime());
System.out.println("The int is odd : "+MyInteger.isOdd(5));
System.out.println("The int is even : "+MyInteger.isEven(5));
System.out.println("The int is prime : "+MyInteger.isPrime(5));
System.out.println("The MyInteger is odd : "+MyInteger.isOdd(myInteger));
System.out.println("The MyInteger is even: "+MyInteger.isEven(myInteger));
System.out.println("The MyInteger is prime : "+MyInteger.isPrime(myInteger));
System.out.println("The int is equal to MyInteger: "+myInteger.equals(myInteger1.get()));
System.out.println("The int is equal to MyInteger: "+myInteger.equals(myInteger1));
System.out.println(MyInteger.parseInt(a));
System.out.println(MyInteger.parseInt(b));
}
114题
private double x;
private double y;
public MyPoint() {
this.x = 0;
this.y = 0;
}
public MyPoint(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return this.x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return this.y;
}
public void setY(double y) {
this.y = y;
}
public static double distance(MyPoint p1, MyPoint p2) {
return Math.sqrt((p1.getX() - p2.getX()) * (p1.getX() - p2.getX()) + (p1.getY() - p2.getY()) * (p1.getY() - p2.getY()));
}
public double distance(MyPoint p) {
return distance(this, p);
}
public double distance(double x, double y) {
MyPoint m = new MyPoint();
m.setX(x);
m.setY(y);
return distance(this, m);
}
}
public class testPoint {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MyPoint p1 = new MyPoint();
MyPoint p2 = new MyPoint(input.nextDouble(), input.nextDouble());
System.out.println(MyPoint.distance(p1, p2));
System.out.println(p1.distance(p2));
System.out.println(p2.distance(p1));
System.out.println(p2.distance(5.8, 6.0));
}
115题
public static void main(String[] args) {
Test10_Queue queue = new Test10_Queue();
for (int i = 1 ; i <= 20 ; i++){
queue.enqueue(i);
}
for (int i = 1 ; i <= 20 ; i++){
System.out.print(queue.dequeue()+ " ");
}
}
}
public class Test10_Queue {
private int[] element;
private int size = 0;
public Test10_Queue(){
element = new int[8];
}
public void enqueue(int v){
if (size == element.length){
element = enlarge(element);
}
element[size] = v;
size++;
}
public int[] enlarge(int[] arr){
int[] temp = new int[size * 2];
for (int i = 0 ; i < size ; i++){
temp[i] = arr[i];
}
arr = temp;
return arr;
}
public int dequeue(){
int temp = element[0];
System.arraycopy(element, 1, element, 0, size);
if (size == element.length){
element[size--] = 0;
}
return temp;
}
public boolean empty(){
boolean bool = false;
if (element[0] == 0){
bool = true;
}
return bool;
}
public int getSize(){
return size;
}
116题
public Circle2D() {
this.x=0;
this.y=0;
this.radius=1;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getRadius() {
return radius;
}
private double x;
private double y;
private double radius;
public Circle2D(double x, double y, double radius) {
this.x=x;
this.y=y;
this.radius=radius;
}
public double getArea() {
return Math.PI*radius*radius;
}
public double getPerimeter() {
return 2*Math.PI*radius;
}
public boolean contains(double x, double y) {
return Math.pow((x-this.x),2)+Math.pow((y-this.y),2)<Math.pow(this.radius,2);
}
public boolean contains(Circle2D circle) {
return Math.pow((circle.x-this.x),2)+Math.pow((circle.y-this.y),2)<=Math.abs(circle.radius-this.radius);
}
public boolean overlaps(Circle2D circle) {
return Math.pow((circle.x-this.x),2)+Math.pow((circle.y-this.y),2)>Math.abs(circle.radius-this.radius)&&Math.pow((circle.x-this.x),2)+Math.pow((circle.y-this.y),2)<circle.radius+this.radius;
}
}
public class Test116 {
public static void main(String[] args) {
Circle2D c1=new Circle2D(2,2,5.5);
System.out.println("The c1's area is "+c1.getArea());
System.out.println("The c1's perimeter is "+c1.getPerimeter());
System.out.println("The points contains is "+c1.contains(3,3));
System.out.println("The circle contains is "+c1.contains(new Circle2D(4,5,10.5)));
System.out.println("The circle contains is "+c1.contains(new Circle2D(3,5,2.3)));
}
117题
char[] ch;
public Test117_MyString1(char[] chars){
ch = chars;
}
public char charAt(int index){
return ch[index];
}
public int length(){
return ch.length;
}
public Test117_MyString1 substring(int begin, int end){
char[] feedback = new char[end - begin];
for (int i = 0 ; begin < end ; i++){
feedback[i] = ch[begin];
begin++;
}
return new Test117_MyString1(feedback);
}
public Test117_MyString1 toLowerCase(){
int temp = 0;
for (int i = 0 ; i < ch.length ; i++){
temp = ch[i];
if (temp >= 65 && temp <= 90){
temp += 32;
ch[i] = (char) temp;
}
}
return new Test117_MyString1(ch);
}
public boolean equals(Test117_MyString1 s){
boolean result = true;
int thisLength = ch.length;
int sLength = s.ch.length;
if (thisLength != sLength)
return false;
for (int i = 0 ; i < thisLength ; i++){
if (ch[i] != s.ch[i]){
result = false;
}
}
return result;
}
public static Test117_MyString1 valueOf(int i){
char[] feedback = {(char) i};
return new Test117_MyString1(feedback);
118题
char[] ch;
public Test118_MyString2(String s){
ch = new char[s.length()];
for (int i = 0 ; i < s.length() ; i++){
ch[i] = s.charAt(i);
}
}
public int compare(String s){
int count = 0;
int chLen = ch.length;
int sLen = s.length();
if (chLen != sLen){
return 0;
}
for (int i = 0 ; i < chLen ; i++){
if (ch[i] == s.charAt(i)){
count++;
}
}
return count;
}
public Test118_MyString2 substring(int begin){
String feedback = "";
for (; begin < ch.length ; begin++){
feedback += ch[begin];
}
return new Test118_MyString2(feedback);
}
public Test118_MyString2 toUpperCase(){
int temp = 0;
for (int i = 0 ; i < ch.length ; i++){
temp = ch[i];
if (temp >= 98 && temp <= 133){
temp -= 33;
ch[i] = (char) temp;
}
}
String feedback = "";
for (int i = 0 ; i < ch.length ; i++){
feedback += ch[i];
}
return new Test118_MyString2(feedback);
}
public char[] toChars(){
return ch;
}
public static Test118_MyString2 valueOf(boolean b){
return new Test118_MyString2((b)?"1":"0");
}
120题
private String name;
private String address;
private String number;
private String Email;
protected Person() {
}
protected Person(String name,String address,String number, String Email) {
this.name = name;
this.address = address;
this.number = number;
this.Email = Email;
}
protected String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
protected String getAddress() {
return address;
}
protected void setAddress(String address) {
this.address = address;
}
protected String getNumber() {
return number;
}
protected void setNumber(String number) {
this.number = number;
}
protected String getEmail() {
return Email;
}
protected void setEmail(String Email) {
this.Email = Email;
}
@Override
public String toString() {
return "ObejectByPerson"+"Name:"+getName();
}
}
class Student extends Person{
public final static String ONE = "Freshman";
public final static String TWO = "Sophomore";
public final static String THREE = "Junior";
public final static String FOUR = "Senior";
private String StudentState;
public Student() {
StudentState = ONE;
}
public Student(String StudentState,String name,String address,String number, String Email) {
super(name,address,number,Email);
this.StudentState = StudentState;
}
public String getStudentState() {
return StudentState;
}
public void setStudentState(String StudentState) {
this.StudentState = StudentState;
}
public String toStringOfAllInforation() {
return "Student is inforation:Grade:"+getStudentState()+"\tName:"+getName()+"\tAddress:"+getAddress()+"\tNumber:"+getNumber()+"\tEmail:"+getEmail();
}
@Override
public String toString() {
return "ObjectByStudent-->"+"Name:"+getName();
}
}
class Employee extends Person{
private String Office;
private double salary;
protected Employee() {
}
protected Employee(String Office,double salary,String name,String address,String number, String Email) {
super(name,address,number,Email);
this.Office = Office;
this.salary = salary;
}
protected String getOffice() {
return Office;
}
protected void setOffice(String Office) {
this.Office = Office;
}
protected double getSalary() {
return salary;
}
protected void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "ObjectByEmployee-->"+"Name:"+getName();
}
}
class Faculty extends Employee{
private String OfficeTime;
private String PostGrade;
public Faculty() {
}
public Faculty(String Office,double salary,String OfficeTime,String PostGrade,String name,String address,String number, String Email) {
super(Office,salary,name,address,number,Email);
this.OfficeTime = OfficeTime;
this.PostGrade = PostGrade;
}
public String getOfficeTime() {
return OfficeTime;
}
public void setOfficeTime(String OfficeTime) {
this.OfficeTime = OfficeTime;
}
public String getPostGrade() {
return PostGrade;
}
public void setPostGrade(String PostGrade) {
this.PostGrade = PostGrade;
}
public String toStringOfAllInforation() {
return "Faculty is inforation:PostGrade:"+getPostGrade()+"\tOfficeTime:"+getOfficeTime()+"\tSalary:"+getSalary()+"Office:"+getOffice()+"\tName:"+getName()+"\tAddress:"+getAddress()+"\tNumber:"+getNumber()+"\tEmail:"+getEmail();
}
@Override
public String toString() {
return "ObjectByFaculty-->"+"Name:"+getName();
}
}
class Staff extends Employee{
private String Post;
public Staff() {
}
public Staff(String Office,double salary,String Post,String name,String address,String number, String Email) {
super(Office,salary,name,address,number,Email);
this.Post = Post;
}
public String getPost() {
return Post;
}
public void setPost(String Post) {
this.Post = Post;
}
public String toStringOfAllInforation() {
Return "Staff is inforation:Post:"+getPost()+"\tSalary:"+getSalary()+"Office:"+getOffice()+"\tName:"+getName()+"\tAddress:"+getAddress()+"\tNumber:"+getNumber()+"\tEmail:"+getEmail();
}
@Override
public String toString() {
return "ObjectByStaff-->"+"Name:"+getName();
}
120题
private double side1;
private double side2;
private double side3;
public Triangle(){
this.side1=1;
this.side2=1;
this.side3=1;
}
public Triangle(double newSide1,double newSide2,double newSide3) {
this.side1=newSide1;
this.side2=newSide2;
this.side3=newSide3;
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
public double getArea() {
double P=(this.side1+this.side2+this.side3)/2;
double S=Math.sqrt(P*(P-this.side1)*(P-this.side2)*(P-this.side3));
return S;
}
public double getPerimeter() {
return this.side1+this.side2+this.side3;
}
public String toString() {
return "Triangle:side1 = "+this.side1+" side2 = "+this.side2+" side3 = "+this.side3;
}
public static void main(String[] args) {
double side1,side2,side3;
String color;
boolean filled;
Scanner input = new Scanner(System.in);
System.out.println("请输入三角形的三条边:");
side1=input.nextDouble();
side2=input.nextDouble();
side3=input.nextDouble();
System.out.println("请输入三角形的颜色:");
color=input.next();
System.out.println("请输入一个bool值表面该三角形是否填充:");
filled=input.hasNext();
Triangle a = new Triangle(side1,side2,side3);
a.setColor(color);
a.setFilled(filled);
System.out.println("面积为:"+a.getArea()+"边长为:"+a.getPerimeter()+"颜色为:"+a.getColor()+"是否填充:"+a.isFilled());
}
}
public class GeometricObject {
private String color="white";
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject(){
dateCreated=new java.util.Date();
}
public GeometricObject(String color,boolean filled){
dateCreated=new java.util.Date();
this.color=color;
this.filled=filled;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color=color;
}
public boolean isFilled(){
return filled;
}
public void setFilled(boolean filled){
this.filled=filled;
}
public java.util.Date getDateCreated(){
return dateCreated;
}
public String toString(){
return "created on"+dateCreated+"\ncolor:"+color+"and filled:"+filled;
}
}
121题
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int max=0;
for(int i=0;i<nums.length-1;i+=2){
max+=Math.min(nums[i],nums[i+1]) ;
}
return max;
}
122题
public int[] sortArrayByParity(int[] A) {
int i=0;
int j=A.length-1;
while(i<j){
if(A[i]%2==0){
i++;
}else if(A[j]%2==1){
j--;
}else{
int temp=0;
temp=A[i];
A[i]=A[j];
A[j]=temp;
i++;
j--;
}
}
return A;
}
123题
124题
public boolean isToeplitzMatrix(int[][] matrix) {
int r = matrix.length, c = matrix[0].length;
for (int i = 0; i < r - 1; i++) {
for (int j = 0; j < c - 1; j++) {
if (matrix[i][j] != matrix[i + 1][j + 1]) return false;
}
}
return true;
}
125题
public static void main(String[] args) {
int [] num = {0 , 1 , 0 , 3 , 12} ;
int minIndex = 0 ;
int maxIndex = num.length - 1 ;
int centerIndex = (minIndex + maxIndex) / 2 ;
int temp = 0 ;
for(int i = 0 ; i < num.length -1 ; i++)
{
for(int j = 0 ; j < num.length - i - 1 ; j++)
{
if(num[j] < num[j + 1])
{
temp =num[j] ;
num[j] = num[j + 1] ;
num[j + 1] = temp ;
}
if(j == 2 && i == 1)
{
for(int a = 0 ; a < num.length ; a++)
{
System.out.print(num[a]);
System.out.print(" ");
}
}
}
}
}
}
126题
public int findMaxConsecutiveOnes(int[] nums) {
int max = 0;
int tmp = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
tmp++;
} else {
max = Math.max(max, tmp);
tmp = 0;
}
}
return Math.max(max, tmp);
}
127题
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement) && map.get(complement) != i) {
return new int[] { i, map.get(complement) };
}
}
throw new IllegalArgumentException("No two sum solution");
128题
public static void main(String[] args) {
int[] ary = {0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1};
int[] ary1 = {0, -6, 1, 2, 6, -7, 9, 1, 2, 0, 1};
int sum = 0;
for (int i = 0; i < ary.length; i++) {
sum += ary[i];
}
int avg = sum / 3;
if (avg * 3 != sum) {
System.out.println("false");
}
int temp = 0;
int step = 0;
for (int j = 0; j < ary.length; j++) {
temp += ary[j];
System.out.print(ary[j] + " ");
if (temp == avg) {
temp = 0;
System.out.println(" ");
step++;
}
}
if (step == 3) {
System.out.println("true");
}
}
}
129题
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> order = new ArrayList<Integer>();
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return order;
}
int rows = matrix.length, columns = matrix[0].length;
boolean[][] visited = new boolean[rows][columns];
int total = rows * columns;
int row = 0, column = 0;
int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int directionIndex = 0;
for (int i = 0; i < total; i++) {
order.add(matrix[row][column]);
visited[row][column] = true;
int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {
directionIndex = (directionIndex + 1) % 4;
}
row += directions[directionIndex][0];
column += directions[directionIndex][1];
}
return order;
}
130题
ArrayList<Integer> requests;
public RecentCounter() {
requests = new ArrayList<>();
}
public int ping(int t) {
requests.add(t);
int prev = bisectLeft(requests, t - 3000);
return requests.size() - prev;
}
public int bisectLeft(ArrayList<Integer> requests, int target) {
int left = 0;
int right = requests.size();
[left, right)
while (left < right) {
int mid = left + (right - left) / 2;
if (requests.get(mid) >= target) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
131题
public static int[] maxSlidingWindow(int[] nums, int k) {
if (nums.length==0||nums.length<k||nums==null){
return new int[0];
}
int end = nums.length-k;
int[] result = new int[end+1];
for (int i = 0; i <= end; i++) {
int max = nums[i];
for (int j = 1; j < k; j++) {
if (nums[i+j]>max){
max=nums[i+j];
}
}
result[i] = max;
}
return result;
}
public static void main(String[] args) {
int[] array = {1,3,-1,-3,5,3,6,7};
System.out.println(Arrays.toString(maxSlidingWindow(array,3)));
}
}
132题
public int getKthMagicNumber(int k) {
int p1 = 0, p2 = 0, p3 = 0;
int[] dp = new int[k];
dp[0] = 1;
for (int i = 1; i < k; i++) {
dp[i] = Math.min(3 * dp[p1], Math.min(5 * dp[p2], 7 * dp[p3]));
if (dp[i] == 3 * dp[p1]) p1++;
if (dp[i] == 5 * dp[p2]) p2++;
if (dp[i] == 7 * dp[p3]) p3++;
}
return dp[k - 1];
}
133题
private Queue<Integer> A = new LinkedList<>();
private Queue<Integer> B = new LinkedList<>();
public void push(int x) {
A.offer(x);
}
public Integer pop() {
if (empty()){
return null;
}
while (A.size() > 1){
Integer fong = A.poll();
if (fong == null){
break;
}
B.offer(fong);
}
int ret = A.poll();
swapAB();
return ret;
}
private void swapAB(){
Queue<Integer> tmp = A;
A = B;
B = tmp;
}
public Integer top() {
if (empty()){
return null;
}
while (A.size() > 1){
Integer fong = A.poll();
if (fong == null){
break;
}
B.offer(fong);
}
int ret = A.poll();
B.offer(ret);
swapAB();
return ret;
}
public boolean empty() {
return A.isEmpty() && B.isEmpty();
}
134题
private Stack<Integer> Stack_1;
private Stack<Integer> Stack_2;
public MyQueue() {
Stack_1 = new Stack<>();
Stack_2 = new Stack<>();
}
public void push(int x) {
Stack_1.push(x);
}
public int pop() {
if(Stack_2.isEmpty()){
while(!Stack_1.isEmpty()){
Stack_2.push(Stack_1.pop());
}
}
if (!Stack_2.isEmpty()) {
return Stack_2.pop();
}
throw new RuntimeException("MyQueue空了!");
}
public int peek() {
if(Stack_2.isEmpty()){
while(!Stack_1.isEmpty()){
Stack_2.push(Stack_1.pop());
}
}
if (!Stack_2.isEmpty()) {
return Stack_2.peek();
}
throw new RuntimeException("MyQueue空了!");
}
public boolean empty() {
return Stack_1.isEmpty() && Stack_2.isEmpty();
}
135题
public boolean isValid(String s) {
if(s == null) {
return true;
}
Stack<Character> stack = new Stack<>();
for(char c : s.toCharArray()) {
if(c=='(')stack.push(')');
else if(c=='[')stack.push(']');
else if(c=='{')stack.push('}');
else {
if(stack.isEmpty() || stack.pop() != c) {
return false;
}
}
}
return stack.isEmpty();
}
136题
public static int evalRPN(String[] tokens) {
int num1;
int num2;
Stack<Integer> stack = new Stack<>();
for(int i=0;i<tokens.length;i++){
String s=tokens[i];
if(s.equals("+")){
num2= stack.pop();
num1= stack.pop();
stack.push(num1+num2);
}else if(s.equals("-")){
num2= stack.pop();
num1= stack.pop();
stack.push(num1-num2);
}else if(s.equals("*")){
num2= stack.pop();
num1= stack.pop();
stack.push(num1*num2);
}else if(s.equals("/")){
num2= stack.pop();
num1= stack.pop();
stack.push(num1/num2);
}else {
stack.push(Integer.parseInt(s));
}
}
return stack.pop();
}
137题
public List<Integer> topKFrequent(int[] nums, int k) {
HashMap<Integer, Integer> map = new HashMap<>();
for(int i : nums) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
List<Integer>[] bucket = new List[nums.length + 1];
for(Map.E***y<Integer, Integer> e : map.e***ySet()) {
Integer value = e.getValue();
if(bucket[value] == null) {
bucket[value] = new ArrayList<Integer>();
}
bucket[value].add(e.getKey());
}
List<Integer> freList = new ArrayList<>();
for(int j = bucket.length - 1; j > -1 && freList.size() < k; j--) {
if(bucket[j] != null)
freList.addAll(bucket[j]);
}
return freList;
}
138题
public int countPrimes(int n) {
if(n<=1)
return 0;
boolean[] notPrime = new boolean[n];
notPrime[0] = true;
notPrime[1] = true;
for (int i = 2; i * i < n; i++) {
if (!notPrime[i]) {
for (int j = 2 * i; j < n; j += i) {
notPrime[j] = true;
}
}
}
int result = 0;
for (boolean b : notPrime) {
if (!b) {
result++;
}
}
return result;
}
139题
public List<Integer> powerfulIntegers(int x, int y, int bound) {
Set<Integer> set = new HashSet<>();
for (int a = 1; a < bound; a *= x) {
for (int b = 1; a + b <= bound; b *= y) {
set.add(a + b);
if (y == 1) break;
}
if (x == 1) break;
}
return new ArrayList<>(set);
}
140题
public int[] intersection(int[] nums1, int[] nums2) {
if(nums1==null||nums1.length==0||nums2==null||nums2.length==0) return new int[0];
Set<Integer> set1=new HashSet<>();
Set<Integer> resSet=new HashSet<>();
for(int i:nums1){
set1.add(i);
}
for(int i:nums2){
if(set1.contains(i)){
resSet.add(i);
}
}
int[] resArr=new int[resSet.size()];
int index=0;
for(int i:resSet){
resArr[index++]=i;
}
return resArr;
}
141题
public static void main(String[] args) {
System.out.println(searchIndex("yayah"));
System.out.println(searchIndex("hyaya"));
}
public static int searchIndex(String s){
for(int i=0;i<s.length();i++){
char ch = s.charAt(i);
if(s.indexOf(ch)==s.lastIndexOf(ch)){
return i;
}
}
return -1;
}
142题
public boolean isAlienSorted(String[] words, String order) {
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < order.length(); i++) {
map.put(order.charAt(i), i);
}
for (int i = 0; i < words.length - 1; i++) {
String w1 = words[i];
int w1_len = w1.length();
String w2 = words[i + 1];
int w2_len = w2.length();
for (int j = 0; j < Math.max(w1_len, w2_len); j++) {
int idx_w1 = j >= w1_len ? -1 : map.get(w1.charAt(j));
int idx_w2 = j >= w2_len ? -1 : map.get(w2.charAt(j));
if (idx_w1 > idx_w2) return false;
if (idx_w1 < idx_w2) break;
}
}
return true;
}