Uva--350 (数学)

本文介绍了一道关于求解循环周期的简单题,通过使用C++编程语言实现了该问题的算法。具体方法为记录每次循环的状态,一旦发现状态重复,则可以确定循环周期。

2014-06-11 13:59:56

题意&思路:简单题不多说。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

int main(){
    int s[10005],p[10005],Z,I,M,L,Case = 0;
    while(scanf("%d %d %d %d",&Z,&I,&M,&L) == 4){
        if(Z == 0 && I == 0 && M == 0 && L == 0)
            break;
        memset(s,0,sizeof(s));
        memset(p,0,sizeof(p));
        s[L] = 1;
        p[L] = 1;
        for(int cnt = 2; ; ++cnt){
            L = (Z * L + I) % M;
            if(s[L]){
                printf("Case %d: %d\n",++Case,cnt - p[L]);
                break;
            }
            else{
                s[L] = 1;
                p[L] = cnt;
            }
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3781821.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值