前言

本文章只是单纯记录课堂老师布置的课堂作业代码,题目都比较简单,所以没有写解题思路,相信大家都能理解,当然其中有的解法和代码不是最优的,当时只是为了完成题目,后来也懒得改了,如果有不恰当或者不正确的地方,欢迎指出


备注:有的忘记记录题目信息了,有的题目直接在作业系统里面提交了,请见谅,将就着看吧

1、

查看代码
package java_works;

import java.util.Scanner;

public class java_10001 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String b=sc.nextLine();
int zm=0,kg=0,qt=0;
for(int i=0;i<b.length();i++)
{
if(b.charAt(i)>=65&&b.charAt(i)<=90)
zm++;
else if(b.charAt(i)>=97&&b.charAt(i)<=122)
zm++;
else if(b.charAt(i)==32)
kg++;
else
qt++;
}
System.out.println("字母:"+zm);
System.out.println("空格:"+kg);
System.out.println("其他:"+qt);
} }

2、

查看代码
package java_works;

import java.text.DecimalFormat;

import java.util.Scanner;

public class java_10002 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int k=sc.nextInt();
double sum=0;
for(double i=0;i<=k;i++)
{
sum=sum+((i+6)/(i+10));
}
System.out.print("sum:");
System.out.printf("%.5f",sum);
} }

3、输入正整数N,输出[1, N]的之间的质数

查看代码
package java_works;

import java.util.Scanner;

public class java_10003 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int N=-1;
N=sc.nextInt();
for(int i=2;i<=N;i++)
{
int j;
for(j=2;j<=Math.sqrt(i);j++)
{
if(i%j==0)break;
}
if(j>Math.sqrt(i))
System.out.print(i+" ");
}
} }

4、用户输入正整数K为3-6之间的数,然后输出k位数的水仙花数

查看代码
package java_works;

import java.util.Scanner;

public class java_10004 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
while(true) {
Scanner sc=new Scanner(System.in);
int K=-1;
K=sc.nextInt();
if(K == 0)
{
break;
}
int a[] = new int[K];
int num = (int) Math.pow(10, K - 1) + 1;
while (num <= Math.pow(10, K)) {
int sum = 0;
for (int j = 0; j < K; j++)
a[j] = (int) (num / Math.pow(10, j) % 10);
for (int j = 0; j < K; j++)
sum = sum + (int) Math.pow(a[j],K);
if (num == sum)
System.out.print(num + " ");
num++;
}
} } }

5、输入正整数K,用二维数组生成并存储K阶方阵,第一行为: 1, 2, 3, ..., K, 第二行为: K+1, K+2, ... ,K+K, ... 依次类推;然后输出方阵的转置。

查看代码
package java_works;

import java.util.Scanner;

public class java_10005 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int K = sc.nextInt();
int a[][] = new int[K][K];
int b[][] = new int[K][K];
int z=1;
for(int i=0;i<K;i++)
{
for(int j=0;j<K;j++)
{
a[i][j]=z;
z++;
}
}
for(int i=0;i<K;i++)
{
for(int j=0;j<K;j++)
{
b[j][i]=a[i][j];
}
}
for(int i=0;i<K;i++)
{
for(int j=0;j<K;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
}

6、输入正整数N,然后输入N个浮点数,然后计算并输出它们平均值和方差。

查看代码
package java_works;

import java.util.Scanner;

public class java_10006 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = -1;
N = sc.nextInt();
double[] a = new double[N];
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextDouble();
}
Double sum=0.0;
for(int i=0;i<a.length;i++)
{
sum=sum+a[i]; //求和
}
double mean = 0.0,diff=0.0;
mean = sum / N;
double c=0.0;
for(int i=0;i<a.length;i++)
{
c=c+Math.pow((a[i]-mean),2);
}
diff=c/N;
System.out.printf("mean:"+"%.5f",mean);
System.out.println();
System.out.printf("diff:"+"%.5f",diff);
} }

7、输入正整数N,然后输入N个浮点数,然后计算并输出它们最小值和最大值。

查看代码
package java_works;

import java.util.Scanner;

public class java_10007 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = -1;
N = sc.nextInt();
Double[] a = new Double[N];
for(int i=0;i<N;i++)
{
a[i]=sc.nextDouble();
}
double minv = a[0],maxv=a[0];
for(int i=0;i<a.length;i++)
{
if(a[i]<minv)
minv=a[i];
if(a[i]>maxv)
maxv=a[i];
}
System.out.printf("minv:"+"%.5f",minv);
System.out.println();
System.out.printf("maxv:"+"%.5f",maxv); } }

8、输入正整数N,然后输入N个浮点数,再按从小到大排序并输出排序结果。

查看代码
package java_works;

import java.util.Scanner;

public class java_10008 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = -1;
N = sc.nextInt();
double[] a = new double[N];
for(int i=0;i<a.length;i++)
{
a[i] = sc.nextDouble();
}
for(int i=0;i<a.length;i++)
for(int j=0;j<a.length-1;j++)
{
double temp;
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
for(int i=0;i<a.length;i++)
{
System.out.printf("%.5f\n",a[i]);
}
}
}

9、输入一行字符串,然后将每个单词的首字母变为大写

查看代码
package java_works;

import java.util.Scanner;

public class java_10009 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str ;
str = sc.nextLine();
daxie(str);
}
public static void daxie(String s){
String a[] = s.split(" ");
for(int i=0;i<a.length;i++)
{
String s1=a[i].substring(0,1).toUpperCase()+a[i].substring(1);
System.out.print(s1+" ");
}
}
}

10、从键盘输入一行字符串,请在单词间做逆序调整。举例:“cat loves dog”逆序成“dog loves cat”。

查看代码
package java_works;

import java.util.Scanner;

public class Java_10010 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str;
str = sc.nextLine();
nixu(str);
}
public static void nixu(String s) {
String[] a = s.split(" ");
String temp = "";
for (int i = 0; i < a.length/2; i++) {
temp = a[i];
a[i] = a[a.length-1-i];
a[a.length-1-i] = temp;
}
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
} } }

