典型的字符串处理题
#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;
int product(char num1[], int num2, char result[])
{
string number(num1);
int n = strlen(num1);
reverse(number.begin(), number.end());
int off = 0;
for (int i = 0; i < n; i++)
{
int temp = (number[i] - '0') * num2 + off;
number[i] = '0' + temp % 10;
off = temp / 10;
}
if (off > 0)
{
char c = '0' + off;
number = number + c;
}
reverse(number.begin(), number.end());
strcpy(result, number.c_str());
// printf("%s:%s\n", num1, result);
return strlen(result);
}
char* offset(char *str)
{
int n = strlen(str);
if (n > 0)
{
char temp = str[0];
for (int i = 0; i < n - 1; i++)
{
str[i] = str[i + 1];
}
str[n - 1] = temp;
}
return str;
}
bool isCyclic(char num1[], char num2[])
{
bool ret = false;
int n1 = strlen(num1);
int n2 = strlen(num2);
if (n1 == n2)
{
for (int i = 0; i < n1; i++)
{
if (strcmp(num1, offset(num2)) == 0)
{
ret = true;
break;
}
}
}
return ret;
}
int main()
{
char number[61];
char result[62] = "";
while(gets(number) != NULL)
{
int n = strlen(number);
bool ret = true;
for (int i = 1; i <= n; i++)
{
result[0] = '\0';
int n2 = product(number, i, result);
if (isCyclic(number, result) == false)
{
ret = false;
break;
}
}
printf("%s is ", number);
if (ret == true)
{
printf("cyclic\n");
}
else
{
printf("not cyclic\n");
}
}
return 0;
}