码迷,mamicode.com
首页 > 其他好文 > 详细

ACM学习历程—51NOD 1770数数字(循环节)

时间:2016-05-24 16:34:35      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1770

这是这次BSG白山极客挑战赛A题。由于数字全部相同,乘上b必然会有循环节,于是模拟乘法,记录数据,出现循环就退出即可。

 

代码:

技术分享
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#define LL long long

using namespace std;

int a, b, d, n;

int main()
{
    //freopen("test.in", "r", stdin);
    int T, rest, to, pre;
    scanf("%d", &T);
    for (int times = 0; times < T; ++times)
    {
        int ans = 0, tmp;
        scanf("%d%d%d%d", &a, &b, &d, &n);
        rest = to = 0;
        pre = -1;
        for (int i = 0; i < n; ++i)
        {
            tmp = a*b+to;
            to = tmp/10;
            rest = tmp%10;
            if (tmp == pre)
            {
                if (rest == d) ans += n-i;
                break;
            }
            pre = tmp;
            if (rest == d) ans++;
        }
        if (to != 0 && to == d) ans++;
        printf("%d\n", ans);
    }
    return 0;
}
View Code

 

ACM学习历程—51NOD 1770数数字(循环节)

标签:

原文地址:http://www.cnblogs.com/andyqsmart/p/5523704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!