杭电oj1313(Round and Round We Go)
杭电oj1313
Problem Description
A cyclic number is an integer n digits in length which, when multiplied by any integer from 1 to n, yields a {!0}cycle{!1} of the digits of the original number. That is, if you consider the number after the last digit to {!0}wrap around{!1} back to the first digit, the sequence of digits in both numbers will be the same, though they may start at different positions.
For example, the number 142857 is cyclic, as illustrated by the following table:
142857*1=142857
142857*2=285714
142857*3=428571
142857*4=571428
142857*5=714285
142857*6=857142
Write a program which will determine whether or not numbers are cyclic. The input file is a list of integers from 2 to 60 digits in length. (Note that preceding zeros should not be removed, they are considered part of the number and count in determining n. Thus, {!0}01{!1} is a two-digit number, distinct from {!0}1{!1} which is a one-digit number.)
Output
For each input integer, write a line in the output indicating whether or not it is cyclic.
Sample Input
142857
142856
142858
01
0588235294117647
Sample Output
142857 is cyclic
142856 is not cyclic
142858 is not cyclic
01 is not cyclic
0588235294117647 is cyclic
答案
利用数组模拟大数的乘法运算,对每次相乘的结果进行匹配分析
注意不要忽略开头的0,乘法的结果如果进位了要截去高位再进行匹配分析
AC代码:
#include <string.h>
#include <ctype.h>
#include <iostream>
using namespace std;
void converse(int *, int);
void multiply(int *, int, int, int *);
void multiply(int *, int, int *, int, int *, int &);
void multiply_10(int *, int &);
void add(int *, int, int *, int, int *, int &);
bool match(int *, int *, int);
int main() {
/*char str_1[100], str_2[100];
int num_1[100], num_2[100], ans[100];
int len_1, len_2, len_ans;
int num;
while (cin >> str_1 >> num) {
len_1 = strlen(str_1);
for (int i = 0; i < len_1; i++)
num_1[i] = str_1[i] - '0';
multiply(num_1, len_1, num, ans);
for (int i = 0; i < len_1; i++)
cout << ans[i];
cout << endl;
}*/
char str[100];
int num_1[100], num_2[100];
int len_1, len_2;
while (cin >> str) {
/*convert to number*/
len_1 = strlen(str);
for (int i = 0; i < len_1; i++)
num_1[i] = str[i] - '0';
/*check*/
bool flg = true;
for (int i = 1; i <= len_1; i++) {
multiply(num_1, len_1, i, num_2);
if (!match(num_1, num_2, len_1)) {
flg = false;
break;
}
}
/*output*/
switch (flg) {
case true:
printf("%s is cyclic\n", str);
break;
case false:
printf("%s is not cyclic\n", str);
break;
}
}
}
void converse(int *num, int len) {
int tmp, i, j;
for (i = 0, j = len - 1; i < j; i++, j--)
tmp = num[i], num[i] = num[j], num[j] = tmp;
}
void multiply(int *num_1, int len_1, int num, int *ans) {
int len_ans;
int num_2[100];
int len_2 = 2;
num_2[0] = num / 10;
num_2[1] = num % 10;
/*multiply the two number*/
multiply(num_1, len_1, num_2, len_2, ans, len_ans);
/*truncate the leading digit*/
converse(ans, len_ans);
len_ans = len_1;
converse(ans, len_ans);
}
void multiply(int *num_1, int len_1, int *num_2, int len_2, int *ans, int &len_ans) {
/*Compare the two numbers*/
int *large = (len_1 > len_2) ? num_1 : num_2;
int *small = (len_1 > len_2) ? num_2 : num_1;
int maxlen = (len_1 > len_2) ? len_1 : len_2;
int minlen = (len_1 > len_2) ? len_2 : len_1;
/*Converse the two numbers*/
converse(num_1, len_1);
converse(num_2, len_2);
/*Multiply correspondingly*/
//initialize the answer
ans[0] = 0;
len_ans = 1;
//do the multiplication
for (int i = 0; i < minlen; i++) {
int value, carry = 0;
int tmp[100], len_tmp = maxlen;
//calculate the tmp
for (int j = 0; j < maxlen; j++) {
value = large[j] * small[i] + carry;
tmp[j] = value % 10;
carry = value / 10;
}
if (carry > 0)
tmp[len_tmp++] = carry;
converse(tmp, len_tmp);
for (int k = 0; k < i; k++)
multiply_10(tmp, len_tmp);
// ans = ans + tmp
int copy[100], len_copy;
add(ans, len_ans, tmp, len_tmp, copy, len_copy);
len_ans = len_copy;
for (int i = 0; i < len_copy; i++)
ans[i] = copy[i];
}
/*Converse the two numbers*/
converse(num_1, len_1);
converse(num_2, len_2);
}
void multiply_10(int *num, int &len) {
len = len + 1;
num[len - 1] = 0;
}
void add(int *num_1, int len_1, int *num_2, int len_2, int *ans, int &len_ans) {
/*Compare the two numbers*/
int *large = (len_1 > len_2) ? num_1 : num_2;
int *small = (len_1 > len_2) ? num_2 : num_1;
int maxlen = (len_1 > len_2) ? len_1 : len_2;
int minlen = (len_1 > len_2) ? len_2 : len_1;
/*Converse the two numbers*/
converse(num_1, len_1);
converse(num_2, len_2);
/*Add correspondingly*/
int value, carry = 0;
len_ans = maxlen;
for (int i = 0; i <= minlen - 1; i++) {
value = large[i] + small[i] + carry;
ans[i] = value % 10;
carry = value / 10;
}
for (int i = minlen; i <= maxlen - 1; i++) {
value = large[i] + carry;
ans[i] = value % 10;
carry = value / 10;
}
if (carry > 0)
ans[len_ans++] = carry;
converse(ans, len_ans);
/*Converse the two numbers*/
converse(num_1, len_1);
converse(num_2, len_2);
}
bool match(int *num_1, int *num_2, int len) {
int i, j, k;
for (i = 0; i < len; i++) {
for (k = 0, j = i; k < len && num_1[k] == num_2[j]; k++, j = ((j + 1) % len))
;
if (k == len)
return true;
}
return false;
}