大数位的总和(10 ^ 9)? [英] sum of digits of large numbers (10^9)?

查看:46
本文介绍了大数位的总和(10 ^ 9)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决此特定问题:欧拉#16项目:

I am trying to solve this particular problem: Project Euler #16:

2 ^ 15 = 32768,其位数之和为3 + 2 + 7 + 6 + 8 =26.
数字2 ^ 1000的位数总和是多少?

2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 2^1000?

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   int num;
   cin >> num;
   long double n = pow(2, num);
   cout << fixed << n << endl;
   int sum = 0;
   while(n != 0.0)
   {
      sum = sum + fmod(n, 10);
      n = floor(n/10);
   }
   cout << sum << endl;
}

用户输入为1000时的输出为:

The output when user input is 1000 is:

1181

但是,互联网上提到的正确答案是 1366 ,而不是我得到的 1181 .

However, the internet mentions the correct answer to be 1366 instead of the 1181 I got.

推荐答案

第一个计算问题不是不是:

long double n = pow(2, num);

甚至是常规的 double 类型(最大值: 1.79769e + 308 )将保持 2 ^ 1000 = 1.07151e + 301 不会出现任何问题,特别是这是以2为基数的计算.

as even the regular double type (max: 1.79769e+308) will hold 2^1000 = 1.07151e+301 without any problems especially that this is a base 2 calculation.

请注意,您改用 pow() fmod() floor()版本的 long double 版本的 powl() fmodl() floorl().但这就是我所说的不是问题,并且不会改变任何内容.

Note that your using the double versions pow(), fmod() and floor() instead of the long double versions powl(), fmodl() and floorl(). But that is as I said not the problem and won't change anything.

但是while循环中的计算将缺少精度和舍入误差.此外,在对它们进行算术运算时,切勿检查浮点数是否存在(不)相等性,请参见

But the calculation in the while loop will lack in precision and rounding errors. Further never check floating point numbers for (un)equality when doing arithmetics with them, see here on SO (there are a lot of other discussion and threads in the web about that topic).

简而言之:
我假设您的 fmod() floor()将导致精度损失和舍入误差.

In short:
I assume that your fmod() and floor() will lead to precision loss and rounding errors.

这是一个可行的解决方案(参见ideone ),可以为您的案例转换 double std :: string 并迭代字符串的每个字符,然后将char转换为整数.

Here is a working solution (see at ideone) for your case converting the double to a std::string and iterating through every character of the string adding that char converted to an integer.

简而言之:
它只是使用初始计算(仅使用 double 进行计算)并将其转换为字符串.

In short:
It is just using the initial calculation (just with double) and convert that to a string.

#include <iostream>
#include <cmath>

int main ()
{
   int num;
   std::cin >> num;
   double n = pow(2, num); /* note I use just double */

   std::string nstr = std::to_string(n);
   const char* nstr_p = nstr.c_str();

   std::cout << std::fixed << nstr << std::endl;
   int sum = 0;
   while (std::isdigit(*nstr_p)) /* number contains dot and fractional (.0000) */
   {
      sum = sum + (*nstr_p - '0'); /* convert character to integer and sum it */
      nstr_p++;
   }
   std::cout << sum << std::endl;
}

输出:

1366

请注意,转换:(* nstr_p-'0')假定使用类似

Note that the conversion: (*nstr_p - '0') assumes an encoding like ASCII or others where numbers are incrementing without gaps in their hexadecimal representation.

这篇关于大数位的总和(10 ^ 9)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