9

こんにちは アドビのインタビューで聞かれた質問です。

Numbers ending in 3 have at least one multiple having all ones. 
for eg., 3 and 13 have amultiples like 111 and 111111 respectively. Given such 
a no. , find the smallest such multiple of that number. The multiple can 
exceed the range of int, long. You cannot use any complex data structure.

効率的な解決策を教えてください

4

6 に答える 6

14

今答えを得ました:)

int count=1, rem=1; 
while(rem)
{
 rem= (rem*10+1)%n; count++;
} 
 while(count--){ cout<<"1";}
于 2013-07-02T18:24:37.377 に答える
0
#include <iostream>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--){
        long long n;
        cin>>n;
        long long cur = 1;
        while(cur%n!=0){
            cur = cur*10+1;
        }
        cout<<cur<<endl;
    }
    return 0;
}
于 2015-10-12T20:52:05.980 に答える
0

Java でのソリューションをお探しの場合は、こちら

public static void main(String[] args) {

    int input = 55333;
    int minAllOnesNum = 1;
    int nextAllOnesNum= minAllOnesNum;  

    int numberof1s=1;
    int count = 0;
    while(true)
    {
        count++;

        if(nextAllOnesNum%input == 0 ) 
        {
            break;  
        }

        nextAllOnesNum = nextAllOnesNum*10 + 1;

        if(nextAllOnesNum>=input) {
            nextAllOnesNum%=input;
        }
        numberof1s++;
    }


    System.out.println("Number of Iterations: " + count);

    for(int i=1; i<=numberof1s; i++) {
        System.out.print("1");
    }
}
于 2021-12-13T17:57:09.453 に答える