首页 > 其他 > 详细

D - Find The Multiple

时间:2020-11-03 21:23:38      阅读:60      评论:0      收藏:0      [点我收藏+]
Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.
Input
The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.
Output
For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111


1.每个数的二进制数 乘以 10,就等于他原来的十进制树乘以 2。
举个例子 : 1二进制1如果 1*10=10 那就是2的二进制,10*10=100 那就是4的二进制。
      一个十进制数除以2得到的数,他们在二进制里相差10倍。
2.同余定理

(a+b)%n = (a%n +b%n)%n;

(a*b)%n = (a%n *b%n)%n;

枚举每一个数的二进制数,看这个是不是n的倍数
 1 //
 2 // Created by w on 2020/11/3.
 3 //
 4 
 5 #include <iostream>
 6 using namespace std;
 7 int arr[1000000];
 8 int main()
 9 {
10     int n;
11     while (cin>>n)
12     {
13         int ans[200];
14         if(n==0)
15             return 0;
16         arr[1]=1;
17         int i=0;
18         for(i=2;arr[i-1]!=0;i++)
19         {
20             arr[i]=(arr[i/2]*10+i%2)%n; //一个个的枚举每个数的二进制,是否满足条件,感觉有点像堆排序
21         }
22         i--;
23         int k=0;  //记录下标,直到i==0
24         while (i)
25         {
26             int tmp=i%2;//缩减倍数
27             ans[k]=tmp;
28             k++;
29             i/=2;
30         }
31         for(int j=k-1;j>=0;j--)
32             cout<<ans[j];
33         cout<<endl;
34     }
35 
36 }

 

D - Find The Multiple

原文:https://www.cnblogs.com/B19044124/p/13922391.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!