Project Euler:Problem 43 Sub-string divisibility
2015-06-05 10:24
585 查看
The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility
property.
Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:
d2d3d4=406 is divisible by 2
d3d4d5=063 is divisible by 3
d4d5d6=635 is divisible by 5
d5d6d7=357 is divisible by 7
d6d7d8=572 is divisible by 11
d7d8d9=728 is divisible by 13
d8d9d10=289 is divisible by 17
Find the sum of all 0 to 9 pandigital numbers with this property.
property.
Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:
d2d3d4=406 is divisible by 2
d3d4d5=063 is divisible by 3
d4d5d6=635 is divisible by 5
d5d6d7=357 is divisible by 7
d6d7d8=572 is divisible by 11
d7d8d9=728 is divisible by 13
d8d9d10=289 is divisible by 17
Find the sum of all 0 to 9 pandigital numbers with this property.
#include <iostream> #include <iostream> #include <string> using namespace std; unsigned long long res = 0; int prime[7] = { 2, 3, 5, 7, 11, 13, 17 }; bool divis(string s) { for (int i = 1; i <= 7; i++) { int d = (s[i] - '0') * 100 + (s[i + 1] - '0') * 10 + (s[i + 2] - '0'); if (d%prime[i - 1] != 0) return false; } return true; } void perm(int list[], int n, int k) { int temp1, temp2; if (n == 1) { if (list[k] != 0) { string s = ""; unsigned long long sum = 0; for (int i = 1; i <= k; i++) { char a = list[i] + '0'; s = s + a; sum = sum * 10 + list[i]; } if (divis(s)) res += sum; } } else for (int i = 1; i <= n; i++) { temp1 = list[i]; list[i] = list ; list = temp1; perm(list, n - 1, k); temp2 = list[i]; list[i] = list ; list = temp2; } } int main() { int list[11]; for (int i = 1; i <= 10; i++) list[i] = i-1; perm(list, 10, 10); cout << res << endl; system("pause"); return 0; }
相关文章推荐
- PCA原理及实现
- C#的StringBuilder 以及string字符串拼接的效率对照
- iOS开发 判断扫码是否为有效网址
- php 汉字转拼音 [包含20902个基本汉字+5059生僻字]
- 关于activiti驳回等功能的封装
- 欢迎使用CSDN-markdown编辑器
- Java for LeetCode 151 Reverse Words in a String
- 用 node 实现仿微信风格的 token 获取接口
- javascript eval和JSON之间的联系
- Forward与Redirect的区别
- SpringMVC学习系列(5) 之 数据绑定-2
- 屏幕适配
- 写c++代码时,一定要注意初始化!
- 在Fragment中实现百度地图,定位到当前位置(基于SDKv2.1.0)
- VBoxGuestAdditions.iso下载地址
- 使用 CSS 显示 XML
- C#调用SQLite的方法实例分析
- 钱、资源和人都不缺的土豪万达,电商业务三年内再次受挫
- spring MVC中定义异常页面
- SpringMVC学习系列(6) 之 数据验证