Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10514 | Accepted: 4812 |
Description
Input
Output
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
#include <cstdio> #include <cstring> #include <cstdlib> using namespace std; const int N = 65; char str[N]; int num[N],ans[N],len; int cmp(const void *a,const void *b) { return *(int *)a-*(int *)b; } bool is_match() { qsort(num,len,sizeof(int),cmp); qsort(ans,len,sizeof(int),cmp); int i; for(i=0;i<len;i++) if(num[i]!=ans[i]) return false; return true; } int main() { int i,j,k; memset(num,0,sizeof(num)); memset(str,0,sizeof(str)); while(~scanf("%s",str)) { len=strlen(str); j=0; memset(num,0,sizeof(num)); for(i=len-1;i>=0;i--) num[j++]=str[i]-'0'; for(i=2;i<=len;i++) { memset(ans,0,sizeof(ans)); for(j=0;j<len;j++) { ans[j] = num[j]*i; } /* for(k=0;k<len;k++) printf("%d ",ans[k]); putchar('\n'); */ for(k=0;k<len;k++)//虽然最多增加2位 , 但匹配函数是默认长度相同的 { if(ans[k]>9) { ans[k+1] += ans[k]/10;//两条语句不可交换,搞了好久才发现 ans[k] %= 10; } } bool flag = is_match(); if(flag) { if(i==len) { for(j=0;j<len;j++) printf("%c",str[j]); printf(" is cyclic\n"); memset(str,0,sizeof(str)); } k=0; for(j=len-1;j>=0;j--) num[k++]=str[j]-'0'; } else { for(j=0;j<len;j++) printf("%c",str[j]); printf(" is not cyclic\n"); memset(str,0,sizeof(str)); break; } } } return 0; }
#include<iostream>
#include<string>
using
namespace
std;
bool
fun(string str)
{
int
n=str.length()+1;
int
i,up=0,temp=0;
for
(i=n-2;i>=0;i--)
{
temp=(
int
)(str[i]-
'0'
);
if
((temp*n+up)%10!=9)
return
false
;
up=(temp*n+up)/10;
}
return
true
;
}
int
main()
{
string str1;
while
(cin>>str1)
{
if
(fun(str1))
{
cout<<str1<<
" is cyclic"
<<endl;
}
else
{
cout<<str1<<
" is not cyclic"
<<endl;
}
}
return
0;
}