poj1047 高精度

 

如题:http://poj.org/problem?id=1047

Round and Round We Go
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12099 Accepted: 5652

Description

A cyclic number is an integer n digits in length which, when multiplied by any integer from 1 to n, yields a"cycle"of the digits of the original number. That is, if you consider the number after the last digit to "wrap around"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

Input

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, "01"is a two-digit number, distinct from "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

Source

 

 

 

思路:一开始我使用vis数组记录原数组数字i是否出现,然后高精度乘法后看结果的每一位在vis中是否出现,之后发现不行。

一个数组可以重复出现,不然就vis数组去记录i数字出现的次数,应该是可行的,我没去尝试,找到了一种更方便的方法。

将原数组复制一份叫做t,和结果数组排序一一对比是否一样。我怎么没想到!!!最后用这种方法轻松AC。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 65

int a[MAXN];

int F(int ma)
{
 int i,j,k;
 for(i=2;i<=ma;i++)
 {
  int c[MAXN]={0};
  int t[MAXN]={0};
  k=0;
   for(j=1;j<=ma;j++)
    t[++k]=a[j];
  for(j=1;j<=ma;j++)
  {
   c[j]+=t[j]*i;
   c[j+1]+=c[j]/10;
   c[j]%=10;
  }
  sort(t+1,t+1+ma);
  sort(c+1,c+1+ma);
  for(j=1;j<=ma;j++)
   if(c[j]!=t[j])
    return 0;
 }
 return 1;
}

int main()
{
 //freopen("C:\\1.txt","r",stdin);
 char str[MAXN];
 while(~scanf("%s",str+1))
 {
  memset(a,0,sizeof(a));
  int len=strlen(str+1);
  int i;
  int ma=0;
  for(i=len;i>=1;i--)
   a[++ma]=str[i]-'0';
  if(F(ma))
   printf("%s is cyclic\n",str+1);
  else
   printf("%s is not cyclic\n",str+1);
 }
 return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值