11、间隔符规范化。输入一行字符串,该字符串使用空格作为单词、符号等之间的间隔符。然而由于用户操作的问题,在录入间隔符时,可能输入了多个空格,现要求对输入的字符串进行处理,清除字符串的首尾空格,并且确保字符串中的间隔符只能有一个空格

查看代码
package java_works;

import java.util.Scanner;

public class java_10011 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
guiFanhua(str);
}
public static void guiFanhua(String s) {
s=s.trim(); //去除字符串首尾空格字符
StringBuffer sb = new StringBuffer();
int flag;
for(int i=0;i<s.length();i++)
{
flag = 0;
if(s.charAt(i)!=' ')
sb.append(s.charAt(i));
else {
flag = 1;
}try {
if(s.charAt(i)==' '&&s.charAt(i+1)!=' ') {
sb.append(' ');
}
}catch(Exception e) {
continue;
}
}
System.out.println(sb);
}
}

12、输入M和N,然后计算一个球从M米高度自由下落,每次落地后反跳回原高度的一半,再落下,再反弹。计算它在第N次落地时共经过多少米?第N次落地时反弹多高

查看代码
package java_works;

import java.util.Scanner;

public class java_10012 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int M = sc.nextInt();
int N = sc.nextInt();
Solve(M,N);
}
public static void Solve(int a,int b) {
double L = 0.0,H = 0.0;
for(int i=1;i<b;i++)
{
L=L+a/(Math.pow(2, i-1));
}
H=a/(Math.pow(2,b));
System.out.printf("%.5f\n",L+a);
System.out.printf("%.5f",H);
} }

13、输入正整数N,用1,2,3,..,N数字给N个学生编号,N个学生围成一圈。然后从第1个人开始报数(从 1 到 5 报数),凡报到 5的人退出圈子,问最后留下的是原来第几号的那位?

查看代码
package java_works;

import java.util.Scanner;

public class java_10013 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = -1;
N = sc.nextInt();
Solves(N);
}
public static void Solves(int a) {
int[] b = new int[a];
for(int i=0;i<a;i++)
{
b[i]=i+1;
}
int currentNum = 1;
int count = b.length;
for(int i=0;;i++)
{
if(count==1) {
break;
}
if(i>=b.length) {
i=0;
}
if(b[i]==0) {
continue;
}
if(currentNum % 5 == 0) {
count--;
b[i]=0;
}
if(currentNum == 5) {
currentNum = 1;
}
else {
currentNum++;
}
}
for(int i=0;i<b.length;i++)
{
if(b[i]!=0) {
System.out.println(b[i]);
}
}
} }

14、输入某年某月某日,输出这一天是这一年的第几天。要特别注意闰年的判断,并且处理好闰年的情况输入一行字符串,统计并输出字符串中每个字符出现的次数

查看代码
package java_works;

import java.util.Scanner;

import java.util.GregorianCalendar;

public class java_10014 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int year = 0,month = 0, day = 0;
year = sc.nextInt();
month = sc.nextInt();
day = sc.nextInt();
Solves(year,month,day);
}
public static void Solves(int n,int y,int r) {
GregorianCalendar gre = new GregorianCalendar();//判断是否为闰年的方法
boolean isLeapYear = gre.isLeapYear(n);//返回值是true,说明是润年,若返回值是false,则不是闰年
int ap = isLeapYear?29:28;//判断2月份的天数
int days=0;
switch(y) {
case 1:
days=r;
break;
case 2:
days=31+r;
break;
case 3:
days=31+ap+r;
break;
case 4:
days=31+ap+31+r;
break;
case 5:
days=31+ap+31+30+r;
break;
case 6:
days=31+ap+31+30+31+r;
break;
case 7:
days=31+ap+31+30+31+30+r;
break;
case 8:
days=31+ap+31+30+31+30+31+r;
break;
case 9:
days=31+ap+31+30+31+30+31+31+r;
break;
case 10:
days=31+ap+31+30+31+30+31+31+30+r;
break;
case 11:
days=31+ap+31+30+31+30+31+31+30+31+r;
default:
days=31+ap+31+30+31+30+31+31+30+31+30+r;
}
System.out.println(days);
} }

15、输入一行字符串,统计并输出字符串中每个字符出现的次数

查看代码
package java_works;

import java.util.Iterator;

import java.util.Scanner;

import java.util.TreeMap;

import java.util.Set;

public class java_10015 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char[] ch = str.toCharArray();
method(ch);
int blank = 0;
for(int i=0;i<ch.length;i++)
{
if(ch[i]==' ')
blank++;
}
System.out.println("空格:"+blank);
}
public static void method(char[] array) {
//初始化一个长度为26的整型数组,初始值全为0,记录字符串中各个字符出现的次数
int[] temp = new int[26];
for(int i=0;i<temp.length;i++)
{
temp[i]=0;
}
//遍历字符数组,将其值对应的ASCII值作为整型数组下标
for(int i=0;i<array.length;i++)
{
if(array[i]>='a'&&array[i]<='z')
{
temp[array[i]-'a']++;
}
}
//打印输出每个小写英文字母及出现的次数
for(int i=0;i<temp.length;i++)
{
System.out.println((char)(i+'a')+":"+temp[i]);
}
}
}

16、输入一行字符串,输出最先出现重复的字符

查看代码
package java_works;

import java.util.Scanner;

public class java_10016 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
getFirstIndex(str);
}
public static void getFirstIndex(String s) {
String[] a = s.split("");
for(int i=0;i<a.length;i++) {
int num = 0;
int index = 0;
while(index<=i) {
if(a[index].equals(a[i])) {
num++;
}
index++;
}
if(num>1) {
System.out.println(a[i]);
break;
}
} } }

17、输入一个正整数N,对它进行质因数分解并输出结果。例如:输入 90,打印出 90=2*3*3*5

查看代码
package java_works;

import java.util.Scanner;

public class java_10017 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = -1;
N = sc.nextInt();
Solve(N);
}
public static void Solve(int a) {
System.out.print(a+"=");
for(int i=2;i<a+1;i++) {
while(a%i==0&&a!=i) {
a/=i;
System.out.print(i+"*");
}
if(a==i) {
System.out.println(i);
break;
}
}
}
}

