很蛋疼的题目,说的很不清楚,调了很久,各种特殊的格式没有说清楚,
附在网上找到的特殊数的处理方式
各种要求的计数格式:(有效位数 原数 要求的计数格式)
3 0 0.000*10^0
5 0.000000 0.00000*10^0
3 0.1 0.1*10^0
4 0.000010100 0.1010*10^-4
5 1.000201 0.10002*10^1
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
class digit{
private:
char a[101];
int pow;
bool iszero;
public:
digit(char *str = "NULL", int n = 0) :pow(n){
strcpy(a, str);
}
void Initial(int sign){
string b;
b = a;
int at, bt = 0;
pow = 0;
while (bt < b.size() && b[bt] == '0'){
bt++;
}
if (bt<b.size() && b[bt] != '.'){
a[0] = '0';
a[1] = '.';
at = 2;
while (bt < b.size() && b[bt] != '.'){
a[at++] = b[bt++];
pow++;
}
bt++;
while (bt < b.size()){
a[at++] = b[bt++];
}
while (at < sign + 2){
a[at++] = '0';
}
a[at] = '\0';
}
else {
at = 2;
a[0] = '0';
a[1] = '.';
bt++;
while (bt < b.size() && b[bt] == '0'){
pow--;
bt++;
}
if (bt >= b.size())
pow = 0;
while (bt < b.size()){
a[at++] = b[bt++];
}
while (at < sign + 2){
a[at++] = '0';
}
a[at] = '\0';
}
}
bool IsSame(digit &b, int &sign){
if (pow != b.pow)
return false;
int count = 0, i;
for (i = 2; i < strlen(a) && i < strlen(b.a) && count < sign; i++, count++)
if (a[i] != b.a[i])
break;
if (count == sign)
return true;
else return false;
}
void show(int sign){
int i;
bool flag = false;
cout << "0.";
for (i = 2; i < sign + 2 && i < strlen(a); i++){
cout << a[i];
if (a[i] != '0')
flag = true;
}
if (flag){
while (i < sign + 2){
cout << '0';
i++;
}
cout << "*10^" << pow;
}
else {
while (i < sign + 1){
cout << '0';
i++;
}
cout << "*10^" << pow;
}
}
friend istream & operator>>(istream &is, digit &b){
is >> b.a;
return is;
}
};
int main(){
freopen("1.in", "r", stdin);
digit a, b;
int n;
cin >> n;
cin >> a >> b;
a.Initial(n);
b.Initial(n);
bool flag = a.IsSame(b, n);
if (flag){
cout << "YES" << ' ';
a.show(n);
cout << endl;
}
else {
cout << "NO" << ' ';
a.show(n);
cout << ' ';
b.show(n);
cout << endl;
}
return 0;
}