PAT 第二周 (1005-1010)_each input file contains one test case. each case -程序员宅基地

技术标签: 算法  c++  数据结构  

稍微熟练后又遇上这周的题目很是简单,观察了这两天做的六道题并不是一组,都是较简单的题,简单整理一下。

目录

1005 Spell It Right

1006 Sign In and Sign Out

1007 Maximum Subsequence Sum

1008 Elevator

1009 Product of Polynomials

1010 Radix


1005 Spell It Right

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (≤{\color{Red}10^{100} }).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345

Sample Output:

one five

本题实质是一个简单的高精度加法问题。

【本题代码】

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	char c = getchar();
	vector<int> str;
	vector<int> result;
	while (c != '\n') {
		str.push_back(int(c-'0'));
		c = getchar();
	}
	result.push_back(str[0]);
	for (int i = 1; i < str.size(); i++)
	{
		int add = str[i];
		for (int j = 0; j < result.size(); j++) {
			int temp = result[j] + add;
			if (temp < 10) {
				result[j] += add;
				break;
			}
			else {
				result[j] += add - 10;
				if (j == result.size() - 1) {
					result.push_back(1);
					break;
				}
				else add = 1;
			}
		}
	}
	for (int i = result.size() - 1; i >= 0; i--)
	{
		switch (result[i])
		{
		case 0:cout << "zero"; 
			break;
		case 1:cout << "one";
			break;
		case 2:cout << "two";
			break;
		case 3:cout << "three";
			break;
		case 4:cout << "four";
			break;
		case 5:cout << "five";
			break;
		case 6:cout << "six";
			break;
		case 7:cout << "seven";
			break;
		case 8:cout << "eight";
			break;
		case 9:cout << "nine";
			break;
		default:
			break;
		}
		if (i != 0) 
			cout << " ";
	}
	return 0;
}

1006 Sign In and Sign Out

At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day.

Input Specification:

Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:

ID_number Sign_in_time Sign_out_time

where times are given in the format HH:MM:SS, and ID_number is a string with no more than 15 characters.

Output Specification:

For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.

Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

Sample Input:

3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

Sample Output:

SC3021234 CS301133

本题的实质是排序问题。 用快速排序如下。

【本题代码】

#include<iostream>
#include<algorithm>
using namespace std;
void Sort(string*, string*, string*,int, int);
void swap(string&, string&);
bool cmp(string, string);
int main()
{
	int M;
	cin >> M;
	string* s1 = new string[M];
	string* s2 = new string[M];
	string* s3 = new string[M];
	for (int i = 0; i < M; i++)
		cin >> s1[i] >> s2[i] >> s3[i];
	Sort(s1, s3, s2, 0, M - 1);
	cout << s1[0] << " ";
	Sort(s1, s2, s3, 0, M - 1);
	cout << s1[M-1];
	return 0;
}
void Sort(string* s1, string* s2, string* s,int left,int right)
{
	if (left >= right)
		return;
	int index = left+1;
	for (int i = left + 1; i <= right; i++)
	{
		if (cmp(s[left], s[i])) {
			swap(s[index], s[i]);
			swap(s1[index], s1[i]);
			swap(s2[index], s2[i]);
			index++;
		}
	}
	swap(s[index-1], s[left]);
	swap(s1[index-1], s1[left]);
	swap(s2[index - 1], s2[left]);
	Sort(s1, s2, s, left, index - 2);
	Sort(s1, s2, s, index, right);
}
void swap(string& a, string& b)
{
	string temp = a;
	a = b;
	b = temp;
}
bool cmp(string a, string b)
{
	if (a[0] * 10 + a[1] > b[0] * 10 + b[1])
		return true;
	else if (a[0] * 10 + a[1] < b[0] * 10 + b[1])
		return false;
	else if (a[3] * 10 + a[4] > b[3] * 10 + b[4])
		return true;
	else if (a[3] * 10 + a[4] < b[3] * 10 + b[4])
		return false;
	else if (a[6] * 10 + a[7] > b[6] * 10 + b[7])
		return true;
	else if (a[6] * 10 + a[7] < b[6] * 10 + b[7])
		return false;
	else return true;
}

1007 Maximum Subsequence Sum

Given a sequence of K integers \left \{ N_1,N_2,...,N_K \right \}. A continuous subsequence is defined to be \left \{ N_i,N_{i+1},...,N_j \right \} where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

本题的实质为最大子串和问题。其实它是动态规划经典问题max(a[i],sum[i-1]+a[i]),但我写的时候还没意识到。

