首页 > 其他分享 >(PAT乙级刷题) 舍入

(PAT乙级刷题) 舍入

时间:2024-06-21 23:30:46浏览次数:14  
标签:舍入 PAT int need && carry 刷题 com size

题目:

题解:

#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

相关文章

  • 【数据结构与算法 刷题系列】判断链表是否有环(图文详解)
                   ......
  • c#中path.combine的用法是什么
    原文链接:https://www.yisu.com/ask/29579392.html在C#中,Path.Combine()方法用于将两个或多个字符串路径组合成一个有效的路径。它接受多个字符串参数作为路径的组成部分,并返回一个字符串,表示有效的路径。语法如下:publicstaticstringCombine(paramsstring[]paths);参数pa......
  • 在使用InputStream templateStream = this.getClass().getClassLoader().getResourceA
    在使用 this.getClass().getClassLoader().getResourceAsStream()读取项目目录路径下的文件需要注意在pom.xml文件中加入<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-......
  • selenium框架学习之获取文本框内容和Xpath元素不唯一
    本周一直写创建简历的测试用例,由于元素和内容过多,只能把各个方法单独封装,然后在测试用例里面使用,以此优化用例。在封装的时候遇到的一些问题,和大家说下怎么解决~1. 获取文本框输入---新增了一个【输入微信号】的功能模块,需要同时测试点击【同手机】和输入文本的功能。于是......
  • Uipath 如何连接SQLite数据进行操作
      1、下载SQLiteDatabaseActivitiesforUipathpackages,无需安装SQLite数据库驱动。2、SQLite查询操作   2.1添加查询活动:在UIPathStudio的“Activities”面板中,搜索并添加“ExecuteQuery”活动。这个活动用于执行SQL查询语。在“ExecuteQuery”活动的属......
  • 设计模式 - Singleton pattern 单例模式
    文章目录定义单例模式的实现构成构成UML图单例模式的六种实现懒汉式-线程不安全懒汉式-线程安全饿汉式-线程安全双重校验锁-线程安全静态内部类实现枚举实现总结其他设计模式文章:定义单例模式是一种创建型设计模式,它用来保证一个类只有一个实例,并且提供一个访问......
  • 代码随想录刷题复习day01
    day01数组-二分查找classSolution{publicintsearch(int[]nums,inttarget){//左闭右闭intleft=0;intright=nums.length-1;intmid=0;while(right>=left){mid=left+(right-le......
  • svn cleanup 系统找不到指定路径_SVN:cleanup failed to process the following paths
    svn执行清理,提示“系统找不到指定的文件。”下面看一下如何解决这个问题吧:1.在sqlite官网(https://www.sqlite.org/download.html)下载sqlite-tools-win32-x86-3310100.zip(资源我会放在后面)下载下来打开如下图所示:2.将其中的sqlite3.exe文件复制到.svn目录下与wc.db同级......
  • SVG <pattern> 标签的用法和应用场景
    通过使用<pattern>标签,可以在SVG图像内部定义可重复使用的任意图案。这些图案可以通过fill属性或stroke属性进行引用。使用场景例如我们要在<svg>中绘制大量的圆点点,可以通过重复使用<circle>标签来实现。<svgwidth="100"height="100"xmlns="http://www.......
  • SVG <pattern> 标签的用法和应用场景
    通过使用<pattern>标签,可以在SVG图像内部定义可重复使用的任意图案。这些图案可以通过fill属性或stroke属性进行引用。使用场景例如我们要在<svg>中绘制大量的圆点点,可以通过重复使用<circle>标签来实现。<svgwidth="100"height="100"xmlns="http://www.w3.org......