找到3所有倍数低于1000的总和或5

编程入门 行业动态 更新时间:2024-10-27 04:34:19
本文介绍了找到3所有倍数低于1000的总和或5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我们列出所有低于10是3或5的倍数的自然数,我们得到3,5,6和9这些倍数的总和是23。我有以下的code,但答案不匹配。

#包括LT&;&stdio.h中GT;诠释的main(){    长期无符号整型我,总和= 0;    clrscr();    对于(i = 0; I< = 1000;我++)    {        如果((I%5 == 0)||(I%3 == 0))        {            总和=总和+ 1;        }    }    的printf(%d个\\ N,总和);    的getchar();    返回0;}

解决方案

两件事情:

  • 你的包括的1000环,和
  • 你每次加一的总和,而不是值本身。

循环改为

为(i = 0; I< 1000;我++)

和之行

总和=总和+ I;

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. I have the following code but the answer does not match.

#include<stdio.h> int main() { long unsigned int i,sum=0; clrscr(); for(i=0;i<=1000;i++) { if((i%5==0)||(i%3==0)) { sum=sum+1; } } printf("%d\n",sum); getchar(); return 0; }

解决方案

Two things:

  • you're including 1000 in the loop, and
  • you're adding one to the sum each time, rather than the value itself.

Change the loop to

for(i=0;i<1000;i++)

And the sum line to

sum=sum+i;

更多推荐

找到3所有倍数低于1000的总和或5

本文发布于:2023-11-29 19:23:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:倍数   总和

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!