题目https://pintia.cn/problem-sets/994805342720868352/problems/994805413520719872

题意

给定两个数,表示成0.xxxxx*10^k次这样的科学记数法之后,判断小数点后的n位是否相同

思路

分类大讨论。细节要考虑清楚。因为最多是100位所以要用字符串处理。

首先我们要知道这两个数的小数点在什么位置(digita,digtb),如果没有小数点就默认在最后一位。

然后我们还需要知道有没有前导零(firsta,firstb),比如0.001,这样的数小数点也是要挪的。

所以*10^k中的k应该是$digita - firsta$,如果结果是负数还应该加一。

然后是输出前面的东西。要特别判断一下他们是不是0.

把输出的小数点之后的数字重新存储一下,如果不到n位就在后面补0

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<map>
  4. #include<set>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<algorithm>
  8. #include<vector>
  9. #include<cmath>
  10. #include<stack>
  11. #include<queue>
  12.  
  13. #define inf 0x7fffffff
  14. using namespace std;
  15. typedef long long LL;
  16. typedef pair<string, string> pr;
  17.  
  18. int n;
  19. char a[], b[];
  20.  
  21. int main()
  22. {
  23. scanf("%d", &n);
  24. scanf("%s%s", a, b);
  25. int tmpa[], tmpb[];
  26. int diga = -, digb = -;
  27. int lena = strlen(a), lenb = strlen(b);
  28. int da = , db = ;
  29. int firsta = -, firstb = -;
  30. for(int i = ; i < lena; i++){
  31. if(a[i] == '.'){
  32. diga = i;
  33. continue;
  34. }
  35. if(firsta == - && a[i] != ''){
  36. firsta = i;
  37. }
  38. if(diga != - && firsta != -)break;
  39. }
  40. if(diga == -)diga = lena;
  41. if(firsta != -){
  42. diga -= firsta;
  43. if(diga < )diga++;
  44. }
  45. // if(diga < 0){
  46. // while(da < abs(diga)){
  47. // tmpa[da++] = 0;
  48. // }
  49. // }
  50. for(int i = max(firsta, ); i < lena; i++){
  51. if(a[i] == '.'){
  52. continue;
  53. }
  54. tmpa[da++] = a[i] - '';
  55. }
  56. while(da < n){
  57. tmpa[da++] = ;
  58. }
  59.  
  60. for(int i = ; i < lenb; i++){
  61. if(b[i] == '.'){
  62. digb = i;
  63. continue;
  64. }
  65. if(firstb == - && b[i] != ''){
  66. firstb = i;
  67. }
  68. }
  69. if(digb == -){
  70. digb = lenb;
  71. }
  72. if(firstb != -){
  73. digb -= firstb;
  74. if(digb < )digb++;
  75. }
  76. // if(digb < 0){
  77. // while(db < abs(digb)){
  78. // tmpb[db++] = 0;
  79. // }
  80. // }
  81. for(int i = max(, firstb); i < lenb; i++){
  82. if(b[i] == '.')continue;
  83. tmpb[db++] = b[i] - '';
  84. }
  85. while(db < n){
  86. tmpb[db++] = ;
  87. }
  88.  
  89. //cout<<firsta<<endl<<firstb<<endl;
  90. //cout<<diga<<endl<<digb<<endl;
  91.  
  92. if(diga != digb && (firsta != - || firstb != -) && (firsta < n || firstb < n)){
  93. printf("NO 0.");
  94. if(firsta == - || firsta >= n){
  95. for(int i = ; i < n; i++){
  96. printf("");
  97. }
  98. printf("*10^0 0.");
  99. }
  100. else{
  101. for(int i = ; i < n; i++){
  102. printf("%d", tmpa[i]);
  103. }
  104. printf("*10^%d 0.", diga);
  105. }
  106. if(firstb == - || firstb >= n){
  107. for(int i = ; i < n; i++){
  108. printf("");
  109. }
  110. printf("*10^0\n");
  111. }
  112. else{
  113. for(int i = ; i < n; i++){
  114. printf("%d", tmpb[i]);
  115. }
  116. printf("*10^%d\n", digb);
  117. }
  118. }
  119. else{
  120. bool flag = true;
  121. for(int i = ; i < n; i++){
  122. if(tmpa[i] != tmpb[i]){
  123. flag = false;
  124. break;
  125. }
  126. }
  127. if(firsta == - && firstb == -)flag = true;
  128. if(flag){
  129. printf("YES 0.");
  130. if(firsta == -){
  131. for(int i = ; i < n; i++){
  132. printf("");
  133. }
  134. printf("*10^0\n");
  135. }
  136. else{
  137. for(int i = ; i < n; i++){
  138. printf("%d", tmpa[i]);
  139. }
  140. printf("*10^%d\n", diga);
  141. }
  142. }
  143. else{
  144. printf("NO 0.");
  145. if(firsta == -){
  146. for(int i = ; i < n; i++){
  147. printf("");
  148. }
  149. printf("*10^0 0.");
  150. }
  151. else{
  152. for(int i = ; i < n; i++){
  153. printf("%d", tmpa[i]);
  154. }
  155. printf("*10^%d 0.", diga);
  156. }
  157. if(firstb == -){
  158. for(int i = ; i < n; i++){
  159. printf("");
  160. }
  161. printf("*10^0\n");
  162. }
  163. else{
  164. for(int i = ; i < n; i++){
  165. printf("%d", tmpb[i]);
  166. }
  167. printf("*10^%d\n", digb);
  168. }
  169. }
  170. }
  171.  
  172. return ;
  173. }

