Chinaunix首页 | 论坛 | 博客
  • 博客访问: 342921
  • 博文数量: 122
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 1191
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 11:12
文章分类

全部博文(122)

文章存档

2010年(122)

我的朋友

分类: C/C++

2010-05-03 16:57:08

一、问题描述

Description

Given any integer 0 <= n <= 10000 not divisible by 2 or 5, some multiple of n is a number which in decimal notation is a sequence of 1's. How many digits are in the smallest such a multiple of n?

Input

Each line contains a number n.

Output

Output the number of digits.

Sample Input

3

7

9901

Sample Output

3

6

12

 

二、解题思路

做一下除法就可以从步骤中得到思路。int ones;表示1的个数,从b=1,ones=1开始,如果b%n==0,则停止,输入ones;否则b=(b%n)*10+1;ones++;

三、代码

 

#include<iostream>
using namespace std;
int Ones(int a)
{
    int ones=1;
    int b=1;
    int r;
    r=b%a;

    while(r!=0)
    {
        b=r*10+1;
        ones+=1;
        r=b%a;
    }
    return ones;

}
int main()
{
    int x;
    while(scanf("%d",&x)!=EOF)
    {
        printf("%d\n",Ones(x));
    }
    return 0;
}


阅读(1865) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~