a53

pomi

a53
Feb 27th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. using namespace std;
  4. ifstream fin("pomi.in");
  5. ofstream fout("pomi.out");
  6. char x[81];
  7. short a[100],b[100],k;
  8. short c[100],d[100],e[100];
  9. void setare(short *a, char *x)
  10. {
  11. int i,lg;
  12. lg=a[0]=strlen(x);
  13. for(i=0; i<lg; ++i)
  14. a[lg-i]=x[i]-'0';
  15. }
  16. void divide(const short *a, short k,short *d)
  17. {
  18. short lg1,nr,i,lg2,j;
  19. lg1=a[0];
  20.  
  21. if(a[lg1]<k)
  22. {
  23. nr=a[lg1];
  24. i=lg1-1;
  25. lg2=lg1-1;
  26. }
  27. else
  28. i=lg2=lg1,nr=0;
  29.  
  30. for(j=lg2; j>=1; --j,--i)
  31. {
  32. nr=nr*10+a[i];
  33. d[j]=nr/k;
  34. nr=nr%k;
  35. }
  36. d[0]=lg2;
  37. }
  38. void scade1(short *a)
  39. {
  40.  
  41. short i;
  42. i=1;
  43. while(a[i]==0)
  44. {
  45. a[i]=9;
  46. i++;
  47. }
  48. a[i]--;
  49. if(a[i]==0 && i==a[0])
  50. a[0]--;
  51. }
  52.  
  53. void diferenta(short *a, short *b, short *e)
  54. {
  55. short i,T=0;
  56. e[0]=max(a[0],b[0]);
  57. for(i=1; i<=c[0]; ++i)
  58. {
  59. e[i]=a[i]-b[i]+T;
  60. if(e[i]<0)
  61. {
  62. e[i]+=10;
  63. T=-1;
  64. }
  65. else
  66. T=0;
  67. }
  68. while(!e[e[0]] && e[0]>1)
  69. e[0]--;
  70. }
  71. int main()
  72. {
  73. fin>>x;
  74. setare(a,x);
  75. fin>>x;
  76. setare(b,x);
  77. fin>>k;
  78. divide(b,k,c);
  79. scade1(a);
  80. divide(a,k,d);
  81. diferenta(c,d,e);
  82. for(short i=e[0]; i>=1; --i)
  83. fout<<e[i];
  84. fout<<'\n';
  85. return 0;
  86. }
Add Comment
Please, Sign In to add comment