比赛 防止浮躁的小练习v0.4 评测结果 AAAAAA
题目名称 增强的减法问题 最终得分 100
用户昵称 kxxy 运行时间 0.002 s
代码语言 C++ 内存使用 0.31 MiB
提交时间 2016-10-13 15:58:56
显示代码纯文本
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. using namespace std;
  7. string x, y;
  8. int xx[101]={0}, yy[101]={0}, result[101]={0};
  9. bool poster=true;
  10. int maxlength=0;
  11. int minlength=0;
  12. inline void compare(string x, string y)
  13. {
  14. if (x.length()==y.length())
  15. {
  16. maxlength=x.length();
  17. minlength=y.length();
  18. if (x>y)
  19. {
  20. int j=x.length();
  21. int k=y.length();
  22. for (int i=j, q=0; i>0; i--, q++)
  23. xx[q]=x[i-1]-48;
  24. for (int i=k, q=0; i>0; i--, q++)
  25. yy[q]=y[i-1]-48;
  26. }
  27. else
  28. {
  29. poster=false;
  30. int j=x.length();
  31. int k=y.length();
  32. maxlength=y.length();
  33. minlength=x.length();
  34. for (int i=j, q=0; i>0; i--, q++)
  35. yy[q]=x[i-1]-48;
  36. for (int i=k, q=0; i>0; i--, q++)
  37. xx[q]=y[i-1]-48;
  38. }
  39. }
  40. else if (x.length()>y.length())
  41. {
  42. int j=x.length();
  43. int k=y.length();
  44. maxlength=x.length();
  45. minlength=y.length();
  46. for (int i=j, q=0; i>0; i--, q++)
  47. xx[q]=x[i-1]-48;
  48. for (int i=k, q=0; i>0; i--, q++)
  49. yy[q]=y[i-1]-48;
  50. }
  51. else if (x.length()<y.length())
  52. {
  53. poster=false;
  54. int j=x.length();
  55. int k=y.length();
  56. maxlength=y.length();
  57. minlength=x.length();
  58. for (int i=j, q=0; i>0; i--, q++)
  59. yy[q]=x[i-1]-48;
  60. for (int i=k, q=0; i>0; i--, q++)
  61. xx[q]=y[i-1]-48;
  62. }
  63. }
  64. inline void sub()
  65. {
  66. for (int i=0; i<maxlength; i++)
  67. result[i]=xx[i];
  68. for (int i=0; i<minlength; i++)
  69. result[i]-=yy[i];
  70. for (int i=0; i<maxlength-1; i++)
  71. {
  72. while (result[i]<0)
  73. {
  74. result[i]+=10;
  75. result[i+1]-=1;
  76. }
  77. }
  78. }
  79. inline void out_data()
  80. {
  81. bool flag=false;
  82. if (poster==false)
  83. cout<<"-";
  84. for (int i=maxlength; i>0; i--)
  85. {
  86. if (result[i-1]!=0)
  87. {
  88. flag=true;
  89. cout<<result[i-1];
  90. }
  91. else if (result[i-1]==0&&flag==true)
  92. cout<<0;
  93. }
  94. }
  95. int main()
  96. {
  97. freopen("sub.in","r",stdin);
  98. freopen("sub.out","w",stdout);
  99. cin>>x>>y;
  100. compare(x, y);
  101. if(x==y)
  102. {
  103. cout<<0;
  104. return 0;
  105. }
  106. sub();
  107. out_data();
  108. return 0;
  109. }