[ALGO-50] 數組查找及替換

算法訓練 數組查找及替換  
時間限制:1.0s   內存限制:512.0MB
問題描述
  給定某整數數組和某一整數b。要求刪除數組中可以被b整除的所有元素,同時將該數組各元素按從小到大排序。如果數組元素數值在A到Z的ASCII之間,替換爲對應字母。元素個數不超過100,b在1至100之間。
輸入格式
  第一行爲數組元素個數和整數b
  第二行爲數組各個元素
輸出格式
  按照要求輸出
樣例輸入
7 2
77 11 66 22 44 33 55
樣例輸出
11 33 55 M
說明:藍橋杯官網上的“樣例輸入”格式有誤,在此我已經改爲正確的格式了

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		while (scanner.hasNext()) {
			int n = scanner.nextInt();
			int b = scanner.nextInt();

			List<Integer> nums = new ArrayList<>();
			for (int i = 0; i < n; i++) {
				int temp = scanner.nextInt();
				if (temp % b != 0) {
					nums.add(temp);
				}
			}

			Collections.sort(nums);

			for (int i = 0; i < nums.size(); i++) {
				if (nums.get(i) >= 'A' && nums.get(i) <= 'Z') {
					int temp = nums.get(i);
					char ch = (char) temp;
					System.out.print(ch);
				} else {
					System.out.print(nums.get(i));
				}
				System.out.print(i == nums.size() - 1 ? "\r\n" : " ");
			}
		}
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章