Advertisement
a53

Radical

a53
Oct 3rd, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. #define NMax 1000
  5. typedef int Huge[NMax+3];
  6.  
  7. void AtribValue(Huge H, unsigned long X) {
  8. H[0] = 0;
  9. while (X) {
  10. ++H[0];
  11. H[H[0]] = X % 10;
  12. X /= 10;
  13. }
  14. }
  15.  
  16. void CopyValue(Huge A,Huge B) /// A <- B
  17. {
  18. for(int i=0;i<=B[0];++i)
  19. A[i]=B[i];
  20. }
  21.  
  22. void Add(Huge A, Huge B)
  23. /* A <- A+B */
  24. { int i,T=0;
  25.  
  26. if (B[0]>A[0])
  27. { for (i=A[0]+1;i<=B[0];) A[i++]=0;
  28. A[0]=B[0];
  29. }
  30. else for (i=B[0]+1;i<=A[0];) B[i++]=0;
  31. for (i=1;i<=A[0];i++)
  32. { A[i]+=B[i]+T;
  33. T=A[i]/10;
  34. A[i]%=10;
  35. }
  36. if (T) A[++A[0]]=T;
  37. }
  38.  
  39. void MultHuge(Huge A, Huge B, Huge C)
  40. /* C <- A x B */
  41. { int i,j,T=0;
  42.  
  43. C[0]=A[0]+B[0]-1;
  44. for (i=1;i<=A[0]+B[0];) C[i++]=0;
  45. for (i=1;i<=A[0];i++)
  46. for (j=1;j<=B[0];j++)
  47. C[i+j-1]+=A[i]*B[j];
  48. for (i=1;i<=C[0];i++)
  49. { T=(C[i]+=T)/10;
  50. C[i]%=10;
  51. }
  52. if (T) C[++C[0]]=T;
  53. }
  54.  
  55. unsigned long Divide(Huge A, unsigned long X)
  56. /* A <- A/X si intoarce A%X */
  57. { int i;
  58. unsigned long R=0;
  59.  
  60. for (i=A[0];i;i--)
  61. { A[i]=(R=10*R+A[i])/X;
  62. R%=X;
  63. }
  64. while (!A[A[0]] && A[0]>1) A[0]--;
  65. return R;
  66. }
  67.  
  68. int Sgn(Huge H1, Huge H2) {
  69. // Elimina zero-urile semnificative, daca exista.
  70. while (H1[0] && !H1[H1[0]]) H1[0]--;
  71. while (H2[0] && !H2[H2[0]]) H2[0]--;
  72.  
  73. if (H1[0] < H2[0]) {
  74. return -1;
  75. } else if (H1[0] > H2[0]) {
  76. return +1;
  77. }
  78.  
  79. for (int i = H1[0]; i > 0; --i) {
  80. if (H1[i] < H2[i]) {
  81. return -1;
  82. } else if (H1[i] > H2[i]) {
  83. return +1;
  84. }
  85. }
  86. return 0;
  87. }
  88.  
  89. void Radical(Huge A)
  90. {
  91. Huge ST,DR,SD;
  92. AtribValue(ST,1);
  93. CopyValue(DR,A);
  94. int semnul;
  95. do
  96. {
  97. CopyValue(SD,ST);
  98. Add(SD,DR);
  99. Divide(SD,2);
  100. MultHuge(SD,SD,SD);
  101. semnul=Sgn(SD,A);
  102. }
  103. while((semnul==-1)||(semnul==1));
  104. }
  105.  
  106. void Afisare(Huge A)
  107. {
  108. for(unsigned int i=A[0];i>=1;--i)
  109. cout<<A[i];
  110. cout<<'\n';
  111. }
  112.  
  113. int main()
  114. {
  115. char s[30];
  116. Huge n,m;
  117. cin>>s;
  118. unsigned int L=strlen(s);
  119. for(unsigned int i=0;i<L;++i)
  120. n[L-i]=s[i]-'0';
  121. n[0]=L;
  122. cin>>s;
  123. L=strlen(s);
  124. for(unsigned int i=0;i<L;++i)
  125. m[L-i]=s[i]-'0';
  126. m[0]=L;
  127. Afisare(n);
  128. Afisare(m);
  129. Radical(n);
  130. Afisare(n);
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement