蓝桥杯 算法设计_5 十进制转十六进制
2014-03-11 19:26
225 查看
题目:十进制转十六进制。
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try {
int n=Integer.parseInt(br.readLine());
System.out.println(jinzhi_10_16(n));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static char getChar(int x){
if(0<=x && 9>=x){
return (x+"").charAt(0);
}else{
char c='G';
switch(x){
case 10: c= 'A';break;
case 11: c= 'B';break;
case 12: c= 'C';break;
case 13: c= 'D';break;
case 14: c= 'E';break;
case 15: c= 'F';
}
return c;
}
}
public static String jinzhi_10_16(int n){
String t="";
if(n==0) t=0+"";
char c='0';
while(true){
if(n==0)break;
c=getChar(n%16);
t=c+t;
n=n/16;
}
return t;
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try {
int n=Integer.parseInt(br.readLine());
System.out.println(jinzhi_10_16(n));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static char getChar(int x){
if(0<=x && 9>=x){
return (x+"").charAt(0);
}else{
char c='G';
switch(x){
case 10: c= 'A';break;
case 11: c= 'B';break;
case 12: c= 'C';break;
case 13: c= 'D';break;
case 14: c= 'E';break;
case 15: c= 'F';
}
return c;
}
}
public static String jinzhi_10_16(int n){
String t="";
if(n==0) t=0+"";
char c='0';
while(true){
if(n==0)break;
c=getChar(n%16);
t=c+t;
n=n/16;
}
return t;
}
}
相关文章推荐
- 动易2006序列号破解算法公布
- C#数据结构与算法揭秘二
- 浅析STL中的常用算法
- JavaScript 组件之旅(二)编码实现和算法
- java数据结构和算法学习之汉诺塔示例
- python基础教程之python消息摘要算法使用示例
- php的hash算法介绍
- 将15位身份证补全为18位身份证的算法示例详解
- C++算法系列之日历生成的算法代码
- 1 2 3 4 5 6 7 8 9 = 110的java实现
- Sedgewick之巨著《算法》,与高德纳TAOCP一脉相承
- 【代码】Pythonの代码片段
- STL中算法
- 数据结构&算法学习
- 算法的时间复杂度
- 算法导论:选择排序的原理与实现
- PHP实现四种常用的排序算法
- 图解插入排序算法
- 一些常见算法的JavaScript实现
- 平方根sqrt()函数的底层算法效率