作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
需要编写一段C代码,要求用户输入一个数字,该代码将检查该数字是否是“卡伦数”。
只要能通过“2^n * n + 1”计算出来的数字就是卡伦数。
卡伦数示例:
3=2^1 * 1 + 1
9=2^2 * 2 + 1
25=2^3 * 3 + 1
这是我正在编写的代码,有什么帮助吗?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int num, brojP, potency = 0, numRepeats = 0, endResult=0, isCullen;
printf("Unesite broj");
scanf("%d", &num);
do
{
potency = potency + 1; // initializing "potency" and at the same time making it one number larger at each repeat of the loop
do
{
brojP = 2*potency;
numRepeats = numRepeats + 1;
} while (numRepeats < potency); // this entire loop is used for "2^n" part
endResult = brojP * potency + 1; // calculate the "2^n * n + 1"
numRepeats = 0;
if (endResult == num)
{
isCullen = 1;
break;
}
} while (endResult < num);
if (isCullen == 1)
printf("Number inputted is Cullen's number\n");
else
printf("Number inputted isn't Cullen't number\n");
return 0;
}
最佳答案
这个循环是错误的:
do
{
brojP = 2*potency;
numRepeats = numRepeats + 1;
} while (numRepeats < potency); // this entire loop is used for "2^n" part
您每次都需要将上一次迭代的结果乘以 2,但实际上是将效力
乘以 2。由于效力
不会改变,因此您只是一遍又一遍地执行相同的任务。这样做:
brojP = 1;
for (numRepeats = 0; numRepeats < potency; numRepeats++) {
brojP *= 2;
}
关于查找 "Cullen' 号的 C 代码”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36344110/
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有多个表,但对于此查询只需要两个表,即 Faculty 和 CourseSection。我尝试了以下但失败了。如果有人可以提供帮助。 Select faculty.firstname,faculty
我是一名优秀的程序员,十分优秀!