PAT 1020 Are They Equal (25)
2016-07-26 11:29
274 查看
思路
特殊输入有 0.000 0.0123 123.02 1234021.找到小数点’.’的位置,小数点’.’是第几个就代表指数项e是几,除了俩种情况即无小数点123402(返回-1)和<1的数
2.对于无小数点的,e就等于它的长度,对于<1的数,要数除去小数点后有几个0,然后就减去几(例如0.0123 = 1 - 2 =-1,即表示为0.123时指数为-1),再对于0.000 这种情况,直接令其指数为0即可。
3.以上步骤已经得到指数项e,接下来求base,即先去除小数点,如果length小于n则补零,否则从a中取第i个就可以了。
4.最后在比较e和base,如果都相等则相等。
我出错的一些点
1.这个以前没用过int pos = a.find('.');
2.这里以前也没用过
a = a.erase(pos, 1);
3.忘记输入为0.000这种情况。
代码
#include <iostream> #include <string> using namespace std; void find_base_e(string &a,int &e1,string &base1,int n) { //第一步:找到'.' int pos = a.find('.'); int length = a.length(); //第二步:求得e,分三种情况 if (pos < 0) { //第一种 e1 = length; } else { //第二种 e1 = pos; a = a.erase(pos, 1); } //第三种 length = a.length(); for (int i = 0; i < length; i++) { if (a[0] == '0') { //针对 0.000这种情况 if ((int)a.size() == 1 && a[0] == '0') { e1 = 0; break; } a.erase(0, 1); e1--; } else break; } //第三部:求得base base1 = ""; length = a.length(); for (int i = 0; i < n; i++) { if (i < length) { base1 += a[i]; } else base1 += '0'; } } int main() { int n; string a, b; cin >> n >> a >> b; string base1, base2; int e1, e2; find_base_e(a, e1, base1,n); find_base_e(b, e2, base2, n); if (base1 == base2&&e1 == e2) { cout << "YES " << "0." << base1 << "*10^" << e1 << endl; } else { cout << "NO "; cout << "0." << base1 << "*10^" << e1; cout << " "; cout << "0." << base2 << "*10^" << e2; cout << endl; } return 0; }
相关文章推荐
- 版本更新
- #NOIP 2014#day.2 T1 无限网络发射器选址
- xml解析工具类
- cygwin 2.8.74版本离线包(分别为win7 32和64位)
- hdu 2147 kiki's game 巴什博弈
- 列表、元组以及字符串
- 动态构建easyUI grid
- C 语言条件运算符详细讲解
- Android Jni 查找错误位置注意事项
- Django入门:python manage.py dbshell错误
- 图片缓存之内存缓存技术LruCache,软引用
- 《性能测试》之理发店模型
- 解决bash: mysql: command not found 的方法
- POJ1548
- super和this的比较
- ajax之原理
- HTTPS系列之RSA算法
- javascrip 日常使用
- 模块SEO优化中{分类名称}分隔符去掉及只调用下级分类方法
- IPV6