鱼C论坛

 找回密码
 立即注册
查看: 1227|回复: 0

[技术交流] Disarium Number (Special Numbers Series #3)

[复制链接]
发表于 2020-1-29 12:54:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Definition

Disarium number is the number that The sum of its digits powered with their respective positions is equal to the number itself.

Task

Given a number, Find if it is Disarium or not .

  1. #include <string>
  2. #include <cmath>

  3. using namespace std;

  4. string int2str(int n)
  5. {
  6.     int m = n;
  7.     char s[100];
  8.     char ss[100];
  9.     int i = 0, j = 0;
  10.     if (n < 0)
  11.     {
  12.         m = 0 - m;
  13.         j = 1;
  14.         ss[0] = '-';
  15.     }
  16.     while (m > 0)
  17.     {
  18.         s[i++] = m % 10 + '0';
  19.         m /= 10;
  20.     }
  21.     s[i] = '\0';
  22.     i = i - 1;
  23.     while (i >= 0)
  24.     {
  25.         ss[j++] = s[i--];
  26.     }
  27.     ss[j] = '\0';
  28.     string res = ss;
  29.     return res;
  30. }

  31. int char2int(char ch)
  32. {
  33.     switch (ch)
  34.     {
  35.         case '0': return 0; break;
  36.         case '1': return 1; break;
  37.         case '2': return 2; break;
  38.         case '3': return 3; break;
  39.         case '4': return 4; break;
  40.         case '5': return 5; break;
  41.         case '6': return 6; break;
  42.         case '7': return 7; break;
  43.         case '8': return 8; break;
  44.         case '9': return 9; break;
  45.     }
  46. }

  47. string disariumNumber (int number )
  48. {
  49.     string res = int2str(number);
  50.     int n = 0;
  51.     for (int i = 1; i <= res.size(); i++) {
  52.         n += pow(char2int(res[i - 1]), i);
  53.     }
  54.     if (n == number) return "Disarium !!";
  55.     return "Not !!";
  56. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 05:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表