首页 > 试题广场 >

进制间转换

[编程题]进制间转换
  • 热度指数:7901 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
设计一个函数, 可以将任意十进制的数, 转换成任意2到9的进制表示的形式

输入描述:
需要转换的数字x(0<=x<=1e18) 转换使用的进制k(2<=k<=9)


输出描述:
对应进制的结果
示例1

输入

33 2

输出

100001
#include <bits/stdc++.h>
using namespace std;

int main(){
    long x;
    int k;
    cin>>x>>k;
    stack<int> s;
    if(x==0)
        cout<<0<<endl;
    else{
        while(x){
            s.push(x%k);
            x /= k;
        }
        while(!s.empty()){
            cout<<s.top();
            s.pop();
        }
        cout<<endl;
    }
    return 0;
}

发表于 2019-11-09 01:02:08 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long x = scanner.nextLong();
        long k = scanner.nextLong();
        System.out.println(radix(x, k));
    }

    private static String radix(long x, long k) {
        StringBuilder sb = new StringBuilder();
        if (x == 0) {
            sb.append(0);
        }
        while (x != 0) {
            sb.append(x % k);
            x /= k;
        }
        return sb.reverse().toString();
    }
}
发表于 2019-07-08 15:30:57 回复(0)
import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] str=br.readLine().split(" ");
        long n=Long.parseLong(str[0]);
        int m=Integer.parseInt(str[1]);
        System.out.println(Long.toString(n,m));
    }
}

发表于 2020-05-25 11:20:18 回复(0)
#include <iostream>
#define ULLI unsigned long long int

using namespace std;

string conv(ULLI x, int base) {
  if (!x) return "";
  return conv(x / base, base) + to_string(x % base);
}

int main(const int argc, const char* argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ULLI x;
  int base;
  
  fscanf(stdin, "%lld %d", &x, &base);
  if (!x) {
    cout << "0";
    return 0;
  }
  
  cout << conv(x, base);
  return 0;
}

发表于 2021-08-08 12:17:57 回复(0)
直接x不断除以k取余就行,但要注意x=0时不用转化,2~9进制全是0
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] params = br.readLine().trim().split(" ");
        long x = Long.parseLong(params[0]);
        int k = Integer.parseInt(params[1]);
        StringBuilder sb = new StringBuilder();
        while(x > 0){
            sb.append(x % k);
            x /= k;
        }
        System.out.println(sb.length() > 0? sb.reverse(): x);
    }
}


发表于 2021-02-27 11:39:13 回复(0)
题目要求将该整数转成2~9进制,用到的就是简单的数学连除取余,比如20转换成3进制,就是拿20/3取余,20 % 3 = 2,20 / 3 = 6; 6 % 3 = 0,6 / 3 = 2; 2 % 3 = 2;2 / 3 = 0。循环结束。将所有余数组成一个数组就是20的3进制表示,也就是 202,验算一下就是 202 = 2*(3^0) + 0*(3^1) + 2*(3^2) = 20。
所以这个题的思路就很明显了,输入x,while(x > 0),然后连除取余,用一个string记录余数即可。
#include <iostream>
#include <string>
using namespace std;

int main(){
    long long x;
    int k;
    string str;
    cin >> x >> k;
    if(x == 0){
        cout << 0 << endl;
        return 0;
    }
    while(x){
          if(x % k == 0){
              str = to_string(0) + str;
          } else{
              str = to_string(x % k) + str;
          }
        x /= k;
    }
    cout << str << endl;
    return 0;
}

 
发表于 2020-07-01 20:52:01 回复(0)
a,b = list(map(int,input().split())),''
while a[0]:
    a[0],b = a[0] // a[1],b + str(a[0] % a[1])
print(b[::-1] if b else 0)

发表于 2020-03-16 15:36:38 回复(0)
#include <bits/stdc++.h>
using namespace std;
string changeNum(long long num,long long x){
    if(num==0)
        return "0";
    stack<int> st;
    while(num>0){
        st.push(num%x);
        num/=x;
    }
    string str;
    while(!st.empty()){
        str+=st.top()+'0';
        st.pop();
    }
    return str;
}
int main(){
    long long n,x;
    cin>>n>>x;
    cout<<changeNum(n,x);
    return 0;
}

发表于 2019-10-23 15:53:36 回复(0)
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <list>
#include <math.h>
#include <set>
#include <cstdio>
#include <queue>
#include <sstream>
#include <stack>

//#define DBL_MAX 1.7976931348623158e+308

using namespace std;
typedef long long ll;
#define BIG 1000000000
//习惯性带上上面

void f(ll n, int x) {
    stack<int> sta;

    while (n != 0) {
        sta.push(n % x);
        n = n / x;
    }
    while (!sta.empty()) {
        cout << sta.top();
        sta.pop();
    }
    cout << endl;
}

