PKU 1001解题代码
本来以前也写过,但是由于许多细节问题,没有AC,今天修改了一下,终于AC了,以前没有AC的具体原因总结了了一下,必须任何数的0次方都等于1没有考虑,还有就是首0和末尾0以及小数点没有处理好,下面贴代码,应该还可以优化一下, 比如乘数和被乘数如果是0,可以忽略,求指数也可以采用分治法,以后再有时间了补充吧
// bigMulMul.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
string bigAdd(string a,string b)
{
int lena=a.length();
int lenb=b.length();
int dislen=lena-lenb;
char cc[];
string temp;
int carry=;
if (lena>lenb)
{
for (int j= ;j<a.length();j++)
{
int c=;
if (j<lenb)
c=(a[j]-''+b[j]-'')+carry;
else
c=a[j]-''+carry;
char tt[];
sprintf(tt,"%d",c%);
temp+=tt;
carry=c/;
} }
if (lena<lenb)
{
for (int j= ;j<b.length();j++)
{
int c=;
if(j<lena)
c=(b[j]-''+a[j]-'')+carry;
else
c=b[j]-''+carry;
char tt[];
sprintf(tt,"%d",c%);
temp+=tt;
carry=c/;
}
}
if (lena==lenb)
{
for (int j= ;j<a.length();j++)
{
int c=;
c=(a[j]-''+b[j]-'')+carry;
sprintf(cc,"%d",c%);
temp+=cc;
carry=c/;
}
} if (carry>)
{
sprintf(cc,"%d",carry);
temp+=cc;
}
return temp; }
string bigMul(string a,string b)
{
int lena=a.length();
int lenb=b.length();
int apoint=lena-a.find('.')-;
int bpoint=lenb-b.find('.')-;
if (apoint==lena)apoint=;
if (bpoint==lenb)bpoint=;
char cc[];
string temp; //store one mul result
string total=""; //store all add result
int ZeroNum=;//store add zero num
for (int i=a.length()-;i>=;i--)
{
int carry=;
if (a[i]=='.')
continue;
for (int j=b.length()-;j>=;j--)
{
if (b[j]=='.')
continue;
int t=(a[i]-'')*(b[j]-'')+carry; //add carry
sprintf(cc,"%d",t%);
temp+=cc;
carry=t/; }
if (carry>)
{
sprintf(cc,"%d",carry);
temp+=cc;
}
if (total.length()==)
{
total=temp;
}else
{
temp.insert(,ZeroNum,''); //convenent for compute
total=bigAdd(total,temp);
ZeroNum++;
}
temp="";
}
if (apoint!=&&bpoint!=)
{
if (apoint+bpoint>total.length())
{
for (int i=;i<apoint+bpoint-total.length();i++)
{
total+= "";
} } total.insert(apoint+bpoint,".");
}
string locals=""; for (int i=total.length()-;i>=;i--)
{
locals+=total[i];
}
return locals;
}
string preProcess(string a ,int b)
{
string res;
char temp[]={};
int pos=a.find('.');
int i;
for (i=;i<a.length();i++)
{
if(a[i]!=''&&i!=pos)break;
}
if (i==a.length())
{
return "";
} res=a;
for (int i=;i<b-;i++)
{
res=bigMul(res,a);
}
int len=res.length();
while (res.length()>)//去除前面的0
{
if (res[]!='')
{
break;
}
res.erase(,);
}
if(res.find('.')!=-) //必须有小数点才可以去除尾部的0
{
for (int i=res.length()-;i>=;i--)//去除尾部的0
{
if (res[i]!='')
{
if(res[i]=='.')
res.erase(i,);
break;
}
res.erase(i,);
}
} return res;
}
int main(int argc, char* argv[])
{
char R[];
int b; while(cin>>R>>b)
{
if (b==) //如果b=0 ,任何数的0次方 =1
{
cout<<""<<endl;
return ;
}
string temp=preProcess(R,b); if (temp.length()==)
{
if (temp[]=='.')
{
cout<<""<<endl;
}
cout<<temp[]<<endl;
}else
{
for (int i=;i<temp.length();i++)
{
cout<<temp[i];
}
cout<<endl;
}
} return ;
}
PKU 1001解题代码的更多相关文章
- 数独_erlang解题代码
前几天LP玩数独,玩到大师级各种被虐,我看了看说,分分钟帮你做出来, 结果当然没有做出来. 于是上网搜了下数独的解题代码,看了下C的代码,大多是递归之类的(如http://blog.sina.com. ...
- POJ 1001 解题报告 高精度大整数乘法模版
题目是POJ1001 Exponentiation 虽然是小数的幂 最终还是转化为大整数的乘法 这道题要考虑的边界情况比较多 做这道题的时候,我分析了 网上的两个解题报告,发现都有错误,说明OJ对于 ...
- PAT (Advanced Level) Practise 1001 解题报告
GiHub markdown PDF 问题描述 解题思路 代码 提交记录 问题描述 A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判 ...
- PKU 1003解题
首先庆祝一下,今天连A了3题.感觉后面这题太简单了.. 由于英文不好 ,找了个翻译: 若将一叠卡片放在一张桌子的边缘,你能放多远?如果你有一张卡片,你最远能达到卡片长度的一半.(我们假定卡片都正放在桌 ...
- PKU 1002解题总结
闲来无事,研究了一下PKU1002的题目,大意就是把含有字母的电话号码,转换为数字,然后再计算每个电话号码出现的次数,输出.本来蛮简单的一道题,结果折腾了好久,主要也是自己的水平太菜了,先是直接用字符 ...
- 极限编程,最强N皇后JAVA解题代码,4秒出15皇后,33秒出16皇后
私人博客原文链接来自:http://www.hexcode.cn/article/show/eight-queen 8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,循环控制及其优化 8皇后以 ...
- ACM/ICPC ZOJ1009-Enigma 解题代码
#include <iostream> #include <string> using namespace std; int main() { int strwide; cin ...
- ACM/ICPC ZOJ1006-Do the Untwist 解题代码
#include <iostream> #include <string> #include <stdlib.h> using namespace std; int ...
- ACM/ICPC ZOJ1003-Crashing Balloon 解题代码
#include <iostream> using namespace std; int main() { int **array = new int *[100]; for ( int ...
随机推荐
- 前端常用的几个js判断(二)
10.鼠标悬停(hover)切换 class 属性假如当用户鼠标悬停在一个可点击的元素上时,你希望改变其效果,下面这段代码可以在其悬停在元素上时添加 class 属性,当用户鼠标离开时,则自动取消该 ...
- Java—泛型
泛型是JDK 5 中引入的一个新特性,泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型.泛型本质是参数化类型,也就是所操作的数据类型指定为一个参数. 假定我们有这样一个需求: ...
- UISegmentControl
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- Kettle6.0安装及问题总结-白痴教程
1.安装JDK 配置java环境变量 2.安装KETTLE: 官方下载地址:http://community.pentaho.com/projects/data-integration/ 下载完后,解 ...
- [转载] linux查找目录下的所有文件中是否含有某个字符串
链接自 http://blog.sina.com.cn/s/blog_691a84f301015khx.html,并略加修订. 查找目录下的所有文件中是否含有某个字符串 find .|xargs gr ...
- 如何在CentOS 7中禁止IPv6
最近,我的一位朋友问我该如何禁止IPv6.在搜索了一番之后,我找到了下面的方案.下面就是在我的CentOS 7 迷你服务器禁止IPv6的方法. 你可以用两个方法做到这个. 方法 1 编辑文件/etc/ ...
- 高手总结的“恋爱法”学习Linux系统,效果更好。
如果你恋爱了,那你一定非常喜欢她.了解她,知道她喜欢吃什么玩什么,知道她需要什么,在她生气的时候可以哄她开心,一切尽在你的手指中.那你想学好Linux吗?喜欢Linux吗?你懂她吗?你有喜欢Linux ...
- Flexbox,更优雅的布局
在设计的眼中,排版的操作是一件很简单的事情,靠左.置中.靠右,我只要点一下,所有元素,就会乖乖的到指定的位置. 但到了前端在排版的实现上,就不是这样了. 我们常常得用一堆其实本来不是这样用的属性来做 ...
- WebView 的使用----android和html的交互
一.主布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...
- js生成[n,m]的随机数 以及实际运用
Math.ceil(); //向上取整. Math.floor(); //向下取整. Math.round(); //四舍五入. Math.random(); //0.0 ~ 1.0 之间的一 ...