gpt4 book ai didi

dec 中的循环变换 (C)

转载 作者:太空宇宙 更新时间:2023-11-04 03:52:03 27 4
gpt4 key购买 nike

我必须编写一个程序来读取 2 个数字中不带 0 的整数,并说明第二个是否由第一个的循环变换构成。例如:4123,3412,2341 和 1234 是由 1234 循环变换而成。现在这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
int last_digit, a, temp, i, b;
int digits = 0, digits1 = 0;

printf("Enter two numbers to test if the second is a circular\n");
printf("transformation of the first.");
printf("\n\n**NOTE**\nThe numbers cannon have 0 in their digits.\n\na= ");
scanf("%d", &a);
while (a <= 0)
{
printf("Number must be greater than 0.\na= ");
scanf("%d", &a);
}
temp = a;
while (temp!=0)
{
digits1++;
temp/=10;
}
printf("b= ");
scanf("%d", &b);
while (b <= 0)
{
printf("Number must be greater than 0.\nb= ");
scanf("%d", &b);
}
while (digits != digits1)
{
digits = 0;
temp = b;
while (temp!=0)
{
digits++;
temp/=10;
}
if (digits != digits1)
{
printf("The two numbers must have the same number of digits.\nb= ");
scanf("%d", &b);
}
}
temp = b;
digits--;
for (i=1;i<=digits1;i++)
{
if (temp == a)
{
printf("%d is a circular transformation of %d",b,a);
return 0;
}
printf("\ntemp = %d",temp);
last_digit = temp % 10;
temp = temp/10;
printf("\n%d\n%d",last_digit,temp);
temp = temp + (last_digit*(pow(10,digits)));
printf("\ntemp = %d\n",temp);
}
printf("%d is not a circular transformation of %d",b,a);
return 0;
}

在您说任何话之前,我必须在不使用表格和其他东西的情况下做这件事……只是基础知识。现在我的问题是

temp = temp + (last_digit*(pow(10,digits)));

没有正常工作。如果数字超过 2 位,它会给出它应该给出的结果 - 1。

我该怎么做才能解决这个问题?跟编译器有关系吗?我在代码块中使用 GCC。

如果我愿意

temp = temp + (last_digit*(pow(10,digits))) + 1;

它适用于超过 2 位数字的数字,而不适用于 2 位数字的数字。

最佳答案

#include <stdio.h>

typedef long Value;

#define SCN_Value "ld"
#define PRI_Value "ld"

static int num_digits(Value number)
{
Value n0 = number;
int r = 0;
int ndigits = 0;
while (number != 0)
{
ndigits++;
if (number % 10 == 0 && r++ == 0)
fprintf(stderr, "Number %" PRI_Value " should not have any zero digits\n", n0);
number /= 10;
}
return ndigits++;
}

static Value prompt_for(char const *tag)
{
Value number = -1;
while (printf("%s = ", tag) > 0 &&
scanf("%" SCN_Value, &number) == 1 &&
number <= 0)
{
printf("Number (%" PRI_Value ") must be greater than 0.\n", number);
}
return number;
}

int main(void)
{
Value num1, num2;

printf("Enter two numbers to test if the second is a circular\n");
printf("transformation of the first.\n");
printf("**NOTE**\nThe numbers cannot have 0 as one of their digits.\n");

if ((num1 = prompt_for("a")) < 0)
return 1;
int digits1 = num_digits(num1);

if ((num2 = prompt_for("b")) < 0)
return 1;

while (digits1 != num_digits(num2))
{
printf("The two numbers must have the same number of digits.\n");
if ((num2 = prompt_for("b")) < 0)
return 1;
}

Value pow_10 = 1;
for (int i = 1; i < digits1; i++)
pow_10 *= 10;

Value temp = num2;
for (int i = 1; i <= digits1; i++)
{
if (temp == num1)
{
printf("%" PRI_Value " is a circular transformation of %" PRI_Value "\n", num2, num1);
return 0;
}
int last_digit = temp % 10;
temp /= 10;
temp = temp + last_digit * pow_10;
printf("rotation = %" PRI_Value "\n", temp);
}
printf("%" PRI_Value " is not a circular transformation of %" PRI_Value "\n", num2, num1);
return 0;
}

关于dec 中的循环变换 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19940969/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com