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 ...
随机推荐
- angular-ui-router中的$stateProvider设置
$stateProvider .state('contacts.list', { url: '', templateUrl: 'contacts.list.html' }) .state('conta ...
- python+selenium安装步骤
1.先安装python 2.下载setuptools 使用方法是在 命令提示符(cmd)下 输入 "easy_install包名称" 3.安装pip 4.安装selenium如果是 ...
- cf#382div2
A. 题意:字符串长度n,每次可向左向右跳k个格子.要求不能在障碍物处停留('#'),可以在空地处停留(' . ').给出字符串,从G开始,问能不能到达T. 分析:直接从G处开始向两边搜,如果能到T则 ...
- css样式大全
字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...
- Windows添加和取消右键管理员权限
亲测可用 新建文本文档,粘贴下列代码 Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\*\shell\runas]@="管理员取 ...
- file_put_contents保存数据,
file_put_contents("sui.txt", var_export($now_member,TRUE),FILE_APPEND); 可以保存数组,方便调试
- EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(二)
前言 写完第一篇后,我一直在想接下来应该从哪一方面开始讲.后来我觉得不用那么死板的把每一个课程和大纲都列出来吧,毕竟我又不是教书的,呵呵...我觉得就像做实验一样,我们一部分一部分的完成,最后总个结果 ...
- 将已有项目提交到github/从github上pull到本地
去自己的工作分支$ git checkout work 工作.... 提交工作分支的修改$ git commit -a 回到主分支$ git checkout master 获取远程最新的修改,此时不 ...
- C#简易一元二次求解器
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- swift 自定义图片轮播视图
Swift封装图片轮播视图: import UIKit class XHAdLoopView: UIView { private var pageControl : UIPageControl? pr ...