首页 > 试题广场 >

八进制

[编程题]八进制
  • 热度指数:31459 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入一个整数,将其转换成八进制数输出。

输入描述:
输入包括一个整数N(0<=N<=100000)。


输出描述:
可能有多组测试数据,对于每组数据,
输出N的八进制表示数。
示例1

输入

7
8
9

输出

7
10
11
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) System.out.println(Integer.toOctalString(scanner.nextInt()));
    }
}


发表于 2020-03-17 15:38:56 回复(0)
import java.util.Scanner;
import java.util.*;

    public class Main{
        public static void main(String args[]){
            Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            System.out.println(Integer.toString(in.nextInt(),8));
            }
        }
    }

发表于 2019-02-16 16:23:07 回复(0)
import java.util.Scanner;
import java.util.ArrayList;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            System.out.print(Integer.toOctalString(n));
        }
    }
}
java自带
发表于 2018-08-05 20:47:59 回复(0)
import java.util.*;
 public class Main{
     public static void main(String[] args){
         Scanner sc=new Scanner(System.in);
         while(sc.hasNext()){
             int n=sc.nextInt(); 
             Stack<Integer> stack=new Stack<Integer>();
             StringBuffer str=new StringBuffer();
             while(n!=0){
                 int y=n%8;
                 stack.push(y);
                 n/=8;
             }
             while(!stack.isEmpty()){
                 str.append(stack.pop());
             }
             System.out.println(str);
         }
     }
 }
发表于 2018-07-25 22:42:25 回复(0)

这... 算作弊了吧

import java.util.Scanner;

/**
 * @author Allen_Hua
 * @create_time 创建时间:May 10, 2018 3:43:56 PM 类说明
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            int n = scan.nextInt();
            System.out.println(Integer.toOctalString(n));
        }
    }
}
发表于 2018-05-10 15:49:19 回复(0)
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.print(Integer.toOctalString(in.nextInt()));
	}
}

单机通过,不能AC


import java.util.Scanner;

public class Main {
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int nextInt = in.nextInt();
		int num = 0;
		int i = 0;
		while (nextInt!=0) {
			num += nextInt%8*Math.pow(10, i);
			nextInt /=8;
			i++;
		}
		System.out.println(num);
	}
}

单机通过,不能AC

编辑于 2017-06-30 22:06:21 回复(0)
public class Main {
	
	public void doSome(int in){
		List<Integer> list=new ArrayList<Integer>();
		int did=8;
		int record=0;
		if(in>=0&&in<=7){
			System.out.println(in);
		}
		if(in>=8){
			while(in>0){
				int temp=in%did;
				list.add(temp);
				in/=did;
			}
			for(int i=list.size()-1;i>=0;i--){
				System.out.print(list.get(i));
			}
		}
		
	}

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		Main m=new Main();
		int input;
		while(true){
			input=s.nextInt();
			m.doSome(input);
		}
	}
}

牛客网的**编译器说不成功!
发表于 2017-06-11 18:51:04 回复(0)
   
import java.util.Scanner;

public class Main {
 
    public static void main (String args []) {
        Main octo = new Main();
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextInt()){  
            int number = scanner.nextInt();
        	System.out.println(octo.dec2oct(number));
        }

       
    }
    
    int dec2oct(int dec) {
        String oct = "";
        int quotient = dec;
        int reminder;
        while (quotient >= 8) {
            reminder = quotient % 8;
            quotient = quotient / 8;
            oct = oct + reminder;
        }
        oct = oct + quotient;
        StringBuffer sb = new StringBuffer(oct);
        sb.reverse();
        oct = sb.toString();
        return Integer.parseInt(oct);
    }
    
}

发表于 2017-05-02 10:30:12 回复(0)
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n1;
		while (in.hasNext()) {
			n1 = in.nextInt();
			System.out.printf("%o\n",n1);
		}
	}
}


发表于 2017-04-22 16:17:47 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int result = 0;
        int binary = input.nextInt();
        if(binary<0 || binary>100000){
            System.out.println("输入数据必须在0到100000之间。");
        }
        else{
            int fac = 0;
            while(binary != 0){
                result += (binary%8)*Math.pow(10,fac++);
                binary /= 8;
            }
            System.out.println(result);
        }
    }
}
发表于 2017-04-01 21:24:35 回复(0)
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int N;
int[] arr=new int[10]; 

while(in.hasNext()){
N=in.nextInt();
int y;
int s;
int i=0;
if(N>8){
while(N>=8){
s=N/8;
y=N%8;
   arr[i]=y;
i++;
N=s;
}
arr[i]=N;
}
else arr[i]=N;

for(int j=i;j>=0;j--){
System.out.print(arr[j]);
}
            System.out.print("\n");

}
}
}
发表于 2017-03-29 18:52:50 回复(0)
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
Scanner can=new Scanner(System.in);
int[] printnum = new int[5];
for(int i=0;i<printnum.length;i++){
printnum[i] = can.nextInt();
//System.out.println(printnum[i]);
}
for(int j=0;j<printnum.length;j++){
System.out.println(Integer.toOctalString(printnum[j])+'o');
}
}
}

发表于 2017-03-12 22:35:00 回复(0)

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        while (in.hasNext())
        {
            //call api, 实现参照jdk源码。java流输入确实慢。
           System.out.println(Integer.toOctalString(in.nextInt()));
        }
    }
}
发表于 2017-03-10 15:43:12 回复(0)

import java.util.Scanner; public class Main{

public static void main(String[] arg){

    Scanner scan = new Scanner(System.in);
    while(scan.hasNext()){
        String octer = String.valueOf(Integer.toOctalString(Integer.parseInt(scan.nextLine())));
        System.out.println(octer);

    }


}

}

发表于 2017-03-05 21:20:33 回复(0)
// 采用递归的方法 import java.util.Scanner; /**  * Created by Yuan on 2017/3/2.  */ public class te { public static void main(String[] args){
        Scanner scanner=new Scanner(System.in); while(scanner.hasNext()){ int num= scanner.nextInt(); changeNum(num);
            System.out.println();
        }
    } public static void changeNum(int num){ if(num==0) return; int mod=num%8; int div=num/8; changeNum(div);
        System.out.print(mod);
    }
}
发表于 2017-03-02 16:28:06 回复(0)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n; while (in.hasNext())
        {
            n=in.nextInt();
            System.out.printf("%o\n",n);//注意这里输出的是字符串而不是数字  }
    }
}

发表于 2016-11-05 12:29:44 回复(0)

问题信息

难度:
16条回答 17214浏览

热门推荐

通过挑战的用户

查看代码
八进制