《算法笔记》练习(二)

  • codeup 5901

题目描述

读入一串字符,判断是否是回文串。“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。

输入

一行字符串,长度不超过255。

输出

如果是回文串,输出“YES”,否则输出“NO”。

样例输入

12321

样例输出

YES
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=256;
int main(){
	char str[maxn];
	gets(str);
	int len = strlen(str);
	bool flag = true;
	//判断第i个 字符和第len-1-i个字符是否相同,奇数个字符时中间的字符不需要判断 
	for(int i=0;i<=len/2;i++){
		if(str[i] != str[len-1-i]){
			flag = false;
			break;
		}
	}
	if(flag == true){
		printf("true\n");
	}
	else printf("false\n");
	return 0;
} 

又是0.5的错,在线疯魔

  • PAT1009B 

给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。

输入格式:

测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用 1 个空格分开,输入保证句子末尾没有多余的空格。

输出格式:

每个测试用例的输出占一行,输出倒序后的句子。

输入样例:

Hello World Here I Come

输出样例:

Come I Here World Hello

 

#include<iostream>
#include<string>
using namespace std;
int main() {
	string worlds[81];
	string world;
	getline(cin, world);
	unsigned int i = 0,j=0;
	int k = 0;
	for (;i<world.length(); i++) {
		for (j=0;; j++) {
			if (world[j + i] == ' '||world[j + i] == '\0') {
				worlds[k++] = world.substr(i, j);
				break;
			}
		}
		i = i + j;
	}
	for (;(k-1) > 0; k--) {
		cout << worlds[k-1] << " ";
	}
	cout << worlds[0];
	return 0;
}
  •  PAT1025A

 

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
//小组数不超过100 ,每组不超过300人 
const int maxn = 30010;
//每个学生有id,成绩,所属小组号码,小组内排名,整体排名 
struct Student{
	//段错误 
	//	int id;
	char id[15]; 
	int score;
	int group;
	int final;
	int local;
}stu[maxn];
//进行比较 
bool cmp(Student stu1,Student stu2){
	if(stu1.score != stu2.score){
		//分数从高到低排序 
		return stu1.score>stu2.score;
	}
	//成绩相同,id号从小到大排序 
	else return stu1.id<stu2.id;
} 
int main(){
	//学校数量 
	int n;
	cin>>n;
	//总人数
	int number=0; 
	for(int i=0;i<n;i++){
		//每组人数
		int k;
		cin>>k;
		cin>>stu[number].id;
		cin>>stu[number].score;
		for(int j=0;j<k;j++){
			cin>>stu[number+1].id;
			cin>>stu[number+1].score;
			//当学号不同,增加总人数,并记录该学号学生的小组id 
			if(stu[number].id != stu[number-1].id){
				stu[number].group = i+1;
				number++;
			}
		}
		//对小组内同学进行排序
		sort(stu+number-k,stu+number,cmp);
	/*	for(int j=0;j<k;j++){
			stu[j].local = j+1;
		}
	*/
		//排序方法错了改: 
		stu[number-k].local = 1;
		//从组内第二名开始 
		for(int j=number-k+1;j<number;j++){
			//如果成绩相同
			if(stu[j].score == stu[j-1].score){
				stu[j].local = stu[j-1].local;
			} 
			else stu[j].local = j+1;
		}
	}
	//输出总人数
	cout<<number<<endl;
	//所有学生成绩排序 
	sort(stu,stu+number,cmp);
	stu[i].final=1;
	for(int i=1;i<number;i++){
		if(stu[i].score == stu[i-1].score){
			stu[i].final = stu[i-1].final;
		}
		else stu[i].final = i+1;
	}
	for(int i=0;i<number;i++){
		printf("%s %d %d %d\n",stu[i].id,stu[i].final,stu[i].group,stu[i].local);
	}
	return 0;
} 

 答案错误,啊,疯了,3分

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 30010;
//学生结构体 
struct Student{
	char id[15];
	int score;
	int group;
	int local;
	int beAll;
}stu[maxn];
//判断方法 
bool cmp(Student stu1,Student stu2){
	if(stu1.score != stu2.score) 
		return stu1.score>stu2.score;
	else return strcmp(stu1.id,stu2.id)<0;
} 

