%ddd 2023-11-12 15:09 采纳率: 86.7%
浏览 11
已结题

十进制转变成R进制编程

这是一个编程题模板。

编程实现将任意的十进制整数N转换成R进制数(R在2-16之间)。N<0则直接退出。

输入格式:
一行中输入两个数分别是N与R,两者之间以空格隔开。

输出格式:
将转换后的R进行数。

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-11-12 18:49
    关注

    【以下回答由 GPT 生成】

    ```c
    #include <stdio.h>
    
    // Function to convert decimal to any base
    void decimalToAnyBase(int N, int R) {
        if (N < 0) {
            printf("N should be a positive number");
            return;
        }
    
        // Array to store the digits of the result
        char result[100];
        int i = 0;
    
        // Convert the number from decimal to base R
        while (N > 0) {
            int remainder = N % R;
            if (remainder < 10) {
                result[i] = remainder + '0';
            } else {
                result[i] = remainder - 10 + 'A';
            }
            N = N / R;
            i++;
        }
    
        // Print the result in reverse order
        printf("The converted number is: ");
        for (int j = i - 1; j >= 0; j--) {
            printf("%c", result[j]);
        }
    }
    
    int main() {
        int N, R;
        printf("Enter N and R: ");
        scanf("%d %d", &N, &R);
        decimalToAnyBase(N, R);
        return 0;
    }
    

    ```



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月20日
  • 已采纳回答 11月12日
  • 创建了问题 11月12日

悬赏问题

  • ¥30 不会,学习,有偿解答
  • ¥15 SQL查询语句报错(检查)
  • ¥15 此表中公式应该怎么写
  • ¥15 求HI-TECH PICC 9.50 PL3安装包
  • ¥15 下载ctorch报错,求解
  • ¥15 如何入门学习c语言,单片机
  • ¥15 idea 编辑语言的选择
  • ¥15 Windows下部署Asmjit
  • ¥15 请问双层规划模型的上下层目标函数不一致,是如何保证迭代收敛性的
  • ¥30 微信小程序 前端页面内容搜索