任意进制转换的总结 (c和java)

27 篇文章 1 订阅
20 篇文章 0 订阅

poj 1546 任意进制转换的总结 用java可以简单的实现

分类: ACM | 标签:  poj    1546    进制    转换    BigInteger  
2009-07-27 23:07阅读( 762) 评论(0)


原题见poj:http://acm.pku.edu.cn/JudgeOnline/problem?id=1546

题目意思简单描述:实现16以内的进制之间的转换(其实可以扩展到36以内的进制之间的转换

主要思路:以十进制数为中间结果,先转换为十进制,然后转换为要求的进制。任意进制转换为十进制采用位权表示法,十进制转换为任意进制采用

         整除和取余,采用堆栈的数据结构。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
char fin[100];
int convent1(char ch[],int base)
{
 int len = strlen(ch);
 int i;
 int temp;
 int re=0;
 for(i=0;i<len;i++)
 {
  if(ch[i]=='A')
   temp = 10;
  if(ch[i]=='B')
   temp = 11;
  if(ch[i]=='C')
   temp = 12;
  if(ch[i]=='D')
   temp = 13;
  if(ch[i]=='E')
   temp = 14;
  if(ch[i]=='F')
   temp = 15;
  if(ch[i]=='0')
   temp = 0;
  if(ch[i]=='1')
   temp = 1;
  if(ch[i]=='2')
   temp = 2;
  if(ch[i]=='3')
   temp = 3;
  if(ch[i]=='4')
   temp = 4;
  if(ch[i]=='5')
   temp = 5;
  if(ch[i]=='6')
   temp = 6;
  if(ch[i]=='7')
   temp = 7;
  if(ch[i]=='8')
   temp = 8;
  if(ch[i]=='9')
   temp = 9;
  re = re+temp*pow((double)base,(double)len-1-i);

 }
 return re;

}
void convent2(int num,int base)
{
 char chan[100];
 char res[100];
 int temp;
 int top = 0;
 while(num)
 {
  temp = num%base;
  num  = num/base;
  if(temp==0)
   chan[top]='0';
  if(temp==1)
   chan[top]='1';
  if(temp==2)
   chan[top]='2';
  if(temp==3)
   chan[top]='3';
  if(temp==4)
   chan[top]='4';
  if(temp==5)
   chan[top]='5';
  if(temp==6)
   chan[top]='6';
  if(temp==7)
   chan[top]='7';
  if(temp==8)
   chan[top]='8';
  if(temp==9)
   chan[top]='9';
  if(temp==10)
   chan[top]='A';
  if(temp==11)
   chan[top]='B';
  if(temp==12)
   chan[top]='C';
  if(temp==13)
   chan[top]='D';
  if(temp==14)
   chan[top]='E';
  if(temp==15)
   chan[top]='F';
  top++;

 }
 int i=0;
 for(top--;top>=0;top--)
 {
  res[i] = chan[top];
  i++;
 }
 res[i]='\0';
 strcpy(fin,res);//strcpy()函数的参数是字符串,那么在字符数组最后要加上'\0',不然就会出现异常

 

}
int main()
{

 int base,changeTo;
 char str[100];
 int mid;
 int len;
 while(scanf("%s%d%d",str,&base,&changeTo)!=EOF)
 {
  mid = convent1(str,base);
  convent2(mid,changeTo);
        len = strlen(fin);
  if(len>7)
   printf("%s\n","  ERROR");
  else
   printf("%7s\n",fin);
 }
 return 0;
}

其实,用java中BigInteger可以更加简单的实现

BigInteger(String val, int radix) 
          将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。

toString

public String toString(int radix)
返回此 BigInteger 的给定基数的字符串表示形式。如果该基数超出从  Character.MIN_RADIX 到  Character.MAX_RADIX(包括)这一范围,则其默认值为 10( Integer.toString 就是这种情况)。使用由  Character.forDigit 提供的从数字到字符的映射,并在需要时在前面加一个负号。(此表示形式与  (String, int) 构造方法兼容。)

参数:
radix - 字符串表示形式的基数。
返回:
此 BigInteger 给定基数的字符串表示形式。
另请参见:
Integer.toString(int, int)Character.forDigit(int, int)BigInteger(java.lang.String, int)

采用以上便可以实现36以内任意进制的转换:例如

import java.math.BigInteger;
public class Main1546 {

 /**
  * @param args

   这里只是演示一下功能
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String str = "1A";  //原始字符串
  BigInteger big = new BigInteger(str,15);//以15进制的字符串str构造一个BigInteger
  String str1 = big.toString(2);          //将这个15进制转换为2进制表示的字符串
  System.out.println(str1);              //呵呵,简单吧

 }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值