另外,本题需要注意的一点是对于N个数均为负数的情况,需要单独处理。

【本题代码】

#include<iostream>
using namespace std;
int main()
{
	int K, key = 0;
	cin >> K;
	int* a = new int[K];
	for (int i = 0; i < K; i++)
	{
		cin >> a[i];
		if (a[i] >= 0)key = 1;
	}
	if (key == 0)
		cout << 0 << " " << a[0] <<" "<< a[K - 1];
	else {
		int* sum = new int[K];
		sum[0] = a[0];
		int begin = 0;
		int end = 0;
		int max = sum[0];
		int x = 0, y = 0;
		for (int j = 1; j < K; j++)
		{
			if (sum[j - 1] < 0) {
				begin = end = j;
				sum[j] = a[j];
			}
			else {
				sum[j] = sum[j - 1] + a[j];
				end = j;
			}
			if (sum[j] > max)
			{
				max = sum[j];
				x = begin;
				y = end;
			}
		}
		cout << max << " " << a[x] << " " << a[y];
	}
}

1008 Elevator

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.

Output Specification:

For each test case, print the total time on a single line.

Sample Input:

3 2 3 1

Sample Output:

41

 本题就是一道小学数学题,不愿多说,我和我班上大一孩子说来着不会做就是没好好读题而已。

【本题代码】

#include<iostream>
using namespace std;
/*
电梯6s上升一层,4s下降一层,每层停5s
*/
int main()
{
	int N;
	cin >> N;
	int* a = new int[N];
	int sum = 0;
	int temp = 0;
	for (int i = 0; i < N; i++)
	{
		cin >> a[i];
		if (a[i] - temp > 0)
			sum += 5 + (a[i] - temp) * 6;
		else
			sum += 5 + (temp - a[i]) * 4;
		temp = a[i];
	}
	cout << sum;
	return 0;
}

1009 Product of Polynomials

This time, you are supposed to find A×B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N_{1} a_{N_{1}} N_{2} a_{N_{2}} ... N_{K} a_{N_{K}}

where K is the number of nonzero terms in the polynomial, N_{i} and a_{N_{i}}(i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1\leq K\leq 10, 0\leq N_{K}<...<N_{2}<N_{1}\leq 1000.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 3 3.6 2 6.0 1 1.6

本题和1002异曲同工,从多项式加法转变为多项式乘法,其实反而更简单,最暴力的方法就行,两层循环解决。

【本题代码】

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int K1,K2;
	cin >> K1;
	int* a = new int[K1];
	double* aa = new double[K1];
	for (int i = 0; i < K1; i++)
		cin >> a[i] >> aa[i];
	cin >> K2;
	int* b = new int[K2];
	double* bb = new double[K2];
	for (int i = 0; i < K2; i++)
		cin >> b[i] >> bb[i];
	double* m = new double[a[0] + b[0] + 1];
	for (int i = 0; i <= a[0] + b[0]; i++)
		m[i] = 0.0;
	for (int i = 0; i < K1; i++)
		for (int j = 0; j < K2; j++)
			m[a[i] + b[j]] += aa[i] * bb[j];
	int num = 0, key = 0;
	for (int i = a[0] + b[0]; i >= 0; i--) 
		if (m[i] != 0) 
			num++;
	cout << num << " ";
	for (int i = a[0] + b[0]; i >= 0; i--) {
		if (m[i] != 0) {
			if (key != 0)
				cout << " ";
			cout << fixed << setprecision(0) << i << setprecision(1) << " " << m[i];
			key++;
		}
	}
	return 0;
}

1010 Radix

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is yes, if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N_1 and N_2​, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:

N1 N2 tag radix

Here {\color{DarkGreen} N_1} and {\color{DarkGreen} N_2} each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number radix is the radix of {\color{DarkGreen} N_1} if tag is 1, or of {\color{DarkGreen} N_2} if tag is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation {\color{Pink} N_1=N_2} is true. If the equation is impossible, print Impossible. If the solution is not unique, output the smallest possible radix.

Sample Input 1:

6 110 1 10

Sample Output 1:

2

Sample Input 2:

1 ab 1 2

Sample Output 2:

Impossible

本题是六道题中花费时间最久的,主要源于以下几点原因:

N_1N_2的范围超过int,可用long long int解决,若超过long long int 范围可用<0来判断;

A digit is less than its radix,基数的下界最大的digit+1,不可单纯从2开始枚举;

③ 逐一增加基数枚举的算法会导致测试点7超时,可用二分法解决,由此需确定基数的上界N_1/N_2的值,因为任何基数的0次方都是1。

