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 ...
随机推荐
- mybatis高级(2)_数据库中的列和实体类不匹配时的两种解决方法_模糊查询_智能标签
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...
- Bootstrap学习之起步
安装Bootstrap环境 从 http://getbootstrap.com/ 上下载 Bootstrap 的最新版本.我下载的是预编译版,即下图中的第一个. 将其解压缩到任意目录即可看到以下(压缩 ...
- django orm总结[转载]
django orm总结[转载] 转载地址: http://www.cnblogs.com/linjiqin/archive/2014/07/01/3817954.html 目录1.1.1 生成查询1 ...
- OC — (Foundation框架-NSDate)
NSDate:是OC中处理日期时间的一个类,可以用来表示时间 获取当前的时间 NSDate *d = [NSDate date]; 创建日期时间对象 NSLog输出是当前时间 格林时间 格式化显示时间 ...
- JavaScript 面向对象(三) —— 高级篇
JavaScript 面向对象(一) —— 基础篇 JavaScript 面向对象(二) —— 案例篇 一.json方式的面向对象 首先要知道,js中出现的东西都能够放到json中.关于json数据格 ...
- SQLSERVER2008R2数据库的整体导出及单个表的导出步骤
今天在同事导SQLSERVER数据库中的表的时候遇到一问题,不知道怎么单独的把一个表的建表语句导出来,,迅速百度一下,按照步骤还真导出来了,导出单个表的步骤看下面来啦....: 点中数据库名字---- ...
- 前端面试题 之 JavaScript
昨天我们一起分享了关于html和css的面试题<前端面试题之Html和CSS>,今天我们来分享关于javascript有关的面试题.我面试的时候最害怕面试官问我js了,因为我真心不擅长这个 ...
- Practical Malware Analysis里有关inetsim\APATEDNS
以前从未接触过linux,碰到了许多问题,按步骤: 1\安装VMWARE,安装ubuntu16.04 问题1:之前装的是VM10,装完后没有安装VMTOOLS,我点安装 VMTOOLS,它弹出“简易安 ...
- My Game --文件读取数据
My Game --线段数据 中说到背景的绘制由贝赛尔曲线生成线段,用 DrawNode 画多边形,同时一张背景有两座山,一座山有两条以上贝赛尔曲线保存,用了嵌套的数据类:Bezier,LineLay ...
- SharePoint中报表选择
Office 365中制作报表的方式很多. 这里介绍下使用js获取SharePoint List实现报表的一种方法 资源 Jquery 1.8.2 http://blog.jquery.com/201 ...