简单的C问题 [英] simple C problem

查看:87
本文介绍了简单的C问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不开始学习C的,我做一个项目的一部分。我已经开始做了欧拉在它的问题,我有与第一个麻烦。我必须找到低于1000的3或5的倍数的总和可能有人请帮助我。谢谢你。

 #包括LT&;&stdio.h中GT;
INT开始;
INT总和;诠释主(){
    同时(开始< 1001){
        如果(起始%3 == 0){
            总和=总和+启动;
            启动+ = 1;
        }其他{
            启动+ = 1;
        }        如果(启动%5 == 0){
            总和=总和+启动;
            启动+ = 1;
        }其他{
            启动+ = 1;
        }
        的printf(%d个\\ N,总和);
    }
    返回(0);
}


解决方案

您已经得到了一些伟大的答案至今,主要建议是这样的:

 的#include<&stdio.h中GT;
INT主(INT ARGC,CHAR *的argv [])
{
  INT I;
  INT SOLN = 0;
  对于(i = 1; I< 1000;我++)
  {
    如果((I%3 == 0)||(I%5 == 0))
    {
      SOLN + =我;
    }
  }
  的printf(%d个\\ N,溶液);
  返回0;
}

所以我要采取不同的策略。我知道你这样做是为了学习C,所以这可能是一个有点切线。

说真的,你让电脑工作太辛苦了这个:)。如果我们想通一些事情提前出局,它可以使任务更容易。

嘛,怎么的3个多倍数小于1000?还有一个每个3进入1000时间 - 1


  

MULT 3 = lfloor; (1000 - 1)/ 3 rfloor; = 333


(在⌊和&rfloor,意味着这是的地板的分工,或在编程方面,的整数的分工,其中剩余部分被丢弃)。

和5好几倍怎么都小于1000?


  

MULT 5 = lfloor; (1000 - 1)/ 5⌋ = 199


现在是什么3所有倍数小于1000的总和?


  

总和<子> 3 = 3 + 6 + 9 + ... 996 + 999 = 3&倍;(1 + 2 + 3 + ... + 332 + 333)= 3&倍;&总和; <子> i = 1到MULT 3 I


和5所有的倍数小于1000的总和?


  

总和<子> 5 = 5 + 10 + 15 + ... + 990 + 995 = 5&倍;(1 + 2 + 3 + ... + 198 + 199)= 5&倍;&总和; <子> i = 1到MULT 5 I


3的倍数一些也都是5的倍数的那些都是15的倍数。
由于这些努力MULT计数 3 和MULT 5 (因此总结 3 及和 5 ),我们需要知道MULT 15 及和 15 ,以避免计数他们两次。


  

MULT <子> 15 = lfloor; (1000 - 1)/ 15 rfloor; = 66


  
  

总和<子> 15 = 15 + 30 + 45 + ... + 975 + 990 = 15&倍;(1 + 2 + 3 + ... + 65 + 66)= 15&倍;&总和; <子> i = 1到MULT 15 I


因此​​,解决问题的方法发现的3所有倍数低于1000的总和或5 然后


  

SOLN = SUM 3 +总和 5 - 总和 15


所以,如果我们想,我们可以实现这个直接:

 的#include&LT;&stdio.h中GT;
INT主(INT ARGC,CHAR *的argv [])
{
  INT I;
  INT常量mult3 =(1000 - 1)/ 3;
  INT常量mult5 =(1000 - 1)/ 5;
  INT常量mult15 =(1000 - 1)/ 15;
  INT sum3 = 0;
  INT sum5 = 0;
  INT sum15 = 0;
  INT SOLN;  对于(i = 1; I&LT; = mult3;我++){sum3 + = 3 * I; }
  对于(i = 1; I&LT; = mult5;我++){sum5 + = 5 * I; }
  对于(i = 1; I&LT; = mult15;我++){sum15 + = 15 *我; }  SOLN = sum3 + sum5 - sum15;
  的printf(%d个\\ N,溶液);
  返回0;
}

但是,我们可以做的更好。计算各个求和,我们有高斯的身份的从1到n的总和(又名&总和表示; <子> I = 1到n ⅰ)为n&倍;第(n + 1)/ 2,所以


  

3 = 3倍; MULT 3 &倍;(多重 3 + 1)/ 2


  
  

5 = 5倍; MULT 5 &倍;(多重 5 + 1)/ 2


  
  

之<子> 15 = 15倍; MULT 15 &倍;(多重 15 + 1)/ 2


(注意,我们可以使用正常的部门或整数除法在这里 - 它,因为n的一个或无关紧要的n + 1必须能被2整除)

现在,这是一种整齐,因为这意味着我们可以找到解决方案,而无需使用一个循环:

 的#include&LT;&stdio.h中GT;
INT主(INT ARGC,CHAR *的argv [])
{
  INT常量mult3 =(1000 - 1)/ 3;
  INT常量mult5 =(1000 - 1)/ 5;
  INT常量mult15 =(1000 - 1)/ 15;
  INT常量sum3 =(3 * mult3 *(mult3 + 1))/ 2;
  INT常量sum5 =(5 * mult5 *(mult5 + 1))/ 2;
  INT常量sum15 =(15 * mult15 *(mult15 + 1))/ 2;  INT常量SOLN = sum3 + sum5 - sum15;
  的printf(%d个\\ N,溶液);
  返回0;
}

当然,由于我们已经走了这么远,我们可以通过手摇了整个事情:


  

总和<子> 3 = 3&倍; 333&倍;(333 + 1)/ 2 = 999&倍334/2 = 999&倍; 117 = 117000 - 117 = 116883


  
  

