1073. Scientific Notation 解析
来源:互联网 发布:金融入门书 知乎 编辑:程序博客网 时间:2024/06/13 00:29
科学计数法的转换
科学计数法的小数点必然在第三位,所以在处理的时候直接剃掉小数点然后根据后面指数左移右移小数点,填0。
这个程序其实有点小BUG。就是+0.34E+3的时候会输出0340,开头的0没有去掉,这个是我用string导致的。但是样例里面没有这样的测试。
…………………………更新线…………………………
科学计数法第一位一定不为0的。
第二次写这个程序发现的问题。+1.02E+2的时候不要错误处理成102.了后面是不能带点的。
#include <iostream>#include <vector>#include <string>using namespace std;string s;int main() {cin >> s;//判定负号bool decisNeg = false;int dotpos;string m;if (s[0] == '-') {decisNeg = true;}//读取科学计数的有效位int p = 1;string digit;while (p < s.size() && s[p] != 'E') {if (s[p] != '.')digit.push_back(s[p]);p++;}p++;int e = 0;bool eisNeg = false;if (s[p++] == '-')eisNeg = true;while (p < s.size()) {e *= 10;e += int(s[p] - '0');p++;}string ans;if (eisNeg) { //右移ans = digit;if (e != 1) {//需要补0for (int i = 0; i < e - 1; i++) {ans = "0" + ans;}}if (decisNeg) {cout << "-";}cout << "0." << ans << endl;return 0;}else {//左移if (e < digit.size() - 1) {//不用补0p = 0;for (int i = 0; i <= e ; i++,p++) {ans.push_back(digit[p]);}ans.push_back('.');while (p < digit.size()) {ans.push_back(digit[p]);p++;}}else if (e == digit.size() - 1)ans = digit;else {//补0ans = digit;for (int i = 0; i < e - (digit.size() - 1); i++) {ans += "0";}}if (decisNeg) {cout << "-";}cout << ans << endl;return 0;}return 0;}
0 0
- 1073. Scientific Notation 解析
- 1073. Scientific Notation <string>
- 1073. Scientific Notation
- 1073. Scientific Notation (20)
- PAT 1073. Scientific Notation
- 1073. Scientific Notation (20)
- 1073.Scientific Notation
- 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
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 1073. Scientific Notation
- 1073. Scientific Notation (20)
- Spring data jpa HQL @Query 自定义查询及更新删除 及 sql写法
- 检测点1.1
- 每日一句:Love
- 1.11内存地址空间(概述)
- 时间转换成毫秒值
- 1073. Scientific Notation 解析
- 1.12主板
- Python常用的函数库
- 1.13接口卡
- 1.14各类存储器芯片
- spring+springmvc+mybatis(下)
- 1.15内存地址空间
- Python 操作MySQL中文显示乱码的解决方法
- idea的使用,想到哪里就写到哪里