Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4. ifstream cin("decbin.in");
  5. ofstream cout("decbin.out");
  6. int n,b,i,aux,nr,j,ok,r;
  7. long long p,put,maxx,v[100005];
  8. void citire()
  9. {
  10. cin>>n>>b;
  11. }
  12. void rezolvare()
  13. {
  14. aux=n;
  15. while(aux)
  16. {
  17. nr++;
  18. aux=aux/b;
  19. }
  20. p=1<<nr;
  21. p--;
  22. cout<<0<<'\n';
  23. ok=1;
  24. i++;
  25. nr=0;
  26. while(i<=p && ok==1)
  27. {
  28. r=1;
  29. for(j=1; j<=nr && r==1; j++)
  30. {
  31. v[j]=v[j]+r;
  32. if(v[j]==2){
  33. v[j]=0;
  34. r=1;
  35. }
  36. if(v[j]==1)
  37. {
  38. r=0;
  39. }
  40. }
  41. if(r>0)
  42. {
  43. v[++nr]=1;
  44. r=0;
  45. }
  46. /*for(j=nr; j>=1; j--)
  47. {
  48. cout<<v[j];
  49. }
  50. cout<<' ';*/
  51. put=1;
  52. maxx=0;
  53. for(j=1; j<=nr; j++)
  54. {
  55. maxx=put*v[j]+maxx;
  56. put=put*b;
  57. }
  58. if(maxx>n)
  59. ok=0;
  60. else cout<<maxx<<'\n';
  61. i++;
  62. }
  63.  
  64. }
  65. int main()
  66. {
  67. citire();
  68. rezolvare();
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement