R格式 (15届B) 高精度

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

void mul(vector<int> &A,int b)
{
	int t=0;
	for(int i=0;i<A.size();i++)
	{
		t+=A[i]*b;
		A[i]=t%10;
		t/=10;
		}
		if(t) A.push_back(t);
}

void add(vector<int >& A, int k,int b)
{
	int t=b;
	for(int i=k;i<A.size();i++)
	{
		t+=A[i];
		A[i]=t%10;
		t/=10;
	}
	if(t) A.push_back(t);
}


int main()
{
	int n;
	string d;
	cin>>n>>d;
	
	reverse(d.begin(),d.end());
	int dot=d.find('.');
	vector<int> D;
	for(auto c:d)
	 	if(c!='.')
	 	D.push_back(c-'0');
	
	while(n--) mul(D,2);
	if(D[dot-1]>=5) add(D,dot,1);
	for(int i=D.size()-1;i>=dot;i--)
	cout<<D[i];
	return 0;
}
相关推荐
鱼跃鹰飞4 分钟前
Leetcode会员尊享100题:270.最接近的二叉树值
数据结构·算法·leetcode
无小道29 分钟前
Qt——QWidget
开发语言·qt
时艰.34 分钟前
Java 并发编程之 CAS 与 Atomic 原子操作类
java·开发语言
梵刹古音1 小时前
【C语言】 函数基础与定义
c语言·开发语言·算法
筵陌1 小时前
算法:模拟
算法
梵刹古音1 小时前
【C语言】 结构化编程与选择结构
c语言·开发语言·嵌入式
Yvonne爱编码1 小时前
JAVA数据结构 DAY3-List接口
java·开发语言·windows·python
一方_self1 小时前
了解和使用python的click命令行cli工具
开发语言·python
南宫码农2 小时前
我的电视 - Android原生电视直播软件 完整使用教程
android·开发语言·windows·电视盒子
We་ct2 小时前
LeetCode 205. 同构字符串:解题思路+代码优化全解析
前端·算法·leetcode·typescript