【本题代码】

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
long long int minRadix(string x);
long long int cal(string x,long long int radix);//int范围,测试样例中会有越界的
int main()
{
	string x, y;
	int tag;
	long long int radix, m1, m2;
	cin >> x >> y >> tag >> radix;
	if (tag == 1) {
		m1 = cal(x, radix); 
		long long int radix1, radix2;
		radix1 = minRadix(y);//不能从2开始枚举,因为digit要<radix且暴力枚举会超时
		radix2 = max(m1, radix1);
		while (radix1<=radix2) {
			long long int mid = (radix1 + radix2) / 2;
			m2 = cal(y, mid);
			if (m2 == m1) {
				cout << mid;
				return 0;
			}
			else if (m2 > m1 || m2 < 0)
				radix2 = mid - 1;
			else radix1 = mid + 1;
		}
	}
	else if (tag == 2) {
		m1 = cal(y, radix);
		long long int radix1, radix2;
		radix1 = minRadix(x);
		radix2 = max(m1, radix1);
		while (radix1 <= radix2) {
			long long int mid = (radix1 + radix2) / 2;
			m2 = cal(x, mid);
			if (m2 == m1) {
				cout << mid;
				return 0;
			}
			else if (m2 > m1 || m2 < 0)
				radix2 = mid - 1;
			else radix1 = mid + 1;
		}
	}
	cout << "Impossible";
	return 0;
}
long long int cal(string x,long long int radix)
{
	long long int m = 0;
	for (int i = 0; i < x.size(); i++) {
		if(x[i]<=57)
			m += (x[i] - '0') * pow(double(radix), x.size() - 1 - i);
		else
			m += (x[i] - 87) * pow(double(radix), x.size() - 1 - i);//a代表10
	}
	return m;
}
long long int minRadix(string x)
{
	long long int m = 0, max = 0;
	for (int i = 0; i < x.size(); i++) {
		if (x[i] <= 57)
			m = (x[i] - '0');
		else
			m = (x[i] - 87);
		if (m > max)
			max = m;
	}
	return max + 1;
}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Catherine_he_ye/article/details/122774751

智能推荐

python opencv resize函数_python opencv 等比例调整(缩放)图片分辨率大小代码 cv2.resize()...-程序员宅基地

文章浏览阅读1.3k次。# -*- coding: utf-8 -*-"""@File : 200113_等比例调整图像分辨率大小.py@Time : 2020/1/13 13:38@Author : Dontla@Email : [email protected]@Software: PyCharm"""import cv2def img_resize(image):height, width = image...._opencv小图等比例缩放

【OFDM、OOK、PPM、QAM的BER仿真】绘制不同调制方案的误码率曲线研究(Matlab代码实现)-程序员宅基地

文章浏览阅读42次。对于这些调制技术的误码率(BER)研究是非常重要的,因为它们可以帮助我们了解在不同信道条件下系统的性能表现。通过以上步骤,您可以进行OFDM、OOK、PPM和QAM的误码率仿真研究,并绘制它们的误码率曲线,以便更好地了解它们在不同信道条件下的性能特点。针对这些调制技术的BER研究是非常重要的,可以帮助我们更好地了解这些技术在不同信道条件下的性能表现,从而指导系统设计和优化。6. 分析结果:根据误码率曲线的比较,分析每种调制方案在不同信噪比条件下的性能,包括其容忍的信道条件和适用的应用场景。_ber仿真

【已解决】Vue的Element框架,日期组件(el-date-picker)的@change事件,不会触发。_el-date-picker @change不触发-程序员宅基地

文章浏览阅读2.5w次,点赞3次,收藏3次。1、场景照抄官方的实例,绑定了 myData.Age 这个值。实际选择某个日期后,从 vuetool(开发工具)看,值已经更新了,但视图未更新。2、尝试绑定另一个值: myData,可以正常的触发 @change 方法。可能是:值绑定到子对象时,组件没有侦测到。3、解决使用 @blur 代替 @change 方法。再判断下 “值有没有更新” 即可。如有更好的方法,欢迎评论!..._el-date-picker @change不触发

PCL学习:滤波—Projectlnliers投影滤波_projectinliers-程序员宅基地

