pat 1073. Scientific Notation
来源:互联网 发布:linux ab压力测试 编辑:程序博客网 时间:2024/05/19 20:44
原题链接:https://www.patest.cn/contests/pat-a-practise/1073
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 file 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-03Sample Output 1:
0.00123400Sample Input 2:
-1.2E+10Sample Output 2:
-12000000000
科学计数法转换为正常实数
主要分两种情况:1.小数点向前移:如果原先整数部分小于前移位数,则补0;如果大于,小数点移动即可
2.小数点后移:如果原先小数部分小于后移位数,则补0,如果大于,小数点移动即可
这里我把小数点前移且需要补0情况单独输出,其余情况又分为后移补0情况和不需要补0(前移和后移)情况
// pat_Scientific Notation.cpp : 定义控制台应用程序的入口点。//#include <cstdio>#include <cstring>#include <cmath>char str[10010];int main(int argc, char* argv[]){int i, j, len, flag = 0, cc = 0, tmp = 0, cnt = 0,sum=0;//cnt统计小数点前面位数,cc统计小数点后面位数,sum记录指数大小,tmp判断指数正负scanf("%s", str);len = strlen(str);for (i = 1; i < len; i++){if (str[i] == '.'){flag = 1;continue;}if (!flag)cnt++;if (flag){cc++;if (str[i] == 'E'){if (str[i + 1] == '+')tmp = 1;sum = 0;for (j = i + 2; j < len; j++){sum = sum * 10 + str[j] - '0';}break;}}}cc--;if (str[0] == '-')printf("-");if (!tmp&&cnt <= sum){printf("0.");for (i = 1; i <= sum - cnt; i++)printf("0");for (i = 1; str[i] != 'E'; i++){if (str[i] == '.')continue;printf("%c", str[i]);}printf("\n");}else{int head;if (tmp)head = sum + cnt;elsehead = sum - cnt;for (i = 1; i <= head&&i < len&&str[i] != 'E'; i++){if (str[i] == '.'){head++;continue;}printf("%c", str[i]);}if (tmp&&sum >= cc){for (i = 1; i <= sum - cc; i++)printf("0");printf("\n");return 0;}else{printf(".");}for (i = head + 1; str[i] != 'E'; i++)printf("%c", str[i]);printf("\n");}return 0;}
- PAT 1073. Scientific Notation
- PAT 1073. Scientific Notation
- 【PAT】1073. Scientific Notation
- pat 1073. Scientific Notation
- PAT 1073. Scientific Notation (20)
- PAT 1073. Scientific Notation (20)
- PAT 1073. Scientific Notation (20)
- PAT 1073. Scientific Notation (20)
- PAT 1073. Scientific Notation(20)
- PAT 1073. Scientific Notation (20)
- PAT 1073. Scientific Notation (20)
- 【PAT】1073. Scientific Notation (20)
- Pat 1073 PAT 1073. Scientific Notation (20)
- PAT (Advanced) 1073. Scientific Notation (20)
- PAT-1073. Scientific Notation(Java StringBuild)
- PAT Advanced Level 1073. Scientific Notation (20)
- PAT A 1073. Scientific Notation (20)
- PAT甲级练习1073. Scientific Notation (20)
- python-框架-网页爬虫-文本处理-科学计算-可视化-机器学习-数据挖掘-深度学习
- 常用网站
- java 锁与可重入锁
- ELK框架搭建
- 【数据库系统概念部分习题】第二章 关系模型介绍
- pat 1073. Scientific Notation
- JWT(JSON Web Token)
- 常用软件
- win2012从远程主机copy文件
- 学习设计模式基础的心得与代码(一)
- 常用第三方开源包
- Lunch Time ZOJ
- 强化学习(Reinforcement Learning)知识整理
- String,StringBuffer与StringBuilder的区别??