将一个十进制非负整数转换为任意进制
  • 板块灌水区
  • 楼主bo_rui2008
  • 当前回复1
  • 已保存回复1
  • 发布时间2021/5/28 19:26
  • 上次更新2023/11/4 22:36:32
查看原帖
将一个十进制非负整数转换为任意进制
383416
bo_rui2008楼主2021/5/28 19:26

输入文件:in.in(从in.in文件中读取两个整数,第一个整数是对应的十进制数,第二个整数是进制数。)

输出文件:out.out(将结果输出到out.out文件中,输出一行,从前到后依次是转换后各个数位上的数字,用空格分开,不能输出多余空格。)

输入样例:13 2

输出样例:1 1 0 1

尝试代码如下:

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    FILE *fp,*tp; int a,b,c;
    if ( fp=fopen("in.in","r") ) {
      if ( tp=fopen("out.out","w+") ) {
        while ( !feof(fp) ) {
          if ( fscanf(fp,"%d",&a)==1 ) { 
             c=1; 
             b=0; 
             while(a){
             b+=a%10*c; a/=10; c*=2; 
             } 
             fprintf(tp,"%d ",b);
          }
        }
        fclose(tp);
      }  printf("Can't Out out.out'\n");
      fclose(fp);
    } 
    else printf("Can't Open in.in'\n");
    return 0;
}

请各位帮忙,我的最终答案是5 2不过不对

2021/5/28 19:26
加载中...