PAT甲级——1073 Scientific Notation (20分)
Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [+-][1-9].[0-9]+E[+-][0-9]+ which means that the integer portion has exactly one digit, there is at least one digit in the fractional portion, and the number and its exponent's signs are always provided even when they are positive.
Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.
Input Specification:
Each input contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent's absolute value is no more than 9999.
Output Specification:
For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros.
Sample Input 1:
+1.23400E-03
Sample Output 1:
0.00123400
Sample Input 2:
-1.2E+10
Sample Output 2:
-12000000000
我的解法:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
string s;
cin >> s;
if(s[0] == '-')
cout << "-";
int n,i;
for(i = 1; s[i] != 'E'; i++);
string t = s.substr(1,i-1);
n = stoi(s.substr(i+1));
if(n < 0)
{
cout << "0.";
for(int j = 0; j < abs(n)-1; j++)
cout << "0";
for(int j = 0; j < t.length(); j++)
{
if(t[j] != '.')
cout << t[j];
}
}
else{
if(n >= t.length()-2)
{
for(int j = 0; j < t.length(); j++)
{
if( t[j] != '.')
cout << t[j];
}
for(int j=0 ;j < n-t.length()+2; j++)
{
cout << "0";
}
}
else{
for(int j = 0; j<n+2; j++)
{
if(t[j] != '.')
cout << t[j];
}
cout << ".";
for(int j= n+2; j < t.length(); j++)
cout << t[j];
}
}
return 0;
}
柳婼的解法:
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
int i = 0;
while (s[i] != 'E') i++;
string t = s.substr(1, i-1);
int n = stoi(s.substr(i+1));
if (s[0] == '-') cout << "-";
if (n < 0) {
cout << "0.";
for (int j = 0; j < abs(n) - 1; j++) cout << '0';
for (int j = 0; j < t.length(); j++)
if (t[j] != '.') cout << t[j];
}
else {
cout << t[0];
int cnt, j;
for (j = 2, cnt = 0; j < t.length() && cnt < n; j++, cnt++) cout <<
t[j];
if (j == t.length()) {
for (int k = 0; k < n - cnt; k++) cout << '0';
}
else {
cout << '.';
for (int k = j; k < t.length(); k++) cout << t[k];
}
}
return 0;
}
PAT甲级——1073 Scientific Notation (20分)的更多相关文章
- PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)
1073 Scientific Notation (20 分) Scientific notation is the way that scientists easily handle very ...
- PAT Advanced 1073 Scientific Notation (20 分)
Scientific notation is the way that scientists easily handle very large numbers or very small number ...
- PAT Basic 1024 科学计数法 (20 分) Advanced 1073 Scientific Notation (20 分)
科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [+-][1-9].[0-9]+E[+-][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指 ...
- 【PAT甲级】1073 Scientific Notation (20 分)
题意: 输入科学计数法输出它表示的数字. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> u ...
- PAT 甲级 1035 Password (20 分)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...
- 1073. Scientific Notation (20)
题目如下: Scientific notation is the way that scientists easily handle very large numbers or very small ...
- PAT 甲级 1050 String Subtraction (20 分) (简单送分,getline(cin,s)的使用)
1050 String Subtraction (20 分) Given two strings S1 and S2, S=S1−S2 is defined to be t ...
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT 甲级 1042 Shuffling Machine (20 分)(简单题)
1042 Shuffling Machine (20 分) Shuffling is a procedure used to randomize a deck of playing cards. ...
随机推荐
- 055-for循环中break的使用
<?php ;;$x++){ //省略表达式2的for循环将是无限循环 echo "$x<br />"; ){ break; //使用if语句控制退出无限循环 } ...
- 8. Redis 持久化对生产环境的灾难恢复的意义
1.故障发生的时候会怎么样2.如何应对故障的发生 很多同学,自己也看过一些redis的资料和书籍,当然可能也看过一些redis视频课程 所有的资料,其实都会讲解redis持久化,但是有个问题,我到目前 ...
- C# SqlBulkCopy 大量数据导入到数据库
之前写了一篇C# 直接使用sql语句对数据库操作 (cmd.ExecuteNonQuery)的文章 这是针对数据量不大的操作,换句话说,效率太低,所以在此介绍一个效率高的.能大批量导入到数据库的方法 ...
- centos破解压缩文件密码
rarcrack是linux系统端的一款破解加密压缩包的工具,rarcrack使用的穷举法进行破解已经加密的rar.zip和7z压缩包,支持设置多线程,和文件类型进行破解. 1.rarcrack下载页 ...
- c++程序—布尔值
#include<iostream> using namespace std; #include<string> int main() { //创建bool数据类型 bool ...
- 自定义checkbox,redio等
直接上代码: 看的懂看,看不懂拉到. .messageState li {list-style: none;float: left;padding-right:30px;font-size: 16px ...
- maven的理解和使用
一.maven是什么? maven是项目管理工具 二.maven为什么要用? 在做开发的时候常常会用到外部的工具包(jar包),这就需要你一个一个的去他们的官网下工具包,然后在项目里依赖他们,比较的麻 ...
- python人脸识别项目face-recognition
该项目基于Github上面的开源项目人脸识别face-recognition,主要是对图像和视频中的人脸进行识别,在开源项目给出的例子基础上对视频人脸识别的KNN算法进行了实现. 0x1 工程项目结构 ...
- 吴裕雄--天生自然TensorFlow2教程:数据统计
import tensorflow as tf a = tf.ones([2, 2]) a tf.norm(a) tf.sqrt(tf.reduce_sum(tf.square(a))) a = tf ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...