close

題目概要:

給定一數字,將此數字以10進位來看,並找出轉2進位後共有多少個1,以及16進位來看,轉2進位後共有多少個1。

解題方向:

依計概所教的方法去求出2進位,並計算有多少個1。

程式碼:

//Java
import java.util.Scanner;
class main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int cases=sc.nextInt(); //多少case需要被處理。
while((cases--)>0){
int n=sc.nextInt();
//Output。
System.out.println(base10(n)+" "+base16(n));
}
}
//10進位轉2進位,並回傳有多少個1。
public static int base10(int n){
int count=0;
while(n>0){
if(n%2==1) count++;
n/=2;
}
return count;
}
//16進位轉2進位,並回傳有多少個1。
public static int base16(int n){
int count=0;
while(n>0){
int temp=n%10;
n/=10;
while(temp>0){
if(temp%2==1) count++;
temp/=2;
}
}
return count;
}
}
view raw uva10019.java hosted with ❤ by GitHub

arrow
arrow
    文章標籤
    Java
    全站熱搜
    創作者介紹
    創作者 a7069810 的頭像
    a7069810

    紀錄自己的程式人生

    a7069810 發表在 痞客邦 留言(0) 人氣()