循环数

#include <iostream>
#include <cstring>

using namespace std;

const int MAXN = 100;

int main()
{
    bool is_cyclic(char origin[], char detect[]);       // is_cyclic() => 判断两个数字串是否成环后一致
    char number[MAXN] = {};
    while(cin >> number)
    {
        int n = strlen(number);
        int temp[MAXN + 10] = {0}, temp2[MAXN + 10] = {0};
        int m = 0;
        int iscyclic = true;
        for(int i = strlen(number) - 1; i >= 0; i--)    // 以temp存数值数组形式的number
        {
            temp[m++] = number[i] - '0';
        }
        for(int mul = 2; mul <= n; mul++)
        {
            memset(temp2, 0, sizeof(temp2));            // 以temp2存temp经高精度乘以后的结果
            for(int j = 0; j < MAXN; j++)
            {
                temp2[j] = temp[j] * mul;
            }
            for(int j = 0; j < MAXN + 5; j++)
            {
                if(temp2[j] >= 10)
                {
                    int ten = temp2[j] / 10;
                    temp2[j] %= 10;
                    temp2[j + 1] += ten;
                }
            }
            int len;
            for(len = MAXN + 9; len >= 0; len--)
            {
                if(temp2[len - 1] != 0)
                {
                    break;
                }
            }

            char buf[MAXN + 10] = {};                   //将计算结果以字符串存入buf
            m = 0;
            if(strlen(number) > len)
            {
                for(int i = 0; i < (strlen(number) - len); i++)
                {
                    buf[m++] = '0';
                }
            }
            for(int i = len - 1; i >= 0; i--)
            {
                buf[m++] = temp2[i] + '0';
            }
            buf[m] = 0;
            if(!is_cyclic(number, buf))
            {
                cout << number << " is not cyclic" << endl;
                iscyclic = false;
                break;
            }
        }
        if(iscyclic)
        {
            cout << number << " is cyclic" << endl;
        }
        memset(number, 0, sizeof(number));

    }
    return 0;
}

bool is_cyclic(char origin[MAXN], char detect[MAXN])
{
    if(strlen(origin) != strlen(detect))
    {
        return false;
    }
    int len = strlen(origin);
    for(int i = 0; i < len; i++)
    {
        char buf[MAXN] = {};
        int m = 0;
        for(int j = i; j < len; j++)
        {
            buf[m++] = detect[j];
        }
        for(int j = 0; j < i; j++)
        {
            buf[m++] = detect[j];
        }
        buf[m] = 0;
        if(!strcmp(origin, buf))
        {
            return true;
        }
    }
    return false;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值