2798(基础题)进制转换 poj 百练 2798(基础题)

poj 百练 2798(基础题)

回顾二进制到16进制的方法

可以直接做

也可以从二转到10,,10转到16

看似容易

实则很需要细心,,

如何摆弄下标位置,如何计算边界,如何存储结果

  1. #include <fstream>
  2. #include <string.h>
  3. #include <cstdio>
  4. using namespace std;
  5. #define _cdebbug
  6. //全局数据
  7. int main()
  8. {
  9. //重定向
  10. #ifdef _cdebbug
  11. freopen( "F:\\input.txt", "r", stdin );
  12. #endif
  13. char shuzi[ 12005];
  14. char resault[ 3000];
  15. int n;
  16. int i,j;
  17. int len;
  18. int len16;
  19. int k = 0;
  20. int temp;
  21. scanf( "%d",&n);
  22. while (n--)
  23. {
  24. scanf( "%s",shuzi);
  25. len = strlen(shuzi); //二进制数长度
  26. len16 = (len + 3) / 4; //16进制数长度
  27. resault[len16] = '\0';
  28. for (i = len - 1; i >= 0; i -= 4)
  29. {
  30. temp = 0;
  31. for (j = i - 3; j <= i; ++j)
  32. {
  33. if (j < 0)
  34. j = 0;
  35. temp *= 2;
  36. temp += shuzi[j] - '0';
  37. }
  38. if (temp <= 9 && temp >= 0)
  39. resault[--len16] = temp + '0';
  40. else
  41. resault[--len16] = temp - 10 + 'A';
  42. }
  43. printf( "%s\n",resault);
  44. }
  45. //解除重定向关闭文件
  46. #ifdef _cdebbug
  47. fclose( stdin);
  48. #endif
  49. return 0 ;

回顾二进制到16进制的方法

可以直接做

也可以从二转到10,,10转到16

看似容易

实则很需要细心,,

如何摆弄下标位置,如何计算边界,如何存储结果

  1. #include <fstream>
  2. #include <string.h>
  3. #include <cstdio>
  4. using namespace std;
  5. #define _cdebbug
  6. //全局数据
  7. int main()
  8. {
  9. //重定向
  10. #ifdef _cdebbug
  11. freopen( "F:\\input.txt", "r", stdin );
  12. #endif
  13. char shuzi[ 12005];
  14. char resault[ 3000];
  15. int n;
  16. int i,j;
  17. int len;
  18. int len16;
  19. int k = 0;
  20. int temp;
  21. scanf( "%d",&n);
  22. while (n--)
  23. {
  24. scanf( "%s",shuzi);
  25. len = strlen(shuzi); //二进制数长度
  26. len16 = (len + 3) / 4; //16进制数长度
  27. resault[len16] = '\0';
  28. for (i = len - 1; i >= 0; i -= 4)
  29. {
  30. temp = 0;
  31. for (j = i - 3; j <= i; ++j)
  32. {
  33. if (j < 0)
  34. j = 0;
  35. temp *= 2;
  36. temp += shuzi[j] - '0';
  37. }
  38. if (temp <= 9 && temp >= 0)
  39. resault[--len16] = temp + '0';
  40. else
  41. resault[--len16] = temp - 10 + 'A';
  42. }
  43. printf( "%s\n",resault);
  44. }
  45. //解除重定向关闭文件
  46. #ifdef _cdebbug
  47. fclose( stdin);
  48. #endif
  49. return 0 ;

猜你喜欢

转载自blog.csdn.net/blue_blue001/article/details/80900831