- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了以下代码:
1 #include<stdio.h>
2 #include<stdbool.h>
3
4 bool c[500];
5 void main()
6 {
7 int i,n=1,j;
8 for (i=2; i<500; i++)
9 {
10 if (!c[i])
11 {
12 printf("%d is the prime number %d\n", i,n);
13 n++;
14 j=2;
15 while (j*i<500)
16 {
17 c[j*i]=1;
18 j++;
19 }
20 }
21 }
22 }
显示500以下的质数;现在,我怎样才能让程序在短短几行内显示所有这些数字的总和(所有 500 以下的质数)?谢谢你的帮助,我是 C 新手
最佳答案
sum
设置为零。sum
中。sum
。因此,假设您的代码可以正常工作,您需要更改上面的第 1 点:
int i,n=1,j;
进入:
int i,n=1,j,sum=0;
<小时/>
第 2 点涉及更改:
printf("%d is the prime number %d\n", i,n);
进入:
printf("%d is the prime number %d\n", i,n);
sum += i;
<小时/>
最后,第 3 点可以通过以下方式制定:
printf("Sum of all those primes is %d\n", sum);
在最后的右大括号之前。
<小时/>对代码的更改(包括注释和更合适的变量名称以提高可读性)将类似于:
#include <stdio.h>
#include <stdbool.h>
// Find/sum all primes less than this number.
#define LIMIT 500
// Flag indicating a number is non-prime, initialised to zeros.
bool isComposite[LIMIT];
void main (void) {
int num, mult;
int count = 1, sum = 0;
// Check every number for primeness.
for (num = 2; num < LIMIT; num++) {
// Ignore if composite.
if (!isComposite[num]) {
// Print prime, add to sum.
printf ("%d is the prime number %d\n", num, count++);
sum += num;
// Mark all multiples of it as composite.
for (mult = num * 2; mult < LIMIT; mult += num) {
isComposite[mult] = 1;
}
}
}
// Now just output the sum.
printf ("The sum of those primes is %d\n", sum);
}
如果是类作业,请勿抄袭,您可能会被发现。我添加它只是为了向您展示如果您遵循一些简单的规则(其中一些规则如下),代码的可读性会提高多少:
500
等“神奇”常量。关于c - 500以下素数之和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27162419/
这个问题可能类似于In Angular2 *ngFor iteration, how do I output only unique values from the array?但我的问题是还有更多功
我编写了一个算法来获取 float 的总和,该算法对于整数来说非常有效,但是当我应用于 float 时,我得到的总和是负数。但是我的 float 数组只有正 float 。在这里我发布我的代码,感谢您
我想将这个简单的 for 循环转换为并行循环。它遍历字符串数组(从文本文件读取的 float )并计算总和。 for (int i = 0; i { float tmp; if (f
我正在尝试总结日期差异,一切都很好,除了如果有相同日期我想添加 1,例如,如果起始日期是:01/01/2003到目前为止是 01/01/2003 那么我想添加 1 天,但它没有添加 1 天,而是仅在
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Is JavaScript’s Floating-Point Math Broken? 这将是一个非常基本的计算机科
我刚接触sql,卡住了。我正在尝试计算每个用户走过的(每年)距离总和。我有一个具有以下结构的表(我们称之为 dist_table): rowid user_name date
我刚接触sql,卡住了。我正在尝试计算每个用户走过的(每年)距离总和。我有一个具有以下结构的表(我们称之为 dist_table): rowid user_name date
给定一个正数数组。我想将数组拆分为 2 个不同的子集,以使它们的 gcd(最大公约数)之和最大。 示例数组:{6,7,6,7}。 答案:需要的两个子集是:{6,6}和{7,7};它们各自的 gcd(s
我想在我的数组中求和:
我想将下面的字符串拆分为字母和数字,然后我需要计算数字的总和。我的示例问题是 a[20]={"abcd123dc2"}; 预期输出: abcddc 8 我的代码: int main() { c
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
我是一名优秀的程序员,十分优秀!