18、输入一个正整数N,输出[1, N]之间的“完数”。“完数”就是如果这个数恰好等于它的因子之和,则这个数就称为"完数",例如6=1+2+3

查看代码
package java_works;

import java.util.Scanner;

public class java_10018 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = -1;
N = sc.nextInt();
Solve(N);
}
public static void Solve(int a) { for(int i=1;i<a;i++)
{
int sum = 0;
for(int j=1;j<=i/2;j++)
{
if(i % j == 0)
{
sum+=j;
}
}
if(sum == i)
{
System.out.print(i+" ");
} } }
}

19、输入一行字符串,按照规格压缩,输出压缩后的字符串。压缩规格为:相同字符连续,则压缩为“字符+数字个数”,如"iamaaaaaAbcCCdD压缩为"iama5AbcC2dD"。

查看代码
package java_works;

import java.util.Scanner;

public class java_10019 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
compressStr(str);
}
public static void compressStr(String s) {
StringBuilder sb = new StringBuilder();
int count = 1;
char c1=s.charAt(0);
for(int i=1;i<s.length();i++)
{
char c2 = s.charAt(i);
if(c1 == c2) {
count++;
continue;
}
if(count>1) {
sb.append(c1).append(count);
}else {
sb.append(c1);
}
c1 = c2;
count = 1;
}
if(count >1) {
sb.append(c1).append(count);
}else {
sb.append(c1);
}
System.out.println(sb.toString());
}
}

20、指令分割。输入正整数N,然后输入N行字符串,每一行字符串为一条指令。

查看代码
package java_works;

import java.util.Scanner;

public class java_10020 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = -1;
N = Integer.parseInt(sc.nextLine());
String[] a = new String[N];
for(int i=0;i<N;i++)
{
a[i] = sc.nextLine().replace(" ","");
}
sc.close();
String[] singleArr = {"printi","jg","jl","je","jne","jmp"};
String[] doubleArr = {"mov","add","sub","mul","div","cmp"};
for(int i=0;i<N;i++) {
int flag = 0;
for(String tmp:singleArr)
{
if(a[i].indexOf(":"+tmp)>0)
{
flag = 1;
String[] tmpStrArr = a[i].split(":"+tmp);
System.out.println("LineNo="+tmpStrArr[0]);
System.out.println("OP="+tmp);
System.out.println("N1="+tmpStrArr[1]);
}
}
if(flag == 0){
for(String tmp:doubleArr)
{
if(a[i].indexOf(":"+tmp)>0) {
flag = 2;
String[] tmpStrArr = a[i].split(":"+tmp);
System.out.println("LineNo="+tmpStrArr[0]);
System.out.println("OP="+tmp);
System.out.println("N1="+tmpStrArr[1].split(",")[0]);
System.out.println("N2="+tmpStrArr[1].split(",")[1]);
}
}
}
if(flag == 0)
return;
}
} }

21、二进制指令序列解码成字符串源代码。输入一行字符串,输入的字符串为二进制指令序列的16进制形式字符串


22、抱歉,忘记写了,题目也给忘了,索性摆烂。。。


23、输入正整数N,然后输入N*N个正整数按一行一行的顺序排列成N行N列的矩阵,要求对矩阵按垂直方向翻转,并输出结果

查看代码
import java.util.Scanner;

public class java_10023 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[][] a = new int[N][N];
for(int i = 0;i < N; i++)
for(int j = 0;j < N;j++)
{
a[i][j] = sc.nextInt();
}
//实现垂直翻转矩阵
for(int i = N - 1;i > -1;i--)
{
for(int j = 0;j < N;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
} } }

24、老式手机健盘上有1, 2, ..., 8, 9, 0键,其中每个键都代表多个字母,且0代表空格。输入一行只含字母和空格的字符串,将字符串转为成手机键盘按键序号,并输出结果

查看代码
import java.util.Scanner;

public class java_10024 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String sentence = sc.nextLine();
char[] a = sentence.toCharArray();
for(int i = 0;i < a.length;i++)
{
if(a[i] >= 97 && a[i] <= 99)
a[i] = '2';
else if(a[i] >= 100 && a[i] <= 102)
a[i] = '3';
else if(a[i] >= 103 && a[i] <= 105)
a[i] = '4';
else if(a[i] >= 106 && a[i] <= 108)
a[i] = '5';
else if(a[i] >= 109 && a[i] <= 111)
a[i] = '6';
else if(a[i] >= 112 && a[i] <= 115)
a[i] = '7';
else if(a[i] >= 116 && a[i] <= 118)
a[i] = '8';
else if(a[i] >= 119 && a[i] <= 122)
a[i] = '9';
else if(a[i] == 32)
a[i] = '0';
System.out.print(a[i]);
}
} }

25、输入一个正整数N,然后输入一个[2, 16]之间的数K,请把N转化为K进制的数,并输出结果

查看代码
import java.util.Scanner;

public class java_10025 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int K = sc.nextInt();
if(K < 2 || K > 16)
return;
//实现任意进制之间的相互转换
String s = Integer.toString(N,K);
System.out.println(s.toUpperCase());
} }

26、用散列表(哈希表)建立城市名主健与电话号码区号的映射,

查看代码
import java.util.HashMap;

import java.util.Scanner;

public class java_10026 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<String,String> code = new HashMap<String,String>();
Scanner sc = new Scanner(System.in);
int N = Integer.parseInt(sc.nextLine());
//将城市名主键和电话号码区号建立映射存储在哈希表中
for(int i = 0;i < N;i++)
{
String tempStr = sc.nextLine();
String tempStrArr[] = tempStr.split(" ");
code.put(tempStrArr[0], tempStrArr[1]);
}
int IN = Integer.parseInt(sc.nextLine());
String[] cityKey = new String[IN];
for(int i = 0;i < IN;i++)
{
String tempStr = sc.nextLine();
cityKey[i] = tempStr;
}
sc.close();
for(int i = 0;i < IN;i++)
{
System.out.println(cityKey[i] + " " + code.get(cityKey[i]));
}
} }

27、请你定义一个类Student,表示学生,并实现相应的功能

查看代码
import java.util.*;

import java.io.*;

