演算法筆記_161:演算法提高 十進位數轉八位元(Java)

來源:互聯網
上載者:User

標籤:[]   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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.