欧拉计划16-20题

16、Power
digit sum

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 =
26.

What is the sum of the digits of the number 21000?

题目大意:

215 = 32768 并且其各位之和为 is 3 + 2 + 7 + 6 + 8 = 26.

21000 的各位数之和是多少?


#include <stdio.h>
#include <stdbool.h>

void solve(void)
{
int a[100000] = {0};
int n, sum, i, j;
n = sum = 0;
a[0] = 1;
for(i = 0; i < 1000; i++) { //以1000进制的方法存储
for(j = 0; j <= n; j++) {
a[j] *= 2;
}
for(j = 0; j <= n; j++) {
if(a[j] >= 10000) {
a[j] %= 10000;
a[j+1]++;
n++;
}
}
}
for(i = 0; i <= n; i++) {
sum += a[i] / 10000;
a[i] %= 10000;
sum += a[i] / 1000;
a[i] %= 1000;
sum += a[i] / 100;
a[i] %= 100;
sum += a[i] / 10;
a[i] %= 10;
sum += a[i];

}
printf("%d\n",sum);
}

int main(void)
{
solve();
return 0;
}

Answer:1366C

ompleted
on Sun, 17 Nov 2013, 15:23


17、Number
letter counts

If the numbers 1 to 5 are written out in words: one, two, three, four, five,
then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out
in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred
and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20
letters. The use of "and" when writing out numbers is in compliance with British
usage.

题目大意:

如果用英文写出数字1到5: one, two, three, four, five, 那么一共需要3 + 3 + 5 + 4 + 4 =
19个字母。

如果数字1到1000(包含1000)用英文写出,那么一共需要多少个字母?

注意: 空格和连字符不算在内。例如,342 (three hundred and forty-two)包含23个字母; 115 (one
hundred and fifteen)包含20个字母。"and" 的使用与英国标准一致。


#include <stdio.h>
#include <stdbool.h>

int a[101] = {0,3,3,5,4,4,3,5,5,4,3,6,6,8,8,7,7,9,8,8};

void init(void) //初始化数组
{
a[20] = 6;
a[30] = 6;
a[40] = 5;
a[50] = 5;
a[60] = 5;
a[70] = 7;
a[80] = 6;
a[90] = 6;
a[100] = 7;
}

int within100(void) //计算1~99所含字母的和
{
int i, sum, t;
t = sum = 0;
for(i = 1; i <= 9; i++) t += a[i];
for(i = 1; i <= 19; i++) sum += a[i];
for(i = 2; i <= 9; i++) {
sum += a[i*10] * 10;
sum += t;
}
return sum;
}

void solve(void)
{
int i;
int sum, t;
sum = t = within100();
for(i = 1; i < 10; i++) {
sum += (a[i] + 10) * 99 + (a[i] + 7) + t;
}
sum += 11;

printf("%d\n",sum);
}

int main(void)
{
init();
solve();
return 0;
}

Answer:21124

Completed
on Sun, 17 Nov 2013, 16:30


18、Maximum
path sum I

By
starting at the top of the triangle below and moving to adjacent numbers on the
row below, the maximum total from top to bottom is 23.

3


 4

2  4  6


5  9  3

That
is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below:

NOTE: As there are only 16384 routes, it is possible to solve this
problem by trying every route. However, Problem
67
, is the same challenge with a triangle containing one-hundred rows; it
cannot be solved by brute force, and requires a clever method! ;o)

题目大意:

找出从以下三角形的顶端走到底端的最大总和:


#include<stdio.h>

#define N 15
int main()
{
char t[5];
int s[N][N]={0};
FILE *f;
int i,j;
f = fopen("18.txt","r");
for (i = 0; i < N; i++) {
for (j = 0; j <= i; j++) {
fgets(t,4,f);
s[i][j] =atoi(t);
}
}
fclose(f);
for ( i = N-2; i >=0; i--) {
for ( j = 0; j <= i; j++) {
if (s[i+1][j] > s[i+1][j+1]) {
s[i][j]+=s[i+1][j];
} else {
s[i][j]+=s[i+1][j+1];
}
}
}
printf("answer: %d\n",s[0][0]);
return 0;
}


Answer:1074

Completed on Thu, 1 May 2014, 16:31


19、Counting
Sundays

You are given the following information, but you may prefer to do some
research for yourself.

  • 1 Jan 1900 was a Monday.

  • Thirty days has September, April, June and November. All the rest have
    thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And
    on leap years, twenty-nine.

  • A leap year occurs on any year evenly divisible by 4, but not on a century
    unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century
(1 Jan 1901 to 31 Dec 2000)?

题目大意:

以下是一些已知信息,但是或许你需要自己做一些其他的调查。

  • 1900年1月1日是星期一。

  • 30天的月份有:9月,4月,6月,11月。

  • 此外的月份都是31天,当然2月除外。

  • 2月在闰年有29天,其他时候有28天。

  • 年份可以被4整除的时候是闰年,但是不能被400整除的世纪年(100的整数倍年)除外。

20世纪(1901年1月1日到2000年12月31日)一共有多少个星期日落在了当月的第一天?


#include <stdio.h>
#include <stdbool.h>

