题目:

The four adjacent digits in the 1000-digit number that have the greatest product are 9 9 8 9 = 5832.

73167176531330624919225119674426574742355349194934

96983520312774506326239578318016984801869478851843

85861560789112949495459501737958331952853208805511

12540698747158523863050715693290963295227443043557

66896648950445244523161731856403098711121722383113

62229893423380308135336276614282806444486645238749

30358907296290491560440772390713810515859307960866

70172427121883998797908792274921901699720888093776

65727333001053367881220235421809751254540594752243

52584907711670556013604839586446706324415722155397

53697817977846174064955149290862569321978468622482

83972241375657056057490261407972968652414535100474

82166370484403199890008895243450658541227588666881

16427171479924442928230863465674813919123162824586

17866458359124566529476545682848912883142607690042

24219022671055626321111109370544217506941658960408

07198403850962455444362981230987879927244284909188

84580156166097919133875499200524063689912560717606

05886116467109405077541002256983155200055935729725

71636269561882670428252483600823257530420752963450

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

代码:将这个矩阵完全复制到一个txt文件中,进行读取计算

 1 #include<iostream>
2 //#include<stdio.h>
3 using namespace std;
4
5 int main() {
6 FILE* pFile = fopen("digits.txt","r");
7 if(pFile == NULL){
8 cout << "打开文件失败!" << endl;
9 return 0;
10 }
11 //unsigned int res = 1;
12 long long res = 1;
13 while(!feof(pFile)) {
14
15 char cnums[14];
16 bool end = false;
17 bool has_n = false;
18 for(int i = 0; i < 14; i++) {
19 if(feof(pFile)){
20 end = true;
21 break;
22 }
23 if(i == 13 && has_n == false){
24 break;
25 }
26 char c = fgetc(pFile);
27 if(c == '\n'){
28 has_n = true;
29 }
30 cnums[i] = c;
31 }
32 if(end)
33 break;
34 //要考虑是否有'/n'和'0'
35 //unsigned int m = 1;
36 long long m = 1;
37 bool haszero = false;
38 int j=0;//可能要用来表示0的位置
39 int k = 13;
40 if(has_n){
41 k = 14;
42 }
43 for(j = 0; j<k;j++) {
44 if(cnums[j] != '0' && cnums[j] != '\n') {
45 m = m*(cnums[j]-'0');
46 }
47 else if (cnums[j] == '0'){
48 haszero = true;
49 break;//13个数字中含有0
50 }
51 }
52 if(haszero == false) {//13个数字中不含0,比较结果,并返回指针
53 if(m > res){
54 res = m;
55 }
56 fseek(pFile, -(k-1), SEEK_CUR );
57 //cout << fgetc(pFile);
58 } else {//13个数字中含有0,将指针返回到0之后
59 fseek(pFile, -k, SEEK_CUR );
60 fseek(pFile, j+1, SEEK_CUR );
61 //cout << fgetc(pFile);
62 }
63 cout << res << endl;
64 }
65 cout << res;
66 if(fclose(pFile) == EOF) {
67 cout << "关闭文件失败." << endl;
68 }
69 system("pause");
70 return 0;
71 }

ProjectEuler 008题的更多相关文章

  1. ProjectEuler 做题记录

    退役选手打发时间的PE计划 挂在这里主要是dalao们看到有什么想交流的东西可以私聊哦(站内信或邮箱吧)~~当然现在高三也不怎么能上网. 2017/8/11  595 :第一题QAQ 2017/8/1 ...

  2. ProjectEuler 005题

    题目: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any ...

  3. ProjectEuler 009题

    题目: A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = c2 For exam ...

  4. ProjectEuler 007题

    题目:By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is ...

  5. ProjectEuler 006题

    题目: The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square ...

  6. ProjectEuler 004题

    1 #include<iostream> 2 using namespace std; 3 4 int main() { 5 bool isPalindromic (int num); 6 ...

  7. ProjectEuler 003题

    1 //题目:The prime factors of 13195 are 5, 7, 13 and 29. 2 //What is the largest prime factor of the n ...

  8. nim也玩一行流,nim版的list comprehension

    nim 是一门风格类似python的静态编译型语言,官方网站:http://nim-lang.org 如果你想折腾nim的编辑环境,可以用sublime text3 +插件nimlime,notepa ...

  9. ProjectEuler && Rosecode && Mathmash做题记录

    退役选手打发时间的PE计划 挂在这里主要是dalao们看到有什么想交流的东西可以私聊哦(站内信或邮箱吧) 2017/8/11  PE595 :第一题QAQ 2017/8/12  PE598 2017/ ...

随机推荐

  1. ClickHouse学习系列之八【数据导入迁移&同步】

    背景 在介绍了一些ClickHouse相关的系列文章之后,大致对ClickHouse有了比较多的了解.它是一款非常优秀的OLAP数据库,为了更好的来展示其强大的OLAP能力,本文将介绍一些快速导入大量 ...

  2. 在 Intenseye,为什么我们选择 Linkerd2 作为 Service Mesh 工具(Part.2)

    在我们 service mesh 之旅的第一部分中,我们讨论了"什么是服务网格以及我们为什么选择 Linkerd2?".在第二部分,我们将讨论我们面临的问题以及我们如何解决这些问题 ...

  3. Appium - adb命令操作

    1.Android 调试桥adb ( Android Debug Bridge)是一个通用命令行工具,其允许您与模拟器实例或连接的 Android 设备进行通信.它可为各种设备操作提供便利,如安装和调 ...

  4. springMVC-5-视图解析器

    视图和视图解析器工作流程 第一步:获取到ModelAndView对象 请求处理方法执行完成后,无论返回是String,View 还是 ModeMap 类型,Spring MVC 也会在内部将它们装配成 ...

  5. [HNOI2008]GT考试 题解

    这题比较难搞.考虑设计状态:\(f_{i,j}\) 表示当前考虑到 \(X_i\) 位,且 \(X\) 的后 \(j\) 位刚好与 \(A\) 列匹配时的方案数.最终答案为 \(\sum_{i=0}^ ...

  6. php 几个算法

    /** * 返回当前运行文件名 * @acces private * @return string */ private function run_filename() { $tmparr = exp ...

  7. js--class类、super和estends关键词的学习笔记

    前言 JavaScript 语言在ES6中引入了 class 这一个关键字,在学习面试的中,经常会遇到面试官问到谈一下你对 ES6 中class的认识,同时我们的代码中如何去使用这个关键字,使用这个关 ...

  8. linux命令打基础

    目录 一.shell概述 二.linux命令分类 三.linux命令行 3.1 格式 3.2 编辑Linux命令行 四.Linux基础命令 4.1 pwd:查看当前的工作目录 4.2 cd:切换工作目 ...

  9. 重定向">" 及">>"符号的简单运用

    1.command > file 将输出重定向(可理解写入)到 file. 2.command >> file  将输出以追加的方式重定向到 file,也就是写入到 file的尾端. ...

  10. [SQL]数据更新

    插入数据 插入单个元组 一般格式: insert into <表名> [<列名1>, <列明2>, --] -- 指出在表中新插入的值的列, values(< ...