总和<子> 5 = 5&倍; 199&倍;(199 + 1)/ 2 = 995&倍200/2 = 995&倍; 100 = 99500


  
  

总和<子> 15 = 15&倍; 66&倍;(66 + 1)/ 2 = 990倍67/2 = 495&倍; 67 = 33165


  
  

SOLN = 116883 + 99500 - 33165 = 233168


和写一个更简单的程序:

 的#include&LT;&stdio.h中GT;
INT主(INT ARGC,CHAR *的argv [])
{
  的printf(233168 \\ n);
  返回0;
}

I have had to start to learning C as part of a project that I am doing. I have started doing the 'euler' problems in it and am having trouble with the first one. I have to find the sum of all multiples of 3 or 5 below 1000. Could someone please help me. Thanks.

#include<stdio.h>
int start;
int sum;

int main() {
    while (start < 1001) {
        if (start % 3 == 0) {
            sum = sum + start;
            start += 1;
        } else {
            start += 1;
        }

        if (start % 5 == 0) {
            sum = sum + start;
            start += 1;
        } else {
            start += 1;
        }
        printf("%d\n", sum);
    }
    return(0);
}

解决方案

You've gotten some great answers so far, mainly suggesting something like:

#include <stdio.h>
int main(int argc, char * argv[])
{
  int i;
  int soln = 0;
  for (i = 1; i < 1000; i++)
  {
    if ((i % 3 == 0) || (i % 5 == 0))
    {
      soln += i;
    }
  }
  printf("%d\n", soln);
  return 0;
}

So I'm going to take a different tack. I know you're doing this to learn C, so this may be a bit of a tangent.

Really, you're making the computer work too hard for this :). If we figured some things out ahead of time, it could make the task easier.

Well, how many multiples of 3 are less than 1000? There's one for each time that 3 goes into 1000 - 1.

mult3 = ⌊ (1000 - 1) / 3 ⌋ = 333

(the ⌊ and ⌋ mean that this is floor division, or, in programming terms, integer division, where the remainder is dropped).

And how many multiples of 5 are less than 1000?

mult5 = ⌊ (1000 - 1) / 5 ⌋ = 199

Now what is the sum of all the multiples of 3 less than 1000?

sum3 = 3 + 6 + 9 + ... + 996 + 999 = 3×(1 + 2 + 3 + ... + 332 + 333) = 3×∑i=1 to mult3 i

And the sum of all the multiples of 5 less than 1000?

sum5 = 5 + 10 + 15 + ... + 990 + 995 = 5×(1 + 2 + 3 + ... + 198 + 199) = 5×∑i = 1 to mult5 i

Some multiples of 3 are also multiples of 5. Those are the multiples of 15. Since those count towards mult3 and mult5 (and therefore sum3 and sum5) we need to know mult15 and sum15 to avoid counting them twice.

mult15 = ⌊ (1000 - 1) /15 ⌋ = 66

sum15 = 15 + 30 + 45 + ... + 975 + 990 = 15×(1 + 2 + 3 + ... + 65 + 66) = 15×∑i = 1 to mult15 i

So the solution to the problem "find the sum of all the multiples of 3 or 5 below 1000" is then

soln = sum3 + sum5 - sum15

So, if we wanted to, we could implement this directly:

#include <stdio.h>
int main(int argc, char * argv[])
{
  int i;
  int const mult3 = (1000 - 1) / 3;
  int const mult5 = (1000 - 1) / 5;
  int const mult15 = (1000 - 1) / 15;
  int sum3 = 0;
  int sum5 = 0;
  int sum15 = 0;
  int soln;

  for (i = 1; i <= mult3; i++) { sum3 += 3*i; }
  for (i = 1; i <= mult5; i++) { sum5 += 5*i; }
  for (i = 1; i <= mult15; i++) { sum15 += 15*i; }

  soln = sum3 + sum5 - sum15;
  printf("%d\n", soln);
  return 0;
}

But we can do better. For calculating individual sums, we have Gauss's identity which says the sum from 1 to n (aka ∑i = 1 to n i) is n×(n+1)/2, so:

sum3 = 3×mult3×(mult3+1) / 2

sum5 = 5×mult5×(mult5+1) / 2

sum15 = 15×mult15×(mult15+1) / 2

(Note that we can use normal division or integer division here - it doesn't matter since one of n or n+1 must be divisible by 2)

Now this is kind of neat, since it means we can find the solution without using a loop:

#include <stdio.h>
int main(int argc, char *argv[])
{
  int const mult3 = (1000 - 1) / 3;
  int const mult5 = (1000 - 1) / 5;
  int const mult15 = (1000 - 1) / 15;
  int const sum3 = (3 * mult3 * (mult3 + 1)) / 2;
  int const sum5 = (5 * mult5 * (mult5 + 1)) / 2;
  int const sum15 = (15 * mult15 * (mult15 + 1)) / 2;

  int const soln = sum3 + sum5 - sum15;
  printf("%d\n", soln);
  return 0;
}

Of course, since we've gone this far we could crank out the entire thing by hand:

sum3 = 3×333×(333+1) / 2 = 999×334 / 2 = 999×117 = 117000 - 117 = 116883

sum5 = 5×199×(199+1) / 2 = 995×200 / 2 = 995×100 = 99500

sum15 = 15×66×(66+1) / 2 = 990×67 / 2 = 495 × 67 = 33165

soln = 116883 + 99500 - 33165 = 233168

And write a much simpler program:

#include <stdio.h>
int main(int argc, char *argv[])
{
  printf("233168\n");
  return 0;
}

这篇关于简单的C问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