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

欧拉计划16-20题

时间:2014-05-02 13:17:49      阅读:1223      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

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 的各位数之和是多少?

bubuko.com,布布扣
bubuko.com,布布扣
#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
bubuko.com,布布扣
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" 的使用与英国标准一致。

bubuko.com,布布扣
bubuko.com,布布扣
#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
bubuko.com,布布扣
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

  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:

bubuko.com,布布扣

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)

题目大意:

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

bubuko.com,布布扣
bubuko.com,布布扣
#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
bubuko.com,布布扣

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日)一共有多少个星期日落在了当月的第一天?

bubuko.com,布布扣
bubuko.com,布布扣
#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
bubuko.com,布布扣

Answer:171

Completed on Mon, 18 Nov 2013, 03:38


20、Factorial digit sum

n! means n bubuko.com,布布扣 (n bubuko.com,布布扣 1) bubuko.com,布布扣 ... bubuko.com,布布扣 3 bubuko.com,布布扣 2 bubuko.com,布布扣 1

For example, 10! = 10 bubuko.com,布布扣 9 bubuko.com,布布扣 ... bubuko.com,布布扣 3 bubuko.com,布布扣 2 bubuko.com,布布扣 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 bubuko.com,布布扣 (n bubuko.com,布布扣 1) bubuko.com,布布扣 ... bubuko.com,布布扣 3 bubuko.com,布布扣 2 bubuko.com,布布扣 1

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

算出100!的各位之和。

bubuko.com,布布扣
bubuko.com,布布扣
#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
bubuko.com,布布扣
Answer:648
Completed on Sun, 13 Apr 2014, 02:47

 

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

欧拉计划16-20题

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/archimedes/p/3703509.html

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