int main(){
	int n,k;
	int num = 0;
	cin>>n;
	for(int i =0;i<n;i++){
		cin>>k;
		
		//将学生信息载入数组 
		for(int j=0;j<k;j++){
			
			cin>>stu[num].id;
			cin>>stu[num].score;
			stu[num].group = i+1;
			++num; 
		}
		//组内排名 
		sort(stu+num-k,stu+num,cmp);
		//组内排名
		stu[num-k].local = 1;
		int l=2; 
		for(int m = num-k+1;m<num;m++){
			if(stu[m].score == stu[m-1].score){
				stu[m].local = stu[m-1].local;
				l++;
			}
			else {
				stu[m].local = l;
				l++;
			}
		} 
		
	}
	sort(stu,stu+num,cmp); 
	stu[0].beAll = 1;
	int l =2;
	for(int m=1;m<num;m++){
		if(stu[m].score == stu[m-1].score){
			stu[m].beAll = stu[m-1].beAll;
			l++;
		}
		else {
			stu[m].beAll = l; 
			l++;
		}
	} 
	cout<<num<<endl;
	for(int m=0;m<num;m++){
		cout<<stu[m].id<<" "<<stu[m].beAll<<" "<<stu[m].group<<" "<<stu[m].local<<endl;
	}
	return 0;
}
  •  散列练习

#include<cstdio>
#include<iostream>
using namespace std;
//给出N个字符串(三个大写字母组成),再给出M个查询字符串,问每个查询字符串在字符串中的出现次数 
char s[100][5],temp[5];
int hashTable[26*26*26+10];
//将字符转换为26位进制数字
int hashFun(char s[],int len){
	int key = 0;
	for(int i=0;i<len;i++){
		key = key*26+(s[i]-'A');
	}
	return key; 
}
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>s[i];
		int key = hashFun(s[i],3);
		hashTable[key]++;
	}
	for(int i=0;i<m;i++){
		cin>>temp;
		int id =hashFun(temp,3);
		cout<<hashTable[id];
	}
	return 0;
} 
  • PAT1020B

月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。

注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么我们最大收益策略应该是卖出全部 15 万吨第 2 种月饼、以及 5 万吨第 3 种月饼,获得 72 + 45/2 = 94.5(亿元)。

输入格式:

每个输入包含一个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N 表示月饼的种类数、以及不超过 500(以万吨为单位)的正整数 D 表示市场最大需求量。随后一行给出 N 个正数表示每种月饼的库存量(以万吨为单位);最后一行给出 N 个正数表示每种月饼的总售价(以亿元为单位)。数字间以空格分隔。

输出格式:

对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后 2 位。

输入样例:

3 20
18 15 10
75 72 45

输出样例:

94.50

 

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1010;
struct Moon{
	//库存量 
	double cnt;
	//零售价
	double price;
	//总售价
	double sale; 
}cake[maxn];

//比较单价进行排序 
bool cmp(Moon a,Moon b){
	if(a.price != b.price){
		return a.price>b.price;
	}
	else return a.cnt>b.cnt;
}

int main(){
	//n为月饼的种类数,d为市场最大需求 
	int n,d;
	cin>>n>>d;
	for(int i=0;i<n;i++){
		cin>>cake[i].cnt;
	}
	for(int i=0;i<n;i++){
		cin>>cake[i].sale;
		cake[i].price = cake[i].sale/cake[i].cnt;
	}
	sort(cake,cake+n,cmp);
	//出错,检查 
/*	for(int i=0;i<n;i++){
		cout<<cake[i].sale;
	} 
	cout<<endl;
*/
	//int key=0;
	double key = 0;
	for(int i=0;i<n&&d>0;i++){
		if(cake[i].cnt <= d){
			key = key+cake[i].sale;
		}
		else
			key = key+cake[i].price*d;
		d = d-cake[i].cnt; 
	}
	//	cout<<key;
	printf("%.2f",key);
	return 0; 
} 

哇,一些小问题,比如说是key的输出两位小数,else的相加内容在原来的基础上相加,翻来覆去的修改

点赞