百炼1001: Exponentiation 解题
链接:http://bailian.openjudge.cn/practice/1001/
思路
乍一看是很简单的题目,但是答案必须高精度输出,因此需要手动实现一个高精度运算方法。如果直接使用int,然后循环乘,结果很容易就会超出int的最大值。因此正确的思路是通过一个数组来保存答案,然后按照数组内容输出。我使用了一个vector容器来保存结果,将每次乘法的结果从个位数往上保存(即逆序)。
这一题需要注意的坑是:
- 需要去掉首尾多余的0;
- 不能去除太多的0。
解
这里我直接放我的代码,可以参考一下:
#include <iostream>
#include <vector>
using namespace std;
vector<short> multiply(vector<short> const &R_0, vector<short> const &R){
vector<short> ans;
int cf = 0;
for(int i = 0; i < R.size(); i++){
for(int j = 0; j < R_0.size(); j++){
int mul = cf + R[i] * R_0[j];
cf = mul / 10;
mul %= 10;
if(ans.size() <= i + j){
ans.push_back(mul);
}
else{
cf += (ans[i + j] + mul) / 10;
ans[i + j] = (ans[i + j] + mul) % 10;
}
}
if(cf) {
ans.push_back(cf);
cf = 0;
}
}
return ans;
};
int main() {
int const len = 6;
string s;
int n;
while(cin >> s >> n){
int dec = 0;
vector<short> R;
vector<short> R_0;
R_0.push_back(1);
for(int i = len - 1; i >= 0; i--){
if(s[i] == '.'){
dec = len - 1 - i;
}
else{
R.push_back(short(s[i] - '0'));
}
}
dec *= n;
for(int i = 0; i < n; i++){
R_0 = multiply(R_0, R);
}
int tail = 0;
while(R_0[tail] == 0)
tail ++;
int i = R_0.size()-1;
while(R_0[i] == 0 && i > dec-1) i--;
for(; i >= tail || i > dec-1; i--){
if(i == dec-1)
cout << ".";
cout << R_0[i];
}
cout << endl;
}
return 0;
}
推荐用例
除了页面上的几个用例可以测试外,可以用以下用例判断程序是否错误:
130.00 12
期望结果:
23298085122481000000000000
百炼1001: Exponentiation 解题的更多相关文章
- POJ 1001 Exponentiation(大数运算)
POJ 1001 Exponentiation 时限:500 ms 内存限制:10000 K 提交材料共计: 179923 接受: 43369 描述:求得数R( 0.0 < R < ...
- [POJ 1001] Exponentiation C++解题报告 JAVA解题报告
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 126980 Accepted: 30 ...
- 百炼OJ - 1001 - Exponentiation
题目链接 哇一遍AC的感觉也太爽了吧:) #include <stdio.h> #include <string.h> int times=0; char *myCalc(ch ...
- [POJ] #1001# Exponentiation : 大数乘法
一. 题目 Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 156373 Accepted: ...
- POJ 1001 Exponentiation 无限大数的指数乘法 题解
POJ做的非常好,本题就是要求一个无限位大的指数乘法结果. 要求基础:无限大数位相乘 额外要求:处理特殊情况的能力 -- 关键是考这个能力了. 所以本题的用例特别重要,再聪明的人也会疏忽某些用例的. ...
- 1001. Exponentiation高精度运算总结
解题思路 这道题属于高精度乘法运算,要求输入一个实数R一个指数N,求实数R的N次方,由于R有5个数位,而N又特别大,因此用C++自带的数据类型放不下. 解题思路是通过数组储存每次乘积结果和底数的每一位 ...
- poj 1001 Exponentiation 第一题 高精度 乘方 难度:1(非java)
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 138526 Accepted: 33859 ...
- POJ 1001 Exponentiation(JAVA,BigDecimal->String)
题目 计算实数a的n次方,具体输出格式看案例 import java.util.*; import java.math.*; public class Main { public static voi ...
- POJ 1001 Exponentiation
题意:求c的n次幂……要求保留所有小数…… 解法:一开始只知道有BigInteger……java大数+模拟.第一次写java大数……各种报错各种exception……ORZ 没有前导0和小数后面的补位 ...
随机推荐
- Huawei vlan 配置及vlan 间通讯
Huawei Vlan配置及vlan 间通讯实例 组网需求:汇聚层交换机做为 PC 电脑的网关, PC3直连 SW2 属于 vlan 2,网关为 vlanif 2 接口地址192.168.2.1/24 ...
- How to Be Assertive Asking for What You Want Firmly and Fairly
What Is Assertiveness? It's not always easy to identify truly assertive behavior. This is because th ...
- 点击一个div ,把div里的某个参数的值,传到一个input里面
- .NET Core 使用 EF 出错的解决方法
在.NET Core 项目钟(类库),使用Entity Framework,建立模型生成数据库时,失败 Could not load assembly 'xxx'. Ensure it is refe ...
- css固定footer到浏览器底部的方法
<html> <head></head> <body> <div class="page-wrapper"> <d ...
- 方法(method)和函数(function)的区别
函数是一段代码,通过名字来进行调用.它能将一些数据(参数)传递进去进行处理,然后返回一些数据(返回值),也可以没有返回值. 所有传递给函数的数据都是显式传递的. 方法也是一段代码,通过一个与对象相关联 ...
- VS2013自带报表+打印功能
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010773667/article/details/27540389 经过了VB版机房收费系统的学习 ...
- android与JS交互,互相调用方法,跳转到网页
在main下面New - Folder - Assets Folder,在Assets下面新建一个js_android.html <html><head> <meta h ...
- Object Detection API 相关
训练官方提供的数据集: http://blog.csdn.net/LiJiancheng0614/article/details/77756252 训练自己的数据集(墙外): https://medi ...
- 筛选法求N以内的所有素数
素数:一个数只能被1和它本身整除的数.2是最小的素数 #include <iostream> using namespace std; #define NUM 100 ]; int mai ...