int main() {
    ll n;
    int x;
    cin >> n >> x;
    if (n == 0) {
        cout << 0 << endl;
    } else{
        f(n, x);
    }
    return 0;
}


发表于 2019-10-04 21:10:59 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong(), radix = sc.nextLong();
        StringBuffer sb = new StringBuffer();
        if (n == 0) { sb.append(0); }
        while (n > 0) {
            sb.append( n % radix);
            n /= radix;
        }
        System.out.println(sb.reverse().toString());
        return;
    }
}

发表于 2019-09-10 17:53:20 回复(0)
""""
进制转换
"""

if __name__ == "__main__":
    x, k = map(int, input().strip().split())
    ans = []
    while x:
        ans.insert(0, x % k)
        x //= k
    if not ans:
        ans.append(0)
    print(''.join(map(str, ans)))

发表于 2019-07-13 13:11:11 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long x,k;
    cin>>x>>k;
    string res="";
    while(x>=k)
    {
        res.insert(res.begin(),x%k+'0');
        x/=k;
    }
    res.insert(res.begin(),x%k+'0');
    cout<<res<<endl;
    return 0;
}

发表于 2019-07-03 21:31:12 回复(0)
我感觉几行代码就行,答案可能有问题
 
for line in sys.stdin:
    a = line.split()
    num = int(a[0])
    jinzhi = int(a[1])
    s = ""
    while num / jinzhi != 0:
        s += str(int(num % jinzhi))
        num = int(num / jinzhi)
    print(s[::-1])
发表于 2023-03-30 17:16:20 回复(0)
public class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            long num = sc.nextLong();
            int k = sc.nextInt();
            System.out.println(transfer(num, k));
        }
    }

    public static String transfer(long num, int k) {
        if (num == 0) return "0";
        StringBuilder sb = new StringBuilder();
        while (num != 0) {
            sb.append(num % k);
            num /= k;
        }
        return sb.reverse().toString();
    }

}

发表于 2023-03-04 18:11:53 回复(0)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long num = sc.nextLong();
        long j = sc.nextInt();
        long z, y;
        ArrayList<Long> res = new ArrayList<>();
        if(num == 0){
            System.out.println(0);
        }else{
            while(num != 0){
                z = num/j;
                y = num%j;
                res.add(y);
                num = z;
            }
            Collections.reverse(res);
        }

        for (long n: res) {
            System.out.print(n);
        }

    }
}

发表于 2021-09-08 15:40:14 回复(0)
递归或者栈
#include<iostream>
#include<stack>
using namespace std;
void f(long long x,int t){
    if(x/t==0){
        cout<<x;
        return ;
    } ;
    f(x/t,t);
    cout<<x%t;
} 
void f1(long long x,int t){
    stack<int> s1;
    while(x){
        s1.push(x%t);
        x/=t;
    }
    if(s1.empty()){
        cout<<0;
        return ;
    }
    while(!s1.empty()){
        cout<<s1.top();
        s1.pop();
    }
}
int main(){
    long long x;int t;
    cin>>x>>t;
    f(x,t);//递归
    //f1(x,t);//栈
    return 0;
}


发表于 2020-12-01 17:36:49 回复(0)
#include<iostream>
#include<vector>
using namespace std;
int main() {
    long long int n;
    int m;
    vector<int> s;
    while (cin >> n >> m) {
        int a = 0, b = 0, c = 0;
        if(n==0){
            cout<<0;
        }
        while (n != 0) {
            a = n % m;
            s.push_back(a);
            n = (n - a) / m;
        }
        for (int i = s.size() - 1; i >= 0; i--) {
            cout << s[i];
        }
        cout << endl;
    }
    return 0;
}

发表于 2020-09-08 16:42:05 回复(0)
x,k=map(int,input().split())
s=''
if x==0:
    print(0)
else:
    while x:
        s=str(x%k)+s
        x=x//k
    print(s)

发表于 2020-05-26 11:05:03 回复(0)
#include <iostream>
(720)#include <string>
using namespace std;
int main(){
    long  x,k;
    cin>>x>>k;
    string ans="";
    if (x==0)
        ans="0";
    else{
        while (x>0){
            ans=to_string(x%k)+ans;
            x/=k;
        }
    }
    cout<<ans;
}

发表于 2020-04-17 10:42:53 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main() {
	long long x;
    int k,q;
	cin >> x >> k;
	vector<int>m;
	do
	{
		int p;
		p = x%k;
		q = x / k;
		x /= k;
		m.push_back(p);
	} while (q!= 0);
	for(int i=m.size()-1;i>=0;i--)
		cout << m[i];
	cout << endl;
	return 0;
}

发表于 2020-03-26 19:51:49 回复(0)