1060. Are They Equal (25)
2015-12-06 11:14
543 查看
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123*105 with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine.
Input Specification:
Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and B are the two float numbers to be compared. Each float number is non-negative, no greater than 10100, and that its total digit number is less than 100.
Output Specification:
For each test case, print in a line "YES" if the two numbers are treated equal, and then the number in the standard form "0.d1...dN*10^k" (d1>0 unless the number is 0); or "NO" if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.
Note: Simple chopping is assumed without rounding.
Sample Input 1:[/b]
来源: <http://www.patest.cn/contests/pat-a-practise/1060>
[/code]
来自为知笔记(Wiz)
Input Specification:
Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and B are the two float numbers to be compared. Each float number is non-negative, no greater than 10100, and that its total digit number is less than 100.
Output Specification:
For each test case, print in a line "YES" if the two numbers are treated equal, and then the number in the standard form "0.d1...dN*10^k" (d1>0 unless the number is 0); or "NO" if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.
Note: Simple chopping is assumed without rounding.
Sample Input 1:[/b]
3 12300 12358.9Sample Output 1:[/b]
YES 0.123*10^5Sample Input 2:[/b]
3 120 128Sample Output 2:[/b]
NO 0.120*10^3 0.128*10^3
来源: <http://www.patest.cn/contests/pat-a-practise/1060>
#include<string>
#include <iostream>
using namespace std;
int n;
int SearchDot(string s) { //Search the pos of '.'
for (int i = 0; i < s.length(); i++) {
if (s[i] == '.')
return i;
}
return -1;
}
string ChangeToSci(string s1) {
string s1new;
bool isZero = true;//check whether it is 0
for (int i = 0; i < s1.length(); i++) {
if (s1[i] != '0'&&s1[i] != '.')
isZero = false;
}
if (isZero) {
s1new += "0.";
for (int i = 0; i < n; i++)
s1new += '0';
s1new += "*10^0";
return s1new;//if 0 return
}
bool hengji = false;
string temp;
for (int i = 0; i < s1.length()-1; i++) {//remove 0 in the head such as 05.02
if (s1[i+1]>='0'&&s1[i+1]<='9'&&s1[i] == '0'&&hengji == false) {
continue;
}
else {
hengji = true;
}
temp += s1[i];
}
temp += s1[s1.length() - 1];
s1 = temp;
int dotPos = SearchDot(s1);
if (dotPos == -1) {//no dot in the number ,such like 12345
s1new += "0.";
for (int i = 0; i < n; i++) {
if (i < s1.length())
s1new += s1[i];
else
s1new += '0';
}
s1new += "*10^";
int temp = s1.length();
if (temp == 100)
s1new += "100";
else if (temp >= 10) {
s1new += temp / 10 + '0';
s1new += temp % 10 + '0';
}
else
s1new += temp + '0';
}
else if (dotPos != 1) {//dot not at the second pos, such like 123.45 12.3
s1new += "0.";
bool dotFlag = false;
for (int i = 0; i < n; i++) {
if (i < s1.length()) {
if (s1[i] == '.') {
dotFlag = true;
continue;
}
s1new += s1[i];
}
else
s1new += '0';
}
if (dotFlag == true) {
if (n < s1.length())
s1new += s1[n];
else
s1new += '0';
}
s1new += "*10^";
int temp = dotPos;
if (temp == 100)
s1new += "100";
else if (temp >= 10) {
s1new += temp / 10 + '0';
s1new += temp % 10 + '0';
}
else
s1new += temp + '0';
}
else {
if (s1[0] == '0') {//0.23 0.00023 and so on
s1new += "0.";
int j = 2, cnt = 0, flag = 0;
while (true)
{
if (s1[j] == '0' && flag == 0)
cnt++;
else
break;
j++;
}
for (int i = j; i < j + n; i++) {
if (i < s1.length())
s1new += s1[i];
else
s1new += '0';
}
if (cnt == 0) {
s1new += "*10^0";
}
else {
s1new += "*10^-";
int temp = cnt;
if (temp == 100)
s1new += "100";
else if (temp >= 10) {
s1new += temp / 10 + '0';
s1new += temp % 10 + '0';
}
else
s1new += temp + '0';
}
}
else {//2.002 1.354 and so on
s1new += "0.";
for (int i = 0; i <= n; i++) {
if (i == 1)
continue;
if (i < s1.length())
s1new += s1[i];
else
s1new += '0';
}
s1new += "*10^1";
}
}
return s1new;
}
int main(void) {
string s1, s2;
string s1new, s2new;
cin >> n >> s1 >> s2;
if (n == 0) {
cout << "YES 0";
return 0;
}
string s11, s22;
s11 = ChangeToSci(s1);
s22 = ChangeToSci(s2);
if (s11 == s22) {
cout << "YES " << s11;
}
else {
cout << "NO " << s11 << " " << s22;
}
return 0;
}
[/code]
来自为知笔记(Wiz)
相关文章推荐
- 1059. Prime Factors (25)
- 几个比较简单的题,但是。。。。。
- nginx gzip压缩提升网站速度
- 1058. A+B in Hogwarts (20)
- 【android布局】px、dp和sp,这些单位有什么区别?
- Redis Mass Insertion(--pipe批量插入)
- 定义底层view的click等事件的作用范围
- iOS UITableView中异步加载图片
- 一种javascript链式多重继承的方式(__proto__原型链)
- 1057. Stack (30)
- 使用jQuery+EasyUI实现CheckBoxTree的级联选中特效
- 1056. Mice and Rice (25)
- 报错apachectl -t
- java内部类实例化
- 1055. The World's Richest (25)
- ubuntu开机自动启动程序设置
- 蓝桥杯 错误票据(排序)
- 高效能人士的七个习惯
- Mac之Git/GitHub使用(3)——Fork A Repo
- jquery入门