public class java_10027 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
class Student{
String Name;//姓名
Double NGrade;//平时成绩
Double EGrade;//考试成绩
public Student(String Name,Double NGrade,Double EGrade){
super();
this.Name = Name;
this.NGrade = NGrade;
this.EGrade = EGrade;
} public Student() {
// TODO Auto-generated constructor stub
} public String getName() {
return this.Name;
}
public void setName(String Name) {
this.Name = Name;
}
public Double getNGrade() {
return NGrade;
}
public void setNGrade(Double NGrade) {
this.NGrade = NGrade;
}
public double sumGrade(Student s) {
return this.NGrade * 0.3 + this.EGrade * 0.7;
}
public boolean CompareTS(Student s) {
Double in = this.sumGrade();
Double out = s.sumGrade();
if (in > out)
return true;
else if (in == out)
return true;
else
return false;
}
private Double sumGrade() {
// TODO Auto-generated method stub
return null;
} public void ToString(Student s) {
System.out.printf("姓名:"+s.Name+"\n平时成绩:"+s.NGrade+"\n考试成绩:"+s.EGrade+"\n总成绩:"+sumGrade(s)+"\n");
}
} } }

28、哎呀,题目又没写。。。

查看代码
import java.io.*;

import java.util.*;

class Complex{
double real;
double image;
public Complex(double real,double image) {
this.real = real;
this.image = image;
}
public Complex(Complex c) {
this.real = c.real;
this.image = c.image;
}
public double getReal() {
return real;
}
public double getImage() {
return image;
}
public void setReal(double real) {
this.real = real;
}
public void setImage(double image) {
this.image = image;
}
public String toString() {
return "(" + String.format("%.5f", real) + ")" + "+" + "(" + String.format("%.5f", image) + ")" + "i";
}
public Complex add(Complex c) {
return new Complex( this.real + c.real, this.image + c.image );
}
public Complex sub(Complex c) {
return new Complex( this.real - c.real, this.image - c.image );
}
public Complex multiply( Complex C ) {
double newa = this.real * C.real - this.image * C.image; // 新建一个 实部
double newb = this.real * C.image + this.image * C.real; // 新建一个 虚部
return new Complex( newa, newb );
}
public Complex exp( ) {
return new Complex( Math.exp(this.real) * Math.cos(this.image),
Math.exp(this.real) * Math.sin(this.image) );
}
public double abs( ) {
return Math.sqrt( this.real * this.real + this.image * this.image );
}
}

29、Rectangle实体类

查看代码
import java.io.*;

import java.util.*;

class Rect {
Double x;//X坐标
Double y;//Y坐标
Double L;//矩形长
Double W;//矩形宽度
public Rect(Double x,Double y,Double l,Double w) {
this.x = x;
this.y = y;
this.L = l;
this.W = w;
}
public Rect(Rect r) {
// TODO Auto-generated constructor stub
this.x = r.x;
this.y = r.y;
this.L = r.L;
this.W = r.W;
}
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 Double getL() {
return this.L;
}
public void setL(Double l) {
this.L = l;
}
public Double getW() {
return this.W;
}
public void setW(Double w) {
this.W = w;
}
public Double Area() {
return L * W;
}
public int CompareArea(Rect r) {
Double a = this.Area();
Double b = r.Area();
if(a > b)
return 1;
else if(a == b)
return 0;
else
return -1;
}
public String toString() {
return (String.format("x:%.5f",x) +"\n" + String.format("y:%.5f", y) + "\n" + String.format("L:%.5f", L)+ "\n" + String.format("W:%.5f", W) +"\n" + String.format("面积:%.5f", this.Area()));
}
}

30、请你定义一个类Circle,表示圆。要求实现相应的功能

查看代码
public class java_10030 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
/* 构造函数参数按顺序为:x, y, r */
Circle rc1 = new Circle(10.01, 20.0, 30.0);
Circle rc2 = new Circle(rc1); System.out.println(String.valueOf(rc1.getX()));
System.out.println(String.valueOf(rc1.getY()));
System.out.println(String.valueOf(rc1.getR())); rc2.setX(40.66);
rc2.setY(58.18);
rc2.setR(28.98); /* 比较面积,如果rc2面积更大则输出1, 相等输出0,否则输出-1; */
System.out.println(String.valueOf(rc2.CompareCS(rc1))); /* 比较周长,如果rc2周长更长则输出1, 相等输出0,否则输出-1; */
System.out.println(String.valueOf(rc2.CompareCL(rc1)));
System.out.println(String.valueOf(rc2.toString()));
} }

31、。。。

import java.io.*;

import java.util.*;

class CUser {
String userName;
String passWord;
public CUser(String u,String p) {
this.userName = u;
this.passWord = p;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String u) {
this.userName = u;
}
public String getPassWord() {
return this.passWord;
}
public void setPassWord(String p) {
this.passWord = p;
}
public String Login(String InUserName,String InPassWord) {
if(userName.equals(InUserName) && passWord.equals(InPassWord))
return "登录成功";
else
return "登陆失败";
}
}

32、。。。

查看代码
import java.io.*;

import java.util.*;

interface IReadBook {
void ReadBook();
}
class CBauStu implements IReadBook{
public void ReadBook() {
System.out.println("本科生读教材");
}
}
class CGduStu implements IReadBook{
public void ReadBook() {
System.out.println("硕士生读中文学术期刊");
}
}
class CDocStu implements IReadBook{
public void ReadBook() {
System.out.println("博士生读外文学术期刊");
}
}

33、直接看代码吧

查看代码
import java.io.*;

import java.util.*;

interface ICry{
void Cry();
}
class CFrog implements ICry{
public void Cry() {
System.out.println("青蛙哇哇叫");
}
}
class CDog implements ICry{
public void Cry() {
System.out.println("小狗汪汪叫");
}
}
class CCat implements ICry{
public void Cry() {
System.out.println("小猫喵喵叫");
}
}

34、

查看代码
public class java_10034 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Integer[] li = new Integer[] {100, 20, 32, 196, 85};
Integer iv = CommonFun.getMax(li); //返回iv值为196
System.out.println(iv); Double[] ld = new Double[] {51.0, 10.6, 165.2, 12.0, 82.0};
Double dv = CommonFun.getMax(ld); //返回dv值为165.2
System.out.println(dv);
} }

