每天幾個Java編程2:無重複三位數&計算獎金&尋找這個數&計算第幾天&sort函數&逆序數&求字符串長度

題目11:有1、2、3、4個數字,能組成多少個互不相同且無重複數字的三位數?都是多少?

分析:如何有兩位出現相同,變量加加


public class NoRepeatNumber {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for(int i=1;i<=4;i++){
			for(int j=1;j<=4;j++){
				if(i==j)continue;
				for(int k=1;k<=4;k++){
					if(k==i||k==j)continue;
					System.out.print(100*i+10*j+k+" ");
				}
			}
		}
	}

}

result

題目12:企業發放的獎金根據利潤提成。利潤(I)低於或等於10萬元時,獎金可提10%;利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分,可可提成7.5%;20萬到40萬之間時,高於20萬元的部分,可提成5%;40萬到60萬之間時高於40萬元的部分,可提成3%;60萬到100萬之間時,高於60萬元的部分,可提成1.5%,高於100萬元時,超過100萬元的部分按1%提成,從鍵盤輸入當月利潤I,求應發放獎金總數?

import java.util.Scanner;
public class SumAward {
	public static double sumMoneyAward(double i){
        if(i <= 10){
            return i * 0.1;
        }else if(i < 20){
            return ((i - 10) * 0.075 + 1);
        }else if(i < 40){
            return (i - 20) * 0.05;
        }else if(i < 60){
            return (i - 40) * 0.03;
        }else if(i < 100){
            return (i - 60) * 0.015;
        }else{
            return (i - 100) * 0.001;
        }
    }
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		double result=s.nextDouble();
		System.out.print("獎金總數爲"+sumMoneyAward(result));
	}

}

題目13:一個整數,它加上100後是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少?

public class FindNumber {
 
    public static void main(String[] args) {
        for(int i=1; i<100000; i++){
            if(Math.sqrt(i + 100) % 1 == 0 && Math.sqrt(i + 268) % 1 == 0){
                System.out.println(i);
            }
        }
    }
}

題目14:輸入某年某月某日,判斷這一天是這一年的第幾天?

分析:判斷平年還是閏年,閏年需要將二月的天數變成29天。

import java.util.Scanner;
public class TestDay {
	public static boolean isLeapYear(int y){
		if((y%4 == 0 && y%100 != 100) || y%400 == 0)
			return true;
		else 
			return false;
	}
	public static int sumDays(int y, int m, int d){
		int[] MonthDays = {31,28,31,30,31,30,31,31,30,31,30,31};
		if(isLeapYear(y)) MonthDays[1] = 29;
		int ans = 0;
		for(int i=0; i<m-1; i++){
			ans = ans + MonthDays[i];
		}
		return ans + d;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		String in=s.nextLine();
		int y = Integer.parseInt(in.substring(0, in.indexOf('-')));
		int m = Integer.parseInt(in.substring(in.indexOf('-') + 1, in.lastIndexOf('-')));
		int d = Integer.parseInt(in.substring(in.lastIndexOf('-') + 1));
		System.out.println(sumDays(y, m, d));

	}

}

result

題目15:輸入三個整數x,y,z,請把這三個數由小到大輸出。

分析:利用好庫裏的函數:一個Arrays.sort非常容易的就排序好了 .而且整數的個數也不限制在三個。

import java.util.Arrays;
import java.util.Scanner;
public class Rank {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		String result=s.nextLine();
		char ch[]=result.toCharArray();
		Arrays.sort(ch);
		String str=new String(ch);
		System.out.print(str);
	}

}

題目16:給一個不多於5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。

分析:因爲數據量不大,完全可以利用數組進行存儲。


public class ReverseNumber {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n,i;
		n=12345;
		i=0;
		int []mar=new int[5]; 
		while(n!=0){
			mar[i]=n%10;
			n=n/10;
			i++;
		}
		int temp=i;
		System.out.println("位數:"+temp);
		for(i=0;i<temp;i++){
			System.out.print(mar[i]);
		}
	}

}

題目17:寫一個函數,求一個字符串的長度,在main函數中輸入字符串,並輸出其長度。

分析:將字符串轉換字符數組


public class TestLength {
	public static int Length(String str){
		return str.toCharArray().length;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s="skdnks";
		System.out.print(Length(s));
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章