循环数的确定

#include <iostream>
#include <cstring>
#define MAX 60
using namespace std;
//两个数相加
int Add(int nMaxLen, int *a1, int *a2)
{
    int nHighestPos = 0;
    for(int i = 0; i <= nMaxLen - 1; ++i)
    {
        a1[i] += a2[i];
        if(a1[i] >= 10)
        {
            a1[i] -= 10;
            a1[i + 1]++;
        }
        if(a1[i + 1])
        nHighestPos = i + 1;
    }
    return nHighestPos;
}
int main()
{
    char *s1 = new char[MAX + 10]();
    char *s2 = new char[MAX + 10]();
    char *sDouble = new char[MAX + 10]();
    int *a1 = new int[MAX + 10]();
    int *a2 = new int[MAX + 10]();
    while(cin >> s1)
    {
       int nLen1 = strlen(s1);
       for(int i = nLen1 - 1, j = 0; 0 <= i; --i)
       {
           a1[j] = s1[i] - '0';
           a2[j++] = s1[i] - '0';
       }
       strcpy(sDouble, s1);
       strcat(sDouble, s1);
       bool bOk = true;
       for(int i = 0; i < nLen1 - 1; ++i)//注意这里一共计算nLen - 1个,不算第一个即原始的a1(a1 * 1)
       {
           int Pos = Add(nLen1, a1, a2);
           if(Pos >= nLen1)
           {
               bOk = false;
               break;
           }
           for(int i = nLen1 - 1, k = 0; 0 <= i; --i)
           {
               s2[k++] = a1[i] + '0';
           }
           for(int i = 0; i < nLen1; ++i)
           if(strstr(sDouble, s2) == NULL)
           {
               bOk = false;0101
               break;
           }
       }
       if(bOk)
       cout << s1 << " is cyclic" << endl;
       else
       cout << s1 << " is not cyclic" << endl;
    }
    delete [] s1;
    delete [] s2;
    delete [] a1;
    delete [] a2;
    delete [] sDouble;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值