35、

import java.util.Scanner;

public class java_10035 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String[][] Student = new String[N][6];
for(int i = 0;i < N;i++)
{
int sum =0;
for(int j = 0;j < 4;j++)
{
Student[i][j] = sc.next();
}
sum = Integer.parseInt(Student[i][1]) + Integer.parseInt(Student[i][2]) + Integer.parseInt(Student[i][3]);
Student[i][4] = "" + sum;
Student[i][5] = "" + i;
}
//排序
for(int i = 0;i < N -1;i++)
{
for(int j = N -1;j > i;j--)
{
if(Integer.parseInt(Student[i][4]) < Integer.parseInt(Student[j][4]))
{
String[] tempArr = Student[i];
Student[i] = Student[j];
Student[j] = tempArr;
}else if(Integer.parseInt(Student[i][4]) == Integer.parseInt(Student[j][4]))
{
if(Integer.parseInt(Student[i][5]) > Integer.parseInt(Student[j][5]))
{
String[] tempArr = Student[i];
Student[i] = Student[j];
Student[j] = tempArr;
}
}
}
}
for(int i = 0;i < N;i++)
{
for(int j = 0;j < 5;j++)
{
System.out.print(Student[i][j] + " ");
}
System.out.println();
}
}
}

36、

import java.util.Scanner;

public class java_10036 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String[][] Student = new String[N][6];
for(int i = 0;i < N;i++)
{
int sum =0;
for(int j = 0;j < 4;j++)
{
Student[i][j] = sc.next();
}
sum = Integer.parseInt(Student[i][1]) + Integer.parseInt(Student[i][2]) + Integer.parseInt(Student[i][3]);
Student[i][4] = "" + sum;
Student[i][5] = "" + i;
}
//排序
for(int i = 0;i < N -1;i++)
{
for(int j = N -1;j > i;j--)
{
if(Integer.parseInt(Student[i][4]) < Integer.parseInt(Student[j][4]))
{
String[] tempArr = Student[i];
Student[i] = Student[j];
Student[j] = tempArr;
}else if(Integer.parseInt(Student[i][4]) == Integer.parseInt(Student[j][4]))
{
if(Integer.parseInt(Student[i][1]) < Integer.parseInt(Student[j][1]))
{
String[] tempArr = Student[i];
Student[i] = Student[j];
Student[j] = tempArr;
}else if(Integer.parseInt(Student[i][1]) == Integer.parseInt(Student[j][1])) {
if(Integer.parseInt(Student[i][5]) > Integer.parseInt(Student[j][5]))
{
String[] tempArr = Student[i];
Student[i] = Student[j];
Student[j] = tempArr;
}
}
}
}
}
for(int i = 0;i < N;i++)
{
for(int j = 0;j < 5;j++)
{
System.out.print(Student[i][j] + " ");
}
System.out.println();
}
} }

37、

import java.util.Scanner;

public class java_10037 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String[][] Student = new String[N][6];
for(int i = 0;i < N;i++)
{
int sum =0;
for(int j = 0;j < 4;j++)
{
Student[i][j] = sc.next();
}
sum = Integer.parseInt(Student[i][1]) + Integer.parseInt(Student[i][2]) + Integer.parseInt(Student[i][3]);
Student[i][4] = "" + sum;
Student[i][5] = "" + i;
}
//排序
for(int i = 0;i < N -1;i++)
{
for(int j = N -1;j > i;j--)
{
if(Integer.parseInt(Student[i][4]) < Integer.parseInt(Student[j][4]))
{
String[] tempArr = Student[i];
Student[i] = Student[j];
Student[j] = tempArr;
}else if(Integer.parseInt(Student[i][4]) == Integer.parseInt(Student[j][4]))
{
if(Integer.parseInt(Student[i][1]) < Integer.parseInt(Student[j][1]))
{
String[] tempArr = Student[i];
Student[i] = Student[j];
Student[j] = tempArr;
}else if(Integer.parseInt(Student[i][1]) == Integer.parseInt(Student[j][1])) {
if(Integer.parseInt(Student[i][2]) < Integer.parseInt(Student[j][2]))
{
String[] tempArr = Student[i];
Student[i] = Student[j];
Student[j] = tempArr;
}else if(Integer.parseInt(Student[i][2]) == Integer.parseInt(Student[j][2]))
if(Integer.parseInt(Student[i][5]) > Integer.parseInt(Student[j][5]))
{
String[] tempArr = Student[i];
Student[i] = Student[j];
Student[j] = tempArr;
}
}
}
}
}
for(int i = 0;i < N;i++)
{
for(int j = 0;j < 5;j++)
{
System.out.print(Student[i][j] + " ");
}
System.out.println();
}
} }

38、

import java.util.Scanner;

public class java_10038 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String[][] Student = new String[N][6];
for(int i = 0;i < N;i++)
{
int sum =0;
for(int j = 0;j < 4;j++)
{
Student[i][j] = sc.next();
}
sum = Integer.parseInt(Student[i][1]) + Integer.parseInt(Student[i][2]) + Integer.parseInt(Student[i][3]);
Student[i][4] = "" + sum;
Student[i][5] = "" + i;
}
//排序
for(int i = 0;i < N -1;i++)
{
for(int j = N -1;j > i;j--)
{
if(Integer.parseInt(Student[i][4]) < Integer.parseInt(Student[j][4]))
{
String[] tempArr = Student[i];
Student[i] = Student[j];
Student[j] = tempArr;
}else if(Integer.parseInt(Student[i][4]) == Integer.parseInt(Student[j][4]))
{
if(Integer.parseInt(Student[i][1]) < Integer.parseInt(Student[j][1]))
{
String[] tempArr = Student[i];
Student[i] = Student[j];
Student[j] = tempArr;
}else if(Integer.parseInt(Student[i][1]) == Integer.parseInt(Student[j][1])) {
if(Integer.parseInt(Student[i][2]) < Integer.parseInt(Student[j][2]))
{
String[] tempArr = Student[i];
Student[i] = Student[j];
Student[j] = tempArr;
}else if(Integer.parseInt(Student[i][2]) == Integer.parseInt(Student[j][2]))
if(Student[i][0].compareTo(Student[j][0]) > 0)
{
String[] tempArr = Student[i];
Student[i] = Student[j];
Student[j] = tempArr;
}
}
}
}
}
for(int i = 0;i < N;i++)
{
for(int j = 0;j < 5;j++)
{
System.out.print(Student[i][j] + " ");
}
System.out.println();
}
} }

