標籤:[] sys nbsp png href lis 程式 port print
目錄
1 問題描述
2 解決方案
1 問題描述
編寫函數,其功能為把一個十進位數轉換為其對應的八位元。程式讀入一個十進位數,調用該函數實現數制轉換後,輸出對應的八位元。
範例輸入
9274
範例輸出
22072範例輸入
18
範例輸出
22
2 解決方案
具體代碼如下:
import java.util.ArrayList;import java.util.Scanner;public class Main { public static void main(String[] args) { ArrayList<Long> list = new ArrayList<Long>(); Scanner in = new Scanner(System.in); long n = in.nextLong(); while(n > 0) { long temp = n % 2; n = n / 2; list.add(temp); } int len = list.size(); if(len % 3 == 1) { list.add(0L); list.add(0L); } else if(len % 3 == 2) { list.add(0L); } len = list.size() / 3; long[] result = new long[len]; int count = 0; for(int i = list.size() - 1;i >= 0;i = i - 3) { long a = list.get(i) * 2 * 2; long b = list.get(i - 1) * 2; long c = list.get(i - 2); result[count++] = a + b + c; } for(int i = 0;i < result.length;i++) System.out.print(result[i]); }}
演算法筆記_161:演算法提高 十進位數轉八位元(Java)