1073. Scientific Notation (20)
来源:互联网 发布:算法导论22章 编辑:程序博客网 时间:2024/05/29 17:46
通过E的位置来判断有效位数,从而计算是否加小数点,是否添0等等
E的位置pos,E后面的数字n,具体算出来的式子已再代码中
也贴一下以前考乙级时候的代码
#include<iostream>#include<string>using namespace std;int main(){ string str1; getline(cin, str1); int pos = str1.find('E'); int n = stoi(string(str1, pos + 1));//E后面的数字 string str2(str1, 0, pos);//获得有效数字 str2.erase(2, 1);//去掉小数点 if (n < 0)//小于0,前面添0,注意位置0字符是+/-,所有从位置1开始 { str2.insert(1, -n,'0'); str2.insert(2, 1,'.'); } else if (n >= 0 && n <= pos - 4)//有小数点的情况 { str2.insert(2 + n, 1, '.'); } else//没有小数点的情况,后面添0(可能正好不用添0) { str2.insert(str2.size(), n - pos + 3, '0'); } if (str2[0] == '+')str2.erase(0, 1);//如果位置0是+,则舍之 cout << str2<<endl;}
#include<stdio.h>#include<string.h>#include<stdlib.h>#pragma warning(disable:4996)int main(){ char flag,a[10000], b[10000], x; int i=0, K; flag = getchar(); a[0] = getchar(); getchar(); a[1] = '\0'; while ((x = getchar()) != 'E') b[i++] = x; b[i] = '\0'; scanf("%d", &K); K++; strncat(a, b,10000); if (K <= 0) { if (flag == '-') putchar('-'); printf("0."); for (i = -K;i > 0;i--) putchar('0'); puts(a); } else if( K > 0 && K <(int)strlen(a)) { for (i = strlen(a)+1;i > K;i--) { a[i] = a[i - 1]; } a[i] = '.'; if (flag == '-') putchar('-'); puts(a); } else { if (flag == '-') putchar('-'); printf("%s", a); K = K - strlen(a); for (;K > 0;K--) putchar('0'); putchar('\n'); } return 0;}
0 0
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 形参改变实参的值不变(C语言基础知识加例题讲解)
- 事件模式与观察者模式
- Word Ladder II
- 常见的进程调度算法
- 欢迎使用CSDN-markdown编辑器
- 1073. Scientific Notation (20)
- PAT甲级练习1021. Deepest Root (25)
- Canny算子
- Shiro 之认证登录的dem
- PAT-B 1022. D进制的A+B (20)
- Java内存不足之PermGen space错误探究
- 基于Android NDK的交叉编译
- java.lang.OutOfMemoryError: PermGen space及其解决方法
- Activiti工作流简单入门