39、

import java.io.*;

import java.util.Scanner;

public class java_10039 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int flag = -1 , i = 0;
char[] ch = str.toCharArray();
while(i < str.length()/2)
{
if(ch[i] != ch[str.length() -1 -i])
{
flag = 0;
break;
}
i++;
}
if(flag ==-1)
{
flag = 1;
}
System.out.println(flag);
} }

40、

import java.util.Scanner;

public class java_10040 {
public String longStr(String s)
{
if(s.length() < 2)
return s;
int max_len = 1;
int start = 0;
//dp[i][j]表示s[i][j]是否是回文串
boolean[][] dp = new boolean[s.length()][s.length()];
//初始化,所有长度为1的字串都是回文串
for(int i = 0;i < s.length();i++)
{
dp[i][i] = true;
}
char[] charArr = s.toCharArray();
//递推开始
//先枚举字串长度
for(int l = 2;l <= s.length();l++)
{
//枚举左边界
for(int i = 0;i < s.length();i++)
{
int j = l + i - 1;
//如果右边越界,则退出循环
if(j >= s.length())
break;
if(charArr[i] != charArr[j]) {
dp[i][j] = false;
}
else {
if(j - i < 3) {
dp[i][j] = true;
}
else {
dp[i][j] = dp[i + 1][j - 1];
}
}
//只要dp[j][k] == true成立,就表示子串s[i][j]是回文
if(dp[i][j] && j - i + 1 > max_len) {
max_len = j - i + 1;
start = i;
}
}
}
return s.substring(start,start + max_len);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
java_10040 solution = new java_10040();
System.out.println(solution.longStr(str));
}
}

41、输入正整数N和K,然后输入N个浮点数,要求输出它们中第K大的数

查看代码
import java.util.Scanner;

public class java_10041 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int K = sc.nextInt();
Double Temp;
Double[] Arr = new Double[N];
for(int i = 0; i < Arr.length;i++)
{
Arr[i] = sc.nextDouble();
}
//利用冒泡排序法对数组进行排序
for(int i = 0;i < Arr.length - 1 ;i++)
{
for(int j = 0;j <Arr.length - i - 1;j++)
{
if(Arr[j] < Arr[j + 1])
{
Temp = Arr[j];
Arr[j] = Arr[j + 1];
Arr[j + 1] = Temp;
}
}
}
System.out.printf("%.5f",Arr[K-1]);
} }

42、输入正整数N,然后输入N个浮点数组成的向量A,再输入N个浮点数组成的向量B,要求计算并输出A+B。

查看代码
import java.util.Scanner;

public class java_10042 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
Double[] A = new Double[N];
Double[] B = new Double[N];
for(int i = 0; i < N;i++)
{
A[i] = sc.nextDouble();
}
for(int i = 0; i < N;i++)
{
B[i] = A[i] + sc.nextDouble();
}
for(int i = 0;i < N;i++)
{
System.out.print(String.format("%.5f" + " ", B[i]));
}
} }

43、输入正整数N,然后输入N个浮点数组成的向量A,再输入N个浮点数组成的向量B,要求计算并输出A-B。

查看代码
import java.util.Scanner;

public class java_10043 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
Double[] A = new Double[N];
Double[] B = new Double[N];
for(int i = 0; i < N;i++)
{
A[i] = sc.nextDouble();
}
for(int i = 0; i < N;i++)
{
B[i] = A[i] - sc.nextDouble();
}
for(int i = 0;i < N;i++)
{
System.out.print(String.format("%.5f" + " ", B[i]));
}
} }

44、输入正整数N,然后输入N个浮点数组成的向量A,再输入N个浮点数组成的向量B,要求计算并输出向量A和B之间的欧氏距离

查看代码
import java.util.Scanner;

public class java_10044 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
Double[] A = new Double[N];
Double[] B = new Double[N];
Double sum = 0.0;
for(int i = 0;i < N;i++)
{
A[i] = sc.nextDouble();
}
for(int i = 0;i < N;i++)
{
B[i]= sc.nextDouble();
sum += Math.pow(A[i] - B[i] , 2);
}
sum = Math.sqrt(sum);
System.out.println(String.format("%.5f",sum));
} }

45、输入正整数N和K,然后输入一个由N个浮点数组成的向量A,再输入K个同样维度的向量,要求在K个向量中找出与向量A的欧氏距离最小的向量,并输出找到的向量与向量A的欧氏距离

查看代码
import java.util.Scanner;

public class java_10045 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int K = sc.nextInt();
Double[] A = new Double[N]; for(int i = 0;i < A.length;i++)
{
A[i] = sc.nextDouble();
}
Double[][] B = new Double[K][N];
for(int i = 0;i < B.length;i++)
{
for(int j = 0;j < B[i].length;j++)
{
B[i][j] = sc.nextDouble();
}
}
//求欧氏距离 Double[] V = new Double[K];
for(int i = 0;i < K;i++)
{
double value = 0;
for(int j = 0;j < N;j++)
{
value += Math.pow(A[j] - B[i][j],2);
}
V[i] = Math.sqrt(value);
}
//寻找与向量A的欧氏距离最小的向量
double minValue = V[0];
for(int i = 1 ;i < V.length;i++)
{
if(V[i] < minValue)
minValue = V[i];
}
System.out.printf("%.5f",minValue);
}
}

46、输入正整数N和K,然后输入K个向量,其中每个向量由N个浮点数组成。现要求在K个向量中找出欧氏距离最小的两个向量,并输出找到的这两个向量之间的欧氏距离

查看代码
import java.util.Scanner;

