zoukankan      html  css  js  c++  java
  • H

    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


    这个题!!!实在是看不懂它到底在说什么!!!英语太差没办法 忧愁!!!
    去网上搜来了翻译
    绐任一个整数 n(1 <= n <= 9999),且 n 不可被 2 或 5 除尽。n的某一个倍数以十进制的表示法将是一连串的1,请问这一连串的1最少是几位数?
    例如:
    n=3,3*37=111,所以答案是3位数
    n=7,7*15873=111111,所以答案是6位数。
    Input
    每一列测试资料有1个整数 n
    Output
    对每一测试资料n,其某一个倍数以十进制的表示法将是一连串的1,请问这一连串的1最少是几位数?

    看完翻译,题目确实不难。

    以下为第一次写出的超时代码
    
    
    #include <iostream>
    using namespace std;
    int main()
    {
        int n;
        while(cin>>n){
            __int64 a=1,i=1;
                while(a%n!=0){
                    a=a*10+1;
                    i++;
                }
                cout<<i<<endl;
        }
        //system("pause");
        return 0;
    }
    以下为正确代码,有一定的技巧性
    #include<iostream>
    using namespace std;
    int f(int n)
    {
        int i=1,a,b=1;
        a=b%n;
        while(a!=0)
        {
            b=a*10+1;
            i++;
            a=b%n;
        }
        return i;
    }
    int main()
    {
        int n;
        while(cin>>n)cout<<f(n)<<endl;
        return 0;
    }
    拿3来解释
    1%3=1,先将其乘3加1,直到得到的值大于3. 11%3=2 , 2×10+1=21 则21/3与111/3效果相同 (因为90%3=0,111-90=21 )

    要多思考 数学简便方法!!!
    
    
    
    
    
    
    
    

  • 相关阅读:
    记录——framework探测定位程序集与core探测定位程序集
    C# 特定框架适用特定代码
    python读取excel代码
    时间比较
    ORA 01791错误
    MongoDB.1什么是MongoDB
    Mayatis 异常之result maps collection already contains value...
    怎样做好黄焖鸡
    关于foreach
    C#之out,ref关键字
  • 原文地址:https://www.cnblogs.com/farewell-farewell/p/5184224.html
Copyright © 2011-2022 走看看