Chinaunix首页 | 论坛 | 博客
  • 博客访问: 635008
  • 博文数量: 54
  • 博客积分: 3812
  • 博客等级: 上校
  • 技术积分: 992
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-16 20:53
文章分类

全部博文(54)

文章存档

2010年(10)

2009年(24)

2008年(20)

分类: C/C++

2008-12-20 16:44:58

    下面代码是使用递归的方式进行多进制的输出:
 

#include <iostream>
using namespace std;

void displayInBase(int n, int base)
{
    if (n > 0)
    {
        displayInBase(n/base, base);
        cout << n % base;
    }
}

int main()
{
    int number, base;
    cout << "Input number and base: ";
    cin >> number >> base;
    if (base >= 2 && base <= 10)
    {
        cout << " " << number << "(base 10) = ";
        displayInBase(number, base);
        cout << "(base " << base << ")" << endl;
    }
    else
    {
        cout << "The base shoud be [2,10]" << endl;
    }
    return 0;
}

阅读(1997) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~