题目:
题解:
#include<iostream>
#include<string>
using namespace std;
int main(){
int n, d, com, cnt, minus, all, carry, point, p, need, have, i, t;
string s;
cin >> n >> d;
while(n--){
cnt = minus = all = carry = point = p = need = have = 0;
cin >> com >> s;
if(s[0] == '-'){
minus = 1;
s.erase(0, 1);
}
while(p < s.size() && s[p] != '.') p++;//找到小数点位置
if(p < s.size() && com != 2){//处理进位
if(com == 1 && p + d + 1 < s.size() && s[p + d + 1] > '4') need = 1;
else if(com == 3 && p + d + 1 < s.size() && s[p + d + 1] > '4'){
if(s[p + d + 1] > '5') need = 1;
else{
for(int i = p + d + 2; i < s.size() && !have; i++){
if(s[i] >= '1' && s[i] <= '9') have = 1;
}
if(have || (s[p + d] - '0') % 2 == 1) need = 1;
}
}
carry = need;
for(int i = p + d; i >= 0 && need; i--){//高精度加法
if(s[i] == '.') continue;
t = s[i] - '0' + carry;
s[i] = t % 10 + '0';
carry = t / 10;
}
if(carry) s = "1" + s;//最高位仍有进位时,数字前加一
}
for(int i = 0; i < s.size() && i < p + d && !all; i++){//是否全0
if(s[i] >= '1' && s[i] <= '9') all = 1;
}
if(all && minus) cout << "-";//不是全0且有负号,才输出负号
for(i = 0; i < s.size() && i <= p + d; i++){
cout << s[i];
}
if(p == s.size()) {//整数时,添加小数点(D是正整数,所以一定需要输出小数点)
cout << ".";
p--;
}
while(i - p <= d + carry){//不足D位则补0
cout << "0";
i++;
}
cout << endl;
}
return 0;
}
标签:舍入,PAT,int,need,&&,carry,刷题,com,size
From: https://blog.csdn.net/2301_79580018/article/details/139871853