Advertisement
a53

numere20_formula

a53
Oct 16th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. using namespace std;
  4. typedef int NrMare[1010];
  5.  
  6. void AtribMic(NrMare x, int n)
  7. {
  8. x[0]=0;
  9. if(n==0)
  10. x[(x[0]=1)]=0;
  11. else
  12. for(;n;n/=10)
  13. x[++x[0]]=n%10;
  14. }
  15.  
  16. void Adunare(NrMare x,NrMare y)
  17. // x = x + y
  18. {
  19. int i,t=0;
  20. if(x[0]<y[0])
  21. x[0]=y[0];
  22. for(i=1;i<=x[0];i++,t/=10)
  23. {
  24. t=x[i]+y[i]+t;
  25. x[i]=t%10;
  26. // echivalent x[i]=(t+=x[i]+y[i])%10
  27. }
  28. if(t)
  29. x[++x[0]]=t;
  30. }
  31.  
  32. void Scadere(NrMare x, NrMare y)
  33. // x <-- x-y
  34. {
  35. int i,j;
  36. for (i = 1; i <= x[0]; i++)
  37. if(x[i]>=y[i])
  38. x[i]-=y[i];
  39. else
  40. {
  41. j=i+1;
  42. while(x[j]==0)
  43. x[j++]=9;
  44. x[j]--;
  45. x[i]=10+x[i]-y[i];
  46. }
  47. for (; x[0] > 1 && !x[x[0]]; x[0]--); // sa n-am zerouri nesemnificative
  48. }
  49.  
  50. int Divide(NrMare x, int n)
  51. //x = x /n, returneaza x%n
  52. {
  53. int i,r=0;
  54. for(i=x[0];i>0;i--)
  55. {
  56. r=10*r+x[i];
  57. x[i]=r/n;
  58. r%=n;
  59. }
  60. for(;x[x[0]]==0 && x[0]>1;)
  61. x[0]--;
  62. return r;
  63. }
  64.  
  65. ofstream g("numere20.out");
  66. void Afisare(NrMare n)
  67. {
  68.  
  69. for(int i=n[0];i>0;--i)
  70. g<<n[i];
  71. g<<'\n';
  72. }
  73.  
  74. int main ()
  75. {
  76. int u,v;
  77. char numar[101];
  78. NrMare n,npeu,npev,npeuxv;
  79. ifstream f("numere20.in");
  80. f>>numar;
  81. n[0]=strlen(numar);
  82. npeu[0]=n[0];
  83. npev[0]=n[0];
  84. npeuxv[0]=n[0];
  85. for(int k=0;k<n[0];++k)
  86. n[n[0]-k]=npeu[n[0]-k]=npev[n[0]-k]=npeuxv[n[0]-k]=numar[k]-'0';
  87. f>>u>>v;
  88. f.close();
  89. /// n-(n/u+n/v-n/(u*v))
  90. Divide(npeu,u);
  91. Divide(npev,v);
  92. Divide(npeuxv,u*v);
  93. Adunare(npeu,npev);
  94. Scadere(npeu,npeuxv);
  95. Scadere(n,npeu);
  96. Afisare(n);
  97. g.close();
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement