歐拉計劃16-20題

1六、Power digit sumphp

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

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

題目大意:數組

215 = 32768 而且其各位之和爲 is 3 + 2 + 7 + 6 + 8 = 26.less

21000 的各位數之和是多少?ide

#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;
}
View Code
Answer:1366C
ompleted on Sun, 17 Nov 2013, 15:23

1七、Number letter countsui

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.this

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

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..net

題目大意:

若是用英文寫出數字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;
}
View Code
Answer:21124
Completed on Sun, 17 Nov 2013, 16:30

1八、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

  7   

 2  4  6

8  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;
}
View Code

Answer:1074

Completed on Thu, 1 May 2014, 16:31


1九、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;
}
View Code

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;
}
View Code
Answer:648
Completed on Sun, 13 Apr 2014, 02:47
相關文章
相關標籤/搜索