zoukankan      html  css  js  c++  java
  • 假期编程

    此博客链接:https://www.cnblogs.com/ping2yingshi/p/12308055.html

    进制转换(63min)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2031

    Problem Description
    输入一个十进制数N,将它转换成R进制数输出。
    Input
    输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)
    Output
    为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
    Sample Input
    7 2
    23 12
    -4 3
    Sample Output
    111
    1B
    -11
    题解:
            方法:对进制取余。
            思路:定义一个数组存储十进制N对R每次求得的余数。
            注意:
                    1.求得的余数倒着输出即为转换的进制数。
                    2.要考虑10进制数可能为负数情况。
                    3.当余数大于等于10就要考虑字母输出,像十六进制一样,余数为10输出A,余数为11输出B直到余数为15时,输出F。
           耗时原因:
                           1.余数超过9的,怎样转成字母费了老半天劲.
                           2.编译器输出乱码。
    代码如下:
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<string.h>
    int main(void)
    {  
          int N;
          int R;
          while(~scanf("%d %d",&N,&R))
          {
            int  result[1000]={0};
            int i=0;
            int k;
            if(N<0)
            {
                printf("-");
                N=-N;
             } 
            while(N>0)
            {   
                    result[i++]=N%R;
                    N=N/R;    
            }
            for(k=i-1;k>=0;k--)
            {
                if(result[k]==10)
                    printf("A");
                else if(result[k]==11)
                    printf("B");
                else if(result[k]==12)
                    printf("C");
                else if(result[k]==13)
                    printf("D");
                else if(result[k]==14)
                    printf("E");
                else if(result[k]==15)
                    printf("F");
                else
                    printf("%d",result[k]);    
            }
            
            printf("\n");     
          }
        return 0;
        
    }
            
    出来混总是要还的
  • 相关阅读:
    小程序解析html(使用wxParse)
    error: the HTTP rewrite module requires the PCRE library
    CMake Error at cmake/boost.cmake:76 (MESSAGE)
    Centos7安装mysql后无法启动,提示 Unit mysql.service failed to load:No such file or directory
    mysql的三种安装方式
    epel源
    cmake
    yum
    wget
    tar指令
  • 原文地址:https://www.cnblogs.com/ping2yingshi/p/12308055.html
Copyright © 2011-2022 走看看