1. 程式人生 > >演算法題一

演算法題一

package com.greatest.Array;

import java.util.Scanner;

public class ti {
	// 1.一個數組 ,讓每個元素去除第一個元素 ,得到的商作為被除數所在的位置
	public static void main(String[] args) {
		int[] arr = new int[] { 12, 34, 1, 45, -564, -56, 2 };
		// for(int i=0;i<arr.length;i++){
		// arr[i]=arr[i]/arr[0];
		// }
		for (int i = arr.length - 1; i >= 0; i--) {
			arr[i] = arr[i] / arr[0];
		}
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}
}

class Test1 {
	public static void main(String[] args) {
		//2. 編寫程式 :從鍵盤上讀入一個學生的成績 存放在變數score中
		// 根據score對應的值輸出所對應成績的等級
		Scanner sc = new Scanner(System.in);
		for (;;) {

			System.out.println("請輸入成績:");
			int score = sc.nextInt();
			char level;
			if (score >= 90) {
				level = 'A';
			} else if (score >= 70) {
				level = 'B';

			} else if (score >= 60) {
				level = 'C';
			} else {
				level = 'D';
			}
			System.out.println("等級為:" + level);
			if (score == 0) {
				return;
			}
		}
	}
}

class test2 {
	public static void main(String[] args) {
		int x = 4;
		int y = 1;
		if (x > 2) {

		}
	}

}

class Yanghuisanjiao {
	/*
	 * 3.楊輝三角
	 *  1	
		1	1	
		1	2	1	
		1	3	3	1	
		1	4	6	4	1	
		1	5	10	10	5	1	
		1	6	15	20	15	6	1	
		1	7	21	35	35	21	7	1	
		1	8	28	56	70	56	28	8	1	
		1	9	36	84	126	126	84	36	9	1
	 */
	public static void main(String[] args) {
		int[][] yanghui = new int[10][];
		// 1.初始化二維陣列
		for (int i = 0; i < yanghui.length; i++) {
			yanghui[i] = new int[i + 1];
		}
		// 2.顯示的為每個元素賦值
		for (int i = 0; i < yanghui.length; i++) {
			for (int j = 0; j < yanghui.length; j++) {
				yanghui[i][0] = yanghui[i][i] = 1;// 為外邊的賦值
				if (i > 1 && j > 0 && j < i) {
					yanghui[i][j] = yanghui[i - 1][j] + yanghui[i - 1][j - 1];
				}
			}
		}
		// 3.遍歷二維陣列
		for (int i = 0; i < yanghui.length; i++) {
			for (int j = 0; j < yanghui[i].length; j++) {
				System.out.print(yanghui[i][j] + "\t");
			}
			System.out.println();
		}
	}
}

class TestArray {
	//4. 金額轉換 如:105600123 => 壹億零仟伍佰陸拾零萬零仟壹佰貳拾叄圓整
	private static final char[] untits = new char[] { '圓', '拾', '佰', '仟', '萬', '拾', '佰', '仟', '億' };
	private static final char[] data = new char[] { '零', '壹', '貳', '叄', '肆', '伍', '陸', '柒', '捌', '玖' };

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("請輸入金額:");
		int money = s.nextInt();
		String strMoney = convert(money);
		System.out.println(strMoney);
	}

	public static String convert(int money) {
		StringBuffer sb = new StringBuffer("整");
		int init = 0;
		while (money != 0) {
			sb.insert(0, untits[init++]);
			int number = money % 10;
			sb.insert(0, data[number]);
			money /= 10;
		}
		return sb.toString();
	}
}

class StudentScore1{
	/*
	 * 5.從鍵盤讀入學生成績,找出最高分,並輸出學生成績等級。 成績>=最高分-10 等級為’A’ 成績>=最高分-20 等級為’B’ 成績>=最高分-30
	 * 等級為’C’ 其餘 等級為’D’
	 * 
	 * 提示:先讀入學生人數,根據人數建立int陣列,存放學生成績。
	 * 
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入學生的個數 ");
		int count = sc.nextInt();
		int[] scores = new int[count];
		int maxsco = 0;
		System.out.println("請輸入" + count + "個學生 的成績");
		for (int i = 0; i < scores.length; i++) {
			int score = sc.nextInt();
			scores[i] = score;// 給陣列賦值
			if (scores[i] > maxsco) {// 找最大的成績
				maxsco = scores[i];
			}
		}
		System.out.println("最高分為" + maxsco);
		for (int i = 0; i < scores.length; i++) {
			char level;
			if (scores[i] >= maxsco - 10) {
				level = 'A';

			} else if (scores[i] >= maxsco - 20) {
				level = 'B';
			} else if (scores[i] >= maxsco - 30) {
				level = 'C';
			} else {
				level = 'D';
			}

			System.out.println("Student" + i + "score is" + scores[i] + "grade is" + level);

		}

	}

}
class WanShu {
    //6.一個數如果恰好等於他的因子之和  這個數就成為完數
	  //列如 6=1+2+3
	  public static void main(String[] args) {
		int factor=0;
		for(int i=1;i<=1000;i++){
			for(int j=1;j<i;j++){
				if(i%j==0){
					factor+=j;//滿足累加
				}
			}
			if(i==factor){
				System.out.println(i);
			}
			factor=0;//重置
		}
	}
}
class mianshi {
	//7.從鍵盤上輸入年 月 日 判斷這天是當年的第幾天
	public  static  void  main(String [] args ){
		Scanner sc=new Scanner(System.in);
		System.out.println("請輸入年");
		int year=sc.nextInt();
		System.out.println("請輸入月:");
		int month=sc.nextInt();
		System.out.println("請輸入天");
		int day=sc.nextInt();
		int num=0;
		int yearday=0;
		switch(month){
		case 12:
			num+=30;
		case 11:
			num+=31;
		case 10:
			num+=30;
		case 9:
			num+=31;
		case 8:
			num+=31;
		case 7:
			num+=30;
		case 6:
			num+=31;
		case 5:
			num+=30;
		case 4:
			num+=31;
		case 3:
			if(year%4==0&& year%100!=0||year%400==0){
				num+=29;
				yearday=366;
				System.out.println("閏年");
			}else{
				num+=28;
				yearday=365;
				System.out.println("平年");
			}
		case 2:
			num+=31;
		case 1:
			num+=day;
}System.out.println(year+"年"+month+"月"+day+"是今年的第"+num+"天,今年還剩"+(yearday-num )+"天。");
}

}