C语言与Java实现:数制转换

                                                                                         ”最简单的数值转换,求余。“

                   也许有好多的算法都可以进行数据转换。但是最根本的其实就是不断的去求余,16进制的转换,人们为了好区分,就把10改为A,11改为B...........

                                          其实最根本的就是不断的求余,从而得到答案,再在答案上面进行修改,从而便于记忆。


                                       如果你想输出含有字母的,请定义一个数组,最后输出对应数组里面的,数字,就可以了。

                                                                                                      

普通代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
typedef long long ll;
int vis[10010];
ll maxn;
int n;
int index;

void cmp()
{
    index=0;
    while(maxn>=n)
    {
        vis[index++] = maxn%n;
        maxn = maxn/n;
    }
    if(maxn!=0)
    {
        vis[index++] = maxn;
    }
    return ;
}

int main()
{
    int m;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        while(m--)
        {
            scanf("%lld",&maxn);
            if(maxn<n)
            {
                printf("%d\n",maxn);
                continue;
            }
            cmp();
            for(int i=index-1; i>=0; i--)
            {
                printf("%d",vis[i]);
            }
            printf("\n");
        }
    }
    return 0;
}

含字母的代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

typedef long long ll;
int vis[10010];
ll maxn;
int n;
int index;

void cmp()
{
    index=0;
    while(maxn>=n)
    {
        vis[index++] = maxn%n;
        maxn = maxn/n;
    }
    if(maxn!=0)
    {
        vis[index++] = maxn;
    }
    return ;
}

int main()
{
    char Character[]= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    int m;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        while(m--)
        {
            scanf("%lld",&maxn);
            if(maxn<n)
            {
                printf("%c\n",Character[maxn]);
                continue;
            }
            cmp();
            for(int i=index-1; i>=0; i--)
            {
                printf("%c",Character[vis[i]]);
            }
            printf("\n");
        }
    }
    return 0;
}


                                             以下的Java代码,是利用自己定义的栈实现的!

                                                    如有不对,请各位大佬,留言!留言,我看到就立即更正!


附上的Java代码:
import java.lang.reflect.Array;
import java.util.Scanner;
class SeqStack<E> {
	private int maxsize; // 顺序栈的容量
	private E[] data; // 数组,用于存储顺序栈中的数据元素
	private int top; // 指示顺序栈的栈顶
	
	
	public int getMaxsize() {
		return maxsize;
	}

	public void setMaxsize(int maxsize) {
		this.maxsize = maxsize;
	}

	public E[] getData() {
		return data;
	}

	public void setData(E[] data) {
		this.data = data;
	}

	public int getTop() {
		return top;
	}

	public void setTop(int top) {
		this.top = top;
	}

	// 初始化栈
	@SuppressWarnings("unchecked")
	public SeqStack(Class<E> type, int size) {
		data = (E[]) Array.newInstance(type, size);
		this.maxsize = size;
		top = 0;
	}

	// 入栈操作
	public E push(E item) {
		if (isFull()) {
			data[top] = item;
			top++;
			return item;
		} else
			return null;
	}

	// 出栈操作
	public E pop() {
		if (!empty()) {
			E temp = data[top - 1];
			top--;
			return temp;
		} else {
			return null;
		}
	}

	// 获取栈顶数据元素
	public E peek() {
		if (!empty()) {
			E temp = data[top - 1];
			return temp;
		} else {
			return null;
		}
	}

	// 求栈的长度
	public int size() {
		return top;
	}

	// 判断顺序栈是否为空
	public boolean empty() {
		if (this.top == 0)
			return true;
		return false;
	}

	// 判断顺序栈是否为满
	public boolean isFull() {
		if (this.maxsize == top)
			return false;
		return true;
	}

	// 清空栈a
	public void clear() {
		this.top = 0;
	}
}

public class Main {
	    public static void main(String[] args) {
	       Character  ch[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	       Scanner sc = new Scanner(System.in);
	       int n,m,k,t,x,y;
	       SeqStack<Character> stack = new SeqStack<Character>(Character.class,100);
	       n = sc.nextInt(); ///用例数目
	       k = sc.nextInt(); //转换的进制数
	       for(int i=0;i<n;i++){
	           m = sc.nextInt();  ///想要转换的数字
	           if(m==0) System.out.println(0);
	           else
	           {
	               stack.clear();
	                while(m>0)
	                {
	                  stack.push(ch[m%k]);
	                  m = m/k;
	                }
	              while(!stack.empty()){
	                  System.out.print(stack.pop());
	              }
	              System.out.println();
	           }
	       }
	   }
}

posted @ 2017-08-11 11:57  让你一生残梦  阅读(697)  评论(0编辑  收藏  举报