POJ2551 & UVA 10127 Ones (数论) 计算至少要多少位十进制的1能够被n整除

55 篇文章 1 订阅
17 篇文章 0 订阅

 Ones

Time limit: 3.000 seconds

Description

Given any integer 0 ≤ n ≤ 10000 not divisibleby 2 or 5, some multiple of n is a number whichin decimal notation is a sequence of 1’s. Howmany digits are in the smallest such a multipleof n?

Input

A file of integers at one integer per line.

Output

Each output line gives the smallest integer x > 0such that p =∑x−1i=0 1×10i = a×b, where a is thecorresponding input integer, and b is an integergreater than zero.Sample Input379901Sample Output


Sample Input

3

7

9901


Sample Output

3

6

12


原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19740

题意:输入一个定不能被2和整除的非负整数n,求它的最小倍数,全由一组成(十进制),即求至少需要多少个1(十进制)将其整除.

例如,n=3时,111 % 3 == 0,所以输出3

n=7是,111 111 % 3 == 0,所以输出6

解题思路:

原来打算"暴力求解",即:x=1,11,111...依次试下去,但不对!样例第三个就无法输出(数字太大了...),

后来发现,如果还没有找到,只要上次的余数*10+1就可以了,大大简化了计算.


AC代码:

#include <iostream>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        int ans=1,time=1;
        while(ans%n)
        {
            ans=ans%n*10+1;
            //ans=ans*10+1;
            time++;
        }
        cout<<time<<endl;
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值