我必须创建一个程序,在其数字中读取2个不带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位数的数字。
答案 0 :(得分: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;
}