博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1001解题报告
阅读量:4205 次
发布时间:2019-05-26

本文共 2142 字,大约阅读时间需要 7 分钟。

 

刚开始做ACM,水平很烂,这道题也是看了网上的解题报告自己照猫画虎写出来的

原文见

这里只是多加了点注释,希望对别的同学有所帮助,对自己也是个总结。

POJ的测试程序只要有一个不对便是Wrong Answer,所以细节都得注意到。需要注意的情况是结果为整数时不能输出小数点“.”,另外0.00 的 0 次方也是我之前没有注意到的。

总体而言,这道题是将字符串转化为整数,然后做大整数乘法。

大整数乘法每五位一组,最后是加入小数点。

#include <iostream>

#include <iomanip>

#include <sstream>

#include <string>

#include <vector>

#include <cmath>

using namespace std;

 

//这部分是将字符串转化为整数,同时记录小数点的位置

//整数的值记录在val中,小数点的位置为返回值

int stringToInt(const string &s,int &val)

{

bool flag=true;//判断小数点前后的标志

string pre,suf;//分别记录小数点之前和之后的部分

for(int ix=0;ix!=s.size();++ix)

{

if(s[ix]=='.')//如果碰到了小数点

{

flag=false;

continue;

}

if(flag)

pre.push_back(s[ix]);//小数点之前的部分

else

suf.push_back(s[ix]);//小数点之后的部分

}

//下面是除去前后的零

while(pre.size()&&pre.find("0")==0)

{

pre=pre.substr(1,pre.size()-1);

}

while(suf.size()&&suf.rfind("0")==suf.size()-1)

{

suf=suf.substr(0,suf.size()-1);

}

int len=suf.size();//记录小数点的位置

val=atoi(suf.c_str())+atoi(pre.c_str())*(int)pow(10.0,len);//将字符串转化为整数

return len;//返回小数点的位置

}

 

int main()

{

string s;

int n,d;

double dval;

vector<double> dvec;//虽然做的是大整数乘法,但用浮点数记录结果,因为两个五位的整数相乘结果可能会超过int范围

while(cin>>s>>n)

{

dvec.clear();

d=0;

int len=stringToInt(s,d);

dvec.push_back(d);

for(int ix=0;ix<n-1;++ix)//连乘求幂

{

int yushu=0;//进位

for(int index=0;index!=dvec.size();++index)//每一部分都处理

{

//先与整数相乘,再加上进位,然后记录小于100000的部分,其余高位作为进位

dvec[index]*=d;

dvec[index]+=yushu;

yushu=dvec[index]/100000;

dvec[index]-=(double)yushu*100000;//注意这里的转化,否则可能会超出int范围导致出错

}

if(yushu!=0)//如果最高位有进位,则增加一部分

dvec.push_back(yushu);

}

//注释部分是为了检验整数计算结果,便于调试

//for(int index=dvec.size()-1;index>=0;--index)

//{

//cout<<dvec[index]<<endl;

//}

//用字符串读写对结果进行处理,加入小数点

ostringstream os;

string tempstr;

os<<dvec[dvec.size()-1];//高位在后面,所以从后往前处理

string output;

output=os.str();

for(int index=dvec.size()-2;index>=0;--index)

{

os.str("");//注意与os.clear()的区别

os<<dvec[index];

tempstr=os.str();

//如果不到5位,则在前面补零

if(tempstr.size()<5)

tempstr.insert(0,5-tempstr.size(),'0');

output+=tempstr;

}

if(len==0)//整数的情况要

cout<<output<<endl;

else if(n*len>=output.size())

cout<<"."<<setw(n*len)<<setfill('0')<<output<<endl;

else

{

output.insert(output.size()-n*len,".");

cout<<output<<endl;

}

}

return EXIT_SUCCESS;

}

 

转载地址:http://itxli.baihongyu.com/

你可能感兴趣的文章
Velocity 模板基本用法
查看>>
SpringMVC 使用总结
查看>>
Mybatis 出现Mapped Statements collection does not contain value for xxx
查看>>
Mybatis 解决字段名与实体类属性名不相同的冲突
查看>>
Mybatis Generator最完整配置详解
查看>>
Mybatis 一级缓存和二级缓存
查看>>
Hibernate 出现Unsupported major.minor version 52.0 [duplicate]
查看>>
Hibernate 使用Intellij IDEA自动生成.hbm.xml文件
查看>>
Hibernate 出现org.hibernate.MappingNotFoundException: resource:**.hbm.xml not found问题的解决方案
查看>>
Hibernate 注解使用总结
查看>>
JAVA 事务之JDBC事务、JTA事务和容器事务
查看>>
EJB 是什么
查看>>
Hibernate 异常StrategySelectionException: Unable to resolve name EhCacheRegionFactory
查看>>
Hibernate 异常CacheException: Another unnamed CacheManager already exists in the same VM
查看>>
Python 常用的几种安装Module方式
查看>>
Mongodb 创建用户后登陆出现mongoAuthentication failed
查看>>
Mongodb GridFS、服务器脚本和数据库引用
查看>>
Mongodb 数据库管理
查看>>
JAVA 基本类型的默认值和取值范围
查看>>
JDK 1.5-1.8特性
查看>>