Java課程課堂作業程式碼
前言
本文章只是單純記錄課堂老師布置的課堂作業程式碼,題目都比較簡單,所以沒有寫解題思路,相信大家都能理解,當然其中有的解法和程式碼不是最優的,當時只是為了完成題目,後來也懶得改了,如果有不恰當或者不正確的地方,歡迎指出
備註:有的忘記記錄題目資訊了,有的題目直接在作業系統裡面提交了,請見諒,將就著看吧
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();
}
}