PAT甲级1060 Are They Equal【模拟】的更多相关文章

  1. PAT 甲级 1060 Are They Equal

    1060. Are They Equal (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue If a ma ...

  2. PAT 甲级 1060 Are They Equal (25 分)(科学计数法,接连做了2天,考虑要全面,坑点多,真麻烦)

    1060 Are They Equal (25 分)   If a machine can save only 3 significant digits, the float numbers 1230 ...

  3. 【PAT】1060 Are They Equal (25)(25 分)

    1060 Are They Equal (25)(25 分) If a machine can save only 3 significant digits, the float numbers 12 ...

  4. PAT甲级1026 Table Tennis【模拟好题】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805472333250560 题意: 有k张乒乓球桌,有的是vip桌 ...

  5. PAT甲级1080 Graduate Admission【模拟】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805387268571136 题意: 模拟高考志愿录取. 考生根据总 ...

  6. PAT 甲级 1026 Table Tennis(模拟)

    1026. Table Tennis (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A table ...

  7. pat 甲级 1053. Path of Equal Weight (30)

    1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  8. PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)

    1053 Path of Equal Weight (30 分)   Given a non-empty tree with root R, and with weight W​i​​ assigne ...

  9. PAT甲级——A1060 Are They Equal

    If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...

随机推荐

  1. dyld`__abort_with_payload:

    dyld`__abort_with_payload: 0x1030422f0 <+0>:  mov    x16, #0x209 0x1030422f4 <+4>:  svc  ...

  2. Variable used in lambda expression should be final or effectively final

    Lambda与匿名内部类在访问外部变量时,都不允许有修改变量的倾向,即若: final double a = 3.141592; double b = 3.141592; DoubleUnaryOpe ...

  3. Asp.Net MVC4中的全局过滤器

    可以对整个项目进行全局监控. 新建一个MVC4项目,可以在global.asax文件中看到如下代码:  FilterConfig.RegisterGlobalFilters(GlobalFilters ...

  4. NDK配置

    NDK 配置 Android SDK中下载NDK, LLDB Android.mk 和 Application.mk 简单来说 Android.mk 用来描述需要生成哪些模块的 .so 文件 Appl ...

  5. numpy数组(4)-二维数组

    python创建二维 list 的方法是在 list 里存放 list : l = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] numpy可以直接 ...

  6. IOS开发之Storyboard应用

    制作一个Tab类型的应用 制作一个表格视图 原型表格单元 设计自定义的原型单元格 为原型单元格设置子类 故事版(Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明 ...

  7. 浅析tornado 中demo的 blog模块

    #!/usr/bin/env python # # Copyright 2009 Facebook # # Licensed under the Apache License, Version 2.0 ...

  8. 关于Python打包运行的一些思路

    需求 本地开发python django应用程序,然后放到生产环境运行.使用了tensorflow,手动安装包很麻烦.生产环境不能联网,不能使用 pip freeze. 思路: 使用docker,直接 ...

  9. django template if return false

    如果if的参数不存在于context中就会返回false 参考:http://stackoverflow.com/questions/11107028/django-template-if-true- ...

  10. one-to-all及all-to-all网络通信模式

    在这两种模式下,因为 占用的通信通道非常高,形成了一个一对多的通道 甚至是多对多的通道,导致现有的fattree网络结构负载太大.