ユーザに1つの数字を入力するように要求するCコードを作成する必要があり、そのコードがその数字が「Cullen's number」であるかどうかをチェックします。「Cullen's number」を見つける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;
}
初期化されていない変数があるため、未定義の動作*があります。初期化されていないローカル変数は不確定*値を持ちます。 –
あなたのループは '2^n'を計算するはずです。 'brojP'をループのたびに同じものに設定するので、' brojP = 2 * potency; 'を実行するのと同じです。 – Barmar