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

智能推荐

解决win10/win8/8.1 64位操作系统MT65xx preloader线刷驱动无法安装_mt65驱动-程序员宅基地

文章浏览阅读1.3w次。转载自 http://www.miui.com/thread-2003672-1-1.html 当手机在刷错包或者误修改删除系统文件后会出现无法开机或者是移动定制(联通合约机)版想刷标准版,这时就会用到线刷,首先就是安装线刷驱动。 在XP和win7上线刷是比较方便的,用那个驱动自动安装版,直接就可以安装好,完成线刷。不过现在也有好多机友换成了win8/8.1系统,再使用这个_mt65驱动

SonarQube简介及客户端集成_sonar的客户端区别-程序员宅基地

文章浏览阅读1k次。SonarQube是一个代码质量管理平台,可以扫描监测代码并给出质量评价及修改建议,通过插件机制支持25+中开发语言,可以很容易与gradle\maven\jenkins等工具进行集成,是非常流行的代码质量管控平台。通CheckStyle、findbugs等工具定位不同,SonarQube定位于平台,有完善的管理机制及强大的管理页面,并通过插件支持checkstyle及findbugs等既有的流..._sonar的客户端区别

元学习系列(六):神经图灵机详细分析_神经图灵机方法改进-程序员宅基地

文章浏览阅读3.4k次,点赞2次,收藏27次。神经图灵机是LSTM、GRU的改进版本,本质上依然包含一个外部记忆结构、可对记忆进行读写操作,主要针对读写操作进行了改进,或者说提出了一种新的读写操作思路。神经图灵机之所以叫这个名字是因为它通过深度学习模型模拟了图灵机,但是我觉得如果先去介绍图灵机的概念,就会搞得很混乱,所以这里主要从神经图灵机改进了LSTM的哪些方面入手进行讲解,同时,由于模型的结构比较复杂,为了让思路更清晰,这次也会分开几..._神经图灵机方法改进

【机器学习】机器学习模型迭代方法(Python)-程序员宅基地

文章浏览阅读2.8k次。一、模型迭代方法机器学习模型在实际应用的场景,通常要根据新增的数据下进行模型的迭代,常见的模型迭代方法有以下几种:1、全量数据重新训练一个模型,直接合并历史训练数据与新增的数据,模型直接离线学习全量数据,学习得到一个全新的模型。优缺点:这也是实际最为常见的模型迭代方式,通常模型效果也是最好的,但这样模型迭代比较耗时,资源耗费比较多,实时性较差,特别是在大数据场景更为困难;2、模型融合的方法,将旧模..._模型迭代

base64图片打成Zip包上传,以及服务端解压的简单实现_base64可以装换zip吗-程序员宅基地

文章浏览阅读2.3k次。1、前言上传图片一般采用异步上传的方式,但是异步上传带来不好的地方,就如果图片有改变或者删除,图片服务器端就会造成浪费。所以有时候就会和参数同步提交。笔者喜欢base64图片一起上传,但是图片过多时就会出现数据丢失等异常。因为tomcat的post请求默认是2M的长度限制。2、解决办法有两种:① 修改tomcat的servel.xml的配置文件,设置 maxPostSize=..._base64可以装换zip吗

Opencv自然场景文本识别系统(源码&教程)_opencv自然场景实时识别文字-程序员宅基地

文章浏览阅读1k次,点赞17次,收藏22次。Opencv自然场景文本识别系统(源码&教程)_opencv自然场景实时识别文字

随便推点

ESXi 快速复制虚拟机脚本_exsi6.7快速克隆centos-程序员宅基地

文章浏览阅读1.3k次。拷贝虚拟机文件时间比较长,因为虚拟机 flat 文件很大,所以要等。脚本完成后,以复制虚拟机文件夹。将以下脚本内容写入文件。_exsi6.7快速克隆centos

好友推荐—基于关系的java和spark代码实现_本关任务:使用 spark core 知识完成 " 好友推荐 " 的程序。-程序员宅基地

文章浏览阅读2k次。本文主要实现基于二度好友的推荐。数学公式参考于:http://blog.csdn.net/qq_14950717/article/details/52197565测试数据为自己随手画的关系图把图片整理成文本信息如下:a b c d e f yb c a f gc a b dd c a e h q re f h d af e a b gg h f bh e g i di j m n ..._本关任务:使用 spark core 知识完成 " 好友推荐 " 的程序。

南京大学-高级程序设计复习总结_南京大学高级程序设计-程序员宅基地

文章浏览阅读367次。南京大学高级程序设计期末复习总结,c++面向对象编程_南京大学高级程序设计

4.朴素贝叶斯分类器实现-matlab_朴素贝叶斯 matlab训练和测试输出-程序员宅基地

文章浏览阅读3.1k次,点赞2次,收藏12次。实现朴素贝叶斯分类器,并且根据李航《统计机器学习》第四章提供的数据训练与测试,结果与书中一致分别实现了朴素贝叶斯以及带有laplace平滑的朴素贝叶斯%书中例题实现朴素贝叶斯%特征1的取值集合A1=[1;2;3];%特征2的取值集合A2=[4;5;6];%S M LAValues={A1;A2};%Y的取值集合YValue=[-1;1];%数据集和T=[ 1,4,-1;..._朴素贝叶斯 matlab训练和测试输出

Markdown 文本换行_markdowntext 换行-程序员宅基地

文章浏览阅读1.6k次。Markdown 文本换行_markdowntext 换行

错误:0xC0000022 在运行 Microsoft Windows 非核心版本的计算机上,运行”slui.exe 0x2a 0xC0000022″以显示错误文本_错误: 0xc0000022 在运行 microsoft windows 非核心版本的计算机上,运行-程序员宅基地

文章浏览阅读6.7w次,点赞2次,收藏37次。win10 2016长期服务版激活错误解决方法:打开“注册表编辑器”;(Windows + R然后输入Regedit)修改SkipRearm的值为1:(在HKEY_LOCAL_MACHINE–》SOFTWARE–》Microsoft–》Windows NT–》CurrentVersion–》SoftwareProtectionPlatform里面,将SkipRearm的值修改为1)重..._错误: 0xc0000022 在运行 microsoft windows 非核心版本的计算机上,运行“slui.ex