public class java_10046 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int K = sc.nextInt();
double sum = 0.0;
double minSum = Double.MAX_VALUE;
int minI = -1;
int minJ = -1;
Double[][] data= new Double[K][N];
for(int i = 0;i < K;i++)
{
for(int j = 0 ;j < N;j++)
{
data[i][j] = sc.nextDouble();
}
}
for(int i = 0 ;i < K;i++)
{
for(int j = i + 1;j < K;j++)
{
sum = 0.0;
for(int m = 0;m < N;m++)
{
sum = sum + Math.pow(data[i][m] - data[j][m],2);
}
sum = Math.sqrt(sum);
if(sum < minSum)
{
minSum = sum;
minI =i;
minJ = j;
}
}
}
System.out.format("%.5f",minSum);
}
}

47、。。。


48、。。。

import java.util.Scanner;

public class java_10048 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
Double[] M = new Double[N];
Double[] A = new Double[N];
Double[] B = new Double[N];
Double[] R = new Double[N];
for(int i = 0;i < N;i++)
{
M[i] = sc.nextDouble();
}
for(int i = 0;i < N;i++)
{
A[i] = sc.nextDouble();
}
for(int i = 0;i < N;i++)
{
B[i] = sc.nextDouble();
}
for(int i = 0;i < N;i++)
{
if(M[i] < 0.5)
{
R[i] = A[i];
}
else
{
R[i] = B[i];
}
}
for(int i = 0;i < N;i++)
{
System.out.format("%.5f" + " ", R[i]);
}
} }

49、。。。

import java.util.Scanner;

	public class java_10049 {

		public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N1 = sc.nextInt();
int C1 = sc.nextInt();
int N2 = sc.nextInt();
int C2 = sc.nextInt();
Double[][] A = new Double[N1][C1];
Double[][] B = new Double[N2][C2];
for(int i = 0;i < N1;i++)
{
for(int j = 0;j < C1;j++)
{
A[i][j] = sc.nextDouble();
}
}
for(int i = 0;i < N2;i++)
{
for(int j = 0;j < C2;j++)
{
B[i][j] = sc.nextDouble();
}
}
for(int i = 0;i < N1;i++)
{
for(int j = 0;j < C2;j++)
{
Double sum = 0.0;
for(int k = 0;k < C1;k++)
{
sum = sum + A[i][k]*B[k][j];
}
System.out.format("%.5f" + " ", sum);
}
System.out.println();
}
} }

50、51、没写啊哈哈哈哈哈哈


52、。。。

import java.util.Scanner;

public class java_10052 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int C = sc.nextInt();
Double[][] M = new Double[N][C];
Double[][] A = new Double[N][C];
Double[][] B = new Double[N][C];
Double[][] R = new Double[N][C];
for(int i = 0;i < N;i++)
{
for(int j = 0;j < C;j++)
{
M[i][j] = sc.nextDouble();
}
}
for(int i = 0;i < N;i++)
{
for(int j = 0;j < C;j++)
{
A[i][j] = sc.nextDouble();
}
}
for(int i = 0;i < N;i++)
{
for(int j = 0;j < C;j++)
{
B[i][j] = sc.nextDouble();
}
} for(int i = 0;i < N;i++)
{
for(int j = 0;j < C;j++)
{
if(M[i][j] < 0.5)
{
R[i][j] = A[i][j];
}
else
{
R[i][j] = B[i][j];
}
}
}
for(int i = 0;i < N;i++)
{
for(int j = 0;j < C;j++)
{
System.out.format("%.5f" + " ", R[i][j]);
}
System.out.println();
}
} }

53、。。

import java.util.Scanner;

public class java_10053 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[][] data = new int[N][N];
for(int i = 0;i < N;i++)
{
for(int j = 0;j < N;j++)
{
data[i][j] = sc.nextInt();
}
}
for(int i = 0;i < N;i++)
{
for(int j = N - 1;j > -1;j--)
{
System.out.print(data[i][j] + " ");
}
System.out.println();
}
} }

54、。。。

import java.util.Scanner;

public class java_10054 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[][] data = new int[N][N];
for(int i = 0;i < N;i++)
{
for(int j = 0;j < N;j++)
{
data[i][j] = sc.nextInt();
}
}
for(int i = N - 1;i > -1;i--)
{
for(int j = N - 1;j > -1;j--)
{
System.out.print(data[j][i] + " ");
}
System.out.println();
}
} }

55、。。。

import java.util.Scanner;

public class java_10055 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[][] data = new int[N][N];
for(int i = 0;i < N;i++)
{
for(int j = 0;j < N;j++)
{
data[i][j] = sc.nextInt();
}
}
for(int i = 0;i < N;i++)
{
for(int j = N - 1;j > -1;j--)
{
System.out.print(data[j][i] + " ");
}
System.out.println();
}
} }

56、

import java.util.Scanner;

public class java_10055 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[][] data = new int[N][N];
for(int i = 0;i < N;i++)
{
for(int j = 0;j < N;j++)
{
data[i][j] = sc.nextInt();
}
}
for(int i = 0;i < N;i++)
{
for(int j = N - 1;j > -1;j--)
{
System.out.print(data[j][i] + " ");
}
System.out.println();
}
} }

57、忘写了 OvO


58、

import java.util.Scanner;

public class java_10058 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int count = 0;
String str = sc.nextLine();
for(int i = 0;i < str.length();i++)
{
if(count < 0)
break;
String temp = str.substring(i,i + 1);
if(temp.equals("("))
count ++;
if(temp.equals(")"))
count --;
}
if(count == 0)
System.out.println(1);
else
System.out.println(0);
} }

59、