文章浏览阅读1.5k次,点赞2次,收藏8次。Projectlnliersclass pcl: : Projectlnliers< PointT >类 Projectlnliers 使用一个模型和一组的内点的索引,将内点投影到模型形成新的一个独立点云。关键成员函数 void setModelType(int model) 通过用户给定的参数设置使用的模型类型 ,参数 Model 为模型类型(见 mo..._projectinliers

未处理System.BadImageFormatException”类型的未经处理的异常在 xxxxxxx.exe 中发生_“system.badimageformatexception”类型的未经处理的异常在 未知模块。 -程序员宅基地

文章浏览阅读2.4k次。“System.BadImageFormatException”类型的未经处理的异常在 xxxx.exe 中发生其他信息: 未能加载文件或程序集“xxxxxxx, Version=xxxxxx,xxxxxxx”或它的某一个依赖项。试图加载格式不正确的程序。此原因是由于 ” 目标程序的目标平台与 依赖项的目标编译平台不一致导致,把所有的项目都修改到同一目标平台下(X86、X64或AnyCPU)进行编译,一般即可解决问题“。若果以上方式不能解决,可采用如下方式:右键选择配置管理器,在这里修改平台。_“system.badimageformatexception”类型的未经处理的异常在 未知模块。 中发生

PC移植安卓---2018/04/26_电脑软件移植安卓-程序员宅基地

文章浏览阅读2.4k次。记录一下碰到的问题:1.Assetbundle加载问题: 原PC打包后的AssetBundle导入安卓工程后,加载会出问题。同时工程打包APK时,StreamingAssets中不能有中文。解决方案: (1).加入PinYinConvert类,用于将中文转换为拼音(多音字可能会出错,例如空调转换为KongDiao||阿拉伯数字不支持,如Ⅰ、Ⅱ、Ⅲ、Ⅳ(IIII)、Ⅴ、Ⅵ、Ⅶ、Ⅷ、Ⅸ、Ⅹ..._电脑软件移植安卓

随便推点

聊聊线程之run方法_start 是同步还是异步-程序员宅基地

文章浏览阅读2.4k次。话不多说参考书籍 汪文君补充知识:start是异步,run是同步,start的执行会经过JNI方法然后被任务执行调度器告知给系统内核分配时间片进行创建线程并执行,而直接调用run不经过本地方法就是普通对象执行实例方法。什么是线程?1.现在几乎百分之百的操作系统都支持多任务的执行,对计算机来说每一个人物就是一个进程(Process),在每一个进程内部至少要有一个线程实在运行中,有时线..._start 是同步还是异步

制作非缘勿扰页面特效----JQuery_单击标题“非缘勿扰”,<dd>元素中有id属性的<span>的文本(主演、导演、标签、剧情-程序员宅基地

文章浏览阅读5.3k次,点赞9次,收藏34次。我主要用了层次选择器和属性选择器可以随意选择,方便简单为主大体CSS格式 大家自行构造网页主体<body> <div class='main' > <div class='left'> <img src="images/pic.gif" /> <br/><br/> <img src="images/col.gif" alt="收藏本片"/&_单击标题“非缘勿扰”,元素中有id属性的的文本(主演、导演、标签、剧情

有了这6款浏览器插件,浏览器居然“活了”?!媳妇儿直呼“大开眼界”_浏览器插件助手-程序员宅基地

文章浏览阅读901次,点赞20次,收藏23次。浏览器是每台电脑的必装软件,去浏览器搜索资源和信息已经成为我们的日常,我媳妇儿原本也以为浏览器就是上网冲浪而已,哪有那么强大,但经过我的演示之后她惊呆了,直接给我竖起大拇指道:“原来浏览器还能这么用?大开眼界!今天来给大家介绍几款实用的浏览器插件,学会之后让你的浏览器“活过来”!_浏览器插件助手

NumPy科学数学库_数学中常用的环境有numpy-程序员宅基地

文章浏览阅读101次。NumPy是Python中最常用的科学数学计算库之一,它提供了高效的多维数组对象以及对这些数组进行操作的函数NumPy的核心是ndarray(N-dimensional array)对象,它是一个用于存储同类型数据的多维数组Numpy通常与SciPy(Scientific Python)和 Matplotlib(绘图库)一起使用,用于替代MatLabSciPy是一个开源的Python算法库和数学工具包;Matplotlib是Python语言及其Numpy的可视化操作界面'''_数学中常用的环境有numpy

dind(docker in docker)学习-程序员宅基地

文章浏览阅读1.1w次。docker in docker说白了,就是在docker容器内启动一个docker daemon,对外提供服务。优点在于:镜像和容器都在一个隔离的环境,保持操作者的干净环境。想到了再补充 :)一:低版本启动及访问启动1.12.6-dinddocker run --privileged -d --name mydocker docker:1.12.6-dind在其他容器访问d..._dind

推荐文章

热门文章

相关标签