欧拉8项目 - C++
Project Euler 8 - C++
我正在尝试用 c++ 解决 https://projecteuler.net/problem=8,我认为我有正确的解决方案,但我得到的值非常大或很小。只是为了提供一些信息,我将巨大的数字放入文本文件中并读取文本文件,现在我正在尝试找到最大的 13 个相邻值。这是我的代码片段,但它是最重要的部分,我正在使用uint64_t并且我得到"18436407170275213312":
uint64_t maxV = 0;
int numOfLines = 0;
uint64_t count = 1;
getline(myfile, line);
int lineLen = line.size();
int index = 0;
// Iterating through the one line
while (index < lineLen - 12)
{
string iter = line.substr(index, 13);
// cout << iter << endl;
for (char e : iter)
{
count *= static_cast<uint64_t>(e);
}
if (count > maxV)
{
maxV = count;
}
count = 1;
index++;
}
myfile.close();
cout << maxV << endl;
我知道解决方案应该是什么,但我得到的是一个更大的数字,这在数学上没有意义。我不知道我遍历强并获取计数数字的方式是错误的,还是我用来存储计数和 maxV 的类型有问题?任何帮助将不胜感激!
>变量e
是一个字符。
所以如果字符是'0'
,则 ascii 值为 48。
当您对值 '0'
(十进制 48)上的 uint64 进行static_cast时,我认为您的意思是获取值 0。
但你实际上得到值 48。
试试这个:
count *= static_cast<uint64_t>(e - '0');
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 既然存在危险,为什么项目要使用-I include开关
- cmake在我的项目中所需的所有静态库都不成功
- QT通过C++添加映射QML项目
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在linux上调试巨大的C++项目
- 在其他文件中创建类时在 c++ 项目中不起作用
- 使外部项目可用于find_package CMake
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- 如何维护资源管理器项目视图中当前可见的项目列表
- 错误-我无法在VS2019中打开新的Qt项目
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