const int a[2][12] = {{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};

bool leapYear(int n) //判断闰年
{
return (((n % 4 ==0) && (n % 100 !=0)) || (n % 400 == 0));
}

bool issunday(int n) //判断某天是否是星期天
{
return (n % 7 == 0 ? true : false);
}

void solve(void)
{
int num, i, j, count;
count = 0;

i = 1901;
num = 1;
while(i < 2000) {

int t = (leapYear(i) ? 1 : 0); //判断闰年
for(j = 0; j < 12; j++) {
num += a[t][j];
if(issunday(num)) count++;
}
i++;
}
printf("%d\n",count);
}

int main(void)
{
solve();
return 0;
}

Answer:171

Completed on Mon, 18 Nov 2013, 03:38


20、Factorial
digit sum

n! means n  (n  1)  ...  3  2  1

For example, 10! = 10  9  ...  3  2  1 = 3628800, and the sum of the digits in the number
10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!

题目大意:

n! = n  (n  1)  ...  3  2  1

例如, 10! = 10  9  ...  3  2  1 = 3628800, 那么10!的各位之和就是3 + 6 + 2 + 8 + 8 + 0 + 0 =
27.

算出100!的各位之和。


#include <stdio.h>
#include <math.h>

#define N 100

int main(void){
int n=N*log(N/3),a[n],ca=0,i,j;
for(i = 0; i < n; i++)
a[i] = 0;
a[n-1] = 1;
for(i = 1; i <= N; i++){
for(j = n - 1; j >= 0; j--){
ca = i * a[j] + ca;
a[j] = ca % 10;
ca /= 10;
}
ca = 0;
}
ca = 0;
for(i = 0; i < n; i++)
ca += a[i];
printf("%d\n",ca);
return 0;
}

Answer:648

Completed
on Sun, 13 Apr 2014, 02:47

欧拉计划16-20题,布布扣,bubuko.com

时间: 2024-08-14 14:57:31

欧拉计划16-20题的相关文章

欧拉计划&#183;第十一题

题目11:在20×20的网格中同一直线上四个数的最大乘积是多少? 在以下这个20*20的网格中,四个处于同一对角线上的相邻数字用红色标了出来: 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 6

欧拉计划&#183;第五题

题目5:找出最小的能被1-20中每个数整除的数. 2520是最小的能被1-10中每个数字整除的正整数. 最小的能被1-20中每个数整除的正整数是多少? 源码 STDMETHODIMP COuLa::Test5(int number) { // TODO: 在此添加实现代码 int iForNumber[MAX_PATH] = {0}; int iForFinalNumber[MAX_PATH] = {0}; for(int i = number; i>0; i--) { int c = i; i

欧拉计划第9题题解

Special Pythagorean triplet A Pythagorean triplet is a set of three natural numbers, \(a < b < c\) , for which, \(a^2 + b^2 = c^2\) For example, \(3^2 + 4^2 = 9 + 16 = 25 = 5^2\) . There exists exactly one Pythagorean triplet for which \(a + b + c =

欧拉计划&#183;第十题

题目10:计算两百万以下所有质数的和. 10以下的质数的和是2 + 3 + 5 + 7 = 17. 找出两百万以下所有质数的和. 源码 STDMETHODIMP COuLa::Test10(int number) { // TODO: 在此添加实现代码 __int64 sum = 2; for(int i = 2; 2*i-1 <= number; i++) { int c = 2*i-1; for( int j = 2;c>10? j<=10 : j<=c;j++) { if(1

欧拉计划&#183;第七题

题目7:找出第10001个质数. 前六个质数是2,3,5,7,11和13,其中第6个是13. 第10001个质数是多少? 源码 STDMETHODIMP COuLa::Test7(int number) { // TODO: 在此添加实现代码 int iNumberForCout = 1; int iNumberForOutput = 0; int iNumberForAdd = 1; while(iNumberForCout<number) { iNumberForAdd++; iNumber

欧拉计划&#183;第八题

题目8:找出这个1000位数字中连续13个数字乘积的最大值. 找出以下这个1000位的整数中连续13个数字的最大乘积. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 1254069874715852386305071569329096329

欧拉计划&#183;第六题

题目6:平方和与和平方的差是多少? 前十个自然数的平方和是: 12 + 22 + ... + 102 = 385 前十个自然数的和的平方是: (1 + 2 + ... + 10)2 = 552 = 3025 所以平方和与和的平方的差是3025  385 = 2640. 找出前一百个自然数的平方和与和平方的差. 源码 STDMETHODIMP COuLa::Test6(int number) { // TODO: 在此添加实现代码 __int64 iSquareSumNumber = 0; __i

欧拉计划&#183;第四题

题目4:找出由两个三位数乘积构成的回文. 一个回文数指的是从左向右和从右向左读都一样的数字.最大的由两个两位数乘积构成的回文数是9009 = 91 * 99. 找出最大的有由个三位数乘积构成的回文数. 源代码 STDMETHODIMP COuLa::Test4(int iMaxNumber) { // TODO: 在此添加实现代码 int outputNumber = 0; int iMaxOutputNumber = 0; for(int i = iMaxNumber/10; i<iMaxNu

欧拉计划第3题题解

Largest prime factor The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? 最大质因数 13195的所有质因数为5.7.13和29. 600851475143最大的质因数是多少? 解题思路 分解质因数的算法是 \(O( \sqrt{n} )\) 的算法. 求一个数 \(a\) 的质因数,可以从 \(2\) 开始到

欧拉计划第10题题解

Summation of primes The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. 素数的和 所有小于10的素数的和是2 + 3 + 5 + 7 = 17. 求所有小于两百万的素数的和. 解题思路 没有特别好的想法,下奶能想到的就是枚举算出200万以内的所有素数,然后求这些素数的和. 实现代码如下: #include <bits/st