poj 1047 Round and Round We Go 字符串的处理

本文介绍了一个用于检测循环数字的算法实现,包括输入验证、循环检测和输出判断。通过字符串操作和数学逻辑,该算法能够高效地识别指定长度内的循环数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Round and Round We Go
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10716 Accepted: 4913

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


post code:原字符串赋值两次

然后 在每次生成的新的字符串中进行查找,如果木有找到 就跳出 返回失败

首先先 将


#include<stdio.h>
#include<string.h>
char temp[100];
char sample[100];
char ch[200];
int  num[100],len;
int  add()   //add 使字符串相加
{
     int i,carry=0;
     for(i=len-1;i>=0;i--)
     {
        temp[i]+=num[i]+carry;
        if(temp[i]>'9'){
                       carry=1;                                 
                       temp[i]-=10;
                       }
        else carry=0;
     }   
     return carry;  
}

int main() 
{
     int i,judge;
     char *pos;
     while(scanf("%s",temp)!=EOF)
     {
          
          len=strlen(temp);
          strcpy(ch,temp);   // ch 进行判断 将原字符串链接到后面
          strcat(ch,temp);
          strcpy(sample,temp);  //sample 保存原字符
          for(i=0;i<len;i++)
          {
            num[i]=temp[i]-'0';                 
          }
          for(i=1;i<len;i++)
          {
             judge=add();          //进行叠加 *1就可以忽略了 一定可以
             if(judge==1)break;
             else {
                     pos=NULL;
                     pos=strstr(ch,temp);     //先查找 如果找到就可以了
                     if(pos==NULL)break;         
                  }                  
          }
          if(i==len)printf("%s is cyclic\n",sample);    //进行了n次 每次都能找到就可以了
          else printf("%s is not cyclic\n",sample);     //木有找到
                                             
     }    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值