码迷,mamicode.com
首页 > 其他好文 > 详细

Project Euler:Problem 40 Champernowne's constant

时间:2015-06-04 13:55:04      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:c++   project euler   

An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021...

It can be seen that the 12th digit of the fractional part is 1.

If dn represents the nth digit of the fractional part, find the value of the following expression.

d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000



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

string int_str(int a)
{
	string res = "";
	while (a)
	{
		char s = a % 10 + '0';
		res = s + res;
		a /= 10;
	}
	return res;
}

int main()
{
	string s = "";
	for (int i = 1; i <= 1000000; i++)
	{
		s = s + int_str(i);
	}
	int ans = 1;
	ans = (s[1 - 1] - '0')*(s[10 - 1] - '0')*(s[100 - 1] - '0')*(s[1000 - 1] - '0')*(s[10000 - 1] - '0')*(s[100000 - 1] - '0')*(s[1000000 - 1] - '0');
	cout << ans << endl;
	system("pause");
	return 0;
}


Project Euler:Problem 40 Champernowne's constant

标签:c++   project euler   

原文地址:http://blog.csdn.net/youb11/article/details/46358267

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!