1

我必须编写一个程序来读取数字中没有 0 的 2 个整数,并说明第二个整数是否由第一个整数的循环变换组成。例如: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 位的数字。

4

1 回答 1

1
#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;
}
于 2013-11-13T21:07:45.220 回答