C语言与Java实现:数制转换
2017-08-11 11:56
183 查看
”最简单的数值转换,求余。“
也许有好多的算法都可以进行数据转换。但是最根本的其实就是不断的去求余,16进制的转换,人们为了好区分,就把10改为A,11改为B...........
其实最根本的就是不断的求余,从而得到答案,再在答案上面进行修改,从而便于记忆。
普通代码:
含字母的代码:
4000
如有不对,请各位大佬,留言!留言,我看到就立即更正!
附上的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代码,是利用自己定义的栈实现的!
4000
如有不对,请各位大佬,留言!留言,我看到就立即更正!
附上的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(); } } } }
相关文章推荐
- Callback 在Java和C语言中的实现
- C语言趣味程序百例精解之JAVA实现(46)列最简真分数
- C语言的语法分析器——java实现
- Java实现C语言语义分析(递归下降)
- JAVA中实现C语言的_FILE_,_LINE_,_FUNC_,_TIME_
- 通过JNI实现C语言设置Java二维字符串数组并返回该字符串数组
- 用C语言实现类似于JAVA readLine()的功能
- 【Java学习之三】用JNI实现Java和C语言的数据传递
- JNI实现java调用C语言实现底层代码
- Android NDK编程小试---实现java和c语言的互掉
- C语言写的方法转用java实现
- 用栈实现数制转换(java)
- JavaSE JNI 动态注册本地方法(c语言实现native层)
- C语言MD5加密算法的简单调用实现(另附:Java/Python/Shell/Golang方法)
- C语言趣味程序百例精解之JAVA实现(83):卡布列克常数
- java、C语言实现数组模拟栈
- C语言趣味程序百例精解之JAVA实现(51)谁是窃贼
- 自动生成Jni中Java调用C语言实现的签名函数名
- 在java中实现类似c语言中goto的功能
- c++和java总异常是如何用c语言实现的