import java.util.Scanner;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack; public class java_10059 { public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
BigDecimal result=cal(str);
System.out.format("%.5f\n",result);
}
public static BigDecimal cal(String str)
{
List<String> list=new ArrayList<>();
char[] arr=str.toCharArray();
StringBuffer sb=new StringBuffer();
for(char c:arr)
{
if(c>='0'&&c<='9') {
sb.append(c);
}else if(c=='.')
{
if(sb.indexOf(".")>0)
{
throw new RuntimeException("非法字符");
}
sb.append(c);
}
else if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')') {
if(sb.length()>0)
{
list.add(sb.toString());
sb.setLength(0);
}
list.add(c+"");
}
else if(c==' ')
{
continue;
}else {
throw new RuntimeException("非法字符");
}
}
if(sb.length()>0)
{
list.add(sb.toString());
}
List<String> strList =new ArrayList<>();
Stack<String> stack=new Stack<>();
String temp;
for(String s:list)
{
if(s.equals("("))
{
stack.push(s);
}
else if(s.equals(")")) {
while(!(temp=stack.pop()).equals("("))
{
strList.add(temp);
}
}
else if (s.equals("*")||s.equals("/")) {
while(!stack.isEmpty()) {
temp=stack.peek();
if(temp.equals("*")||temp.equals("/"))
{
stack.pop();
strList.add(temp);
}else {
break;
}
}
stack.push(s);
}else if(s.equals("+")||s.equals("-"))
{
while(!stack.isEmpty()) {
temp=stack.peek();
if(!temp.equals("("))
{
stack.pop();
strList.add(temp);
}else {
break;
}
}
stack.push(s);
}
else {
strList.add(s);
}
}
while(!stack.isEmpty()) {
strList.add(stack.pop());
}
Stack<BigDecimal>newStack=new Stack<>();
for(String s:strList)
{
if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")) {
BigDecimal b1=newStack.pop();
BigDecimal b2=newStack.pop();
switch(s){
case "+":
newStack.push(b2.add(b1));
break;
case "-":
newStack.push(b2.subtract(b1));
break;
case "*":
newStack.push(b2.multiply(b1));
break;
case "/":
newStack.push(b2.divide(b1,9,BigDecimal.ROUND_HALF_UP));
break;
}
}
else {
newStack.push(new BigDecimal(s));
}
}
return newStack.peek();
} }

60、

import java.util.Scanner;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack; public class java_10060{
public static void main(String[] args) throws Exception{
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
String str=sc.nextLine().replace(" ", "");
BigDecimal result=cal(str);
System.out.format("%.5f\n",result);
}
public static BigDecimal cal(String str)
{
List<String> list=new ArrayList<>();
char[] arr=str.toCharArray();
StringBuffer sb=new StringBuffer();
for(char c:arr)
{
if(c>='0'&&c<='9') {
sb.append(c);
}else if(c=='.')
{
if(sb.indexOf(".")>0)
{
throw new RuntimeException("非法字符");
}
sb.append(c);
}
else if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')') {
if(sb.length()>0)
{
list.add(sb.toString());
sb.setLength(0);
}
list.add(c+"");
}
else if(c==' ')
{
continue;
}else {
throw new RuntimeException("非法字符");
}
}
if(sb.length()>0)
{
list.add(sb.toString());
}
List<String> strList =new ArrayList<>();
Stack<String> stack=new Stack<>();
String temp;
for(String s:list)
{
if(s.equals("("))
{
stack.push(s);
}
else if(s.equals(")")) {
while(!(temp=stack.pop()).equals("("))
{
strList.add(temp);
}
}
else if (s.equals("*")||s.equals("/")) {
while(!stack.isEmpty()) {
temp=stack.peek();
if(temp.equals("*")||temp.equals("/"))
{
stack.pop();
strList.add(temp);
}else {
break;
}
}
stack.push(s);
}else if(s.equals("+")||s.equals("-"))
{
while(!stack.isEmpty()) {
temp=stack.peek();
if(!temp.equals("("))
{
stack.pop();
strList.add(temp);
}else {
break;
}
}
stack.push(s);
}
else {
strList.add(s);
}
}
while(!stack.isEmpty()) {
strList.add(stack.pop());
}
Stack<BigDecimal>newStack=new Stack<>();
for(String s:strList)
{
if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")) {
BigDecimal b1=newStack.pop();
BigDecimal b2=newStack.pop();
switch(s){
case "+":
newStack.push(b2.add(b1));
break;
case "-":
newStack.push(b2.subtract(b1));
break;
case "*":
newStack.push(b2.multiply(b1));
break;
case "/":
newStack.push(b2.divide(b1,9,BigDecimal.ROUND_HALF_UP));
break;
}
}
else {
newStack.push(new BigDecimal(s));
}
}
return newStack.peek();
}
}

最新文章

  1. JavaScript学习笔记1之基础知识点
  2. zabbix监控系列(3)之zabbix触发器格式配置
  3. easyUI draggable插件使用不当,导致拖动div内部文本框无法输入;设置echarts数据为空时就显示空白,不要动画和文字
  4. Android 数据通信
  5. javaSE第一天
  6. Codeforces Round #342 (Div. 2) A - Guest From the Past 数学
  7. 安装完oracle重新启动后报ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务(重启前正常)
  8. 返回当前页面title、url等操作
  9. C# 语言的多线程编程,完全是本科OS里的知识
  10. JDBC(MySQL)一周学习总结(二)
  11. 第1章 初始Docker容器
  12. (haut oj 1261 ) 地狱飞龙 利用不定积分求值
  13. Java学习笔记34(集合框架八:综合案例:模拟斗地主的洗牌发牌)
  14. Machine Learning Netsite
  15. mysql 字符串数值计算 精度丢失
  16. November 14th, 2017 Week 46th Tuesday
  17. heat 用法 示例
  18. (转)Python 3 collections.defaultdict() 与 dict的使用和区别
  19. Vundle,Vim 的 Bundle(转)
  20. String.valueof;和String = &quot;&quot;+1;的区别

热门文章

  1. Djnago中缓存配置(redis配置案例)
  2. Eclipse 从SVN检出项目之《文件夹 “” 已不存在 》
  3. Apollo模块文章
  4. 《剑指offer》面试题1:赋值运算函数
  5. 002.MEMS应用在开关电源上,实现大功率超小型化
  6. 利用angular4和nodejs-express构建一个简单的网站(七)—用户注册之ReactiveForm
  7. 面试题:给你个id,去拿到name,多叉树遍历
  8. ccf201912-1 报数 C++代码实现
  9. FastAPI(七十四)实战开发《在线课程学习系统》接口开发-- 删除留言
  10. 日志、第三方模块(openpyxl模块)