1. 程式人生 > >PAT乙級1011-1020

PAT乙級1011-1020

1011A+B和C,

注意要使用longlong型別,因為兩個32位相加有可能超出範圍導致符號反轉

#include<iostream>
#include<vector>
using namespace std;
int main(int argc, char*argv[])
{
	
	int sum;
	cin >> sum;
	vector<vector<long long> >arrays;
	for (int i = 0; i < sum; i++)
	{
		vector<long long>a(3);
		cin >> a[0] >> a[1] >> a[2];
		arrays.push_back(a);
	}
	for (int i = 0; i < arrays.size(); i++)
	{
		cout << "Case #" << i + 1 << ": ";
		if (arrays[i][0] + arrays[i][1] > arrays[i][2])
			cout << "true" << endl;
		else
			cout << "false" << endl;
	}

	return 0;
}

1012,數字分類,

注意演算法庫中函式的使用,求和,求最大值,求積,注意格式化輸出一個小數

#include<iostream>
#include<vector>
#include<numeric>
#include<functional>
#include<iomanip>
#include<algorithm>
#include"stdlib.h"
using namespace std;
int main(int argc, char*argv[])
{
	
	int sum;
	cin >> sum;
	vector<int>nums(sum);
	for (int i = 0; i < sum; i++)
		cin >> nums[i];
	vector<vector<int> >clfs(5);
	for (auto i : nums)
	{
		if (i % 5 == 0)
		{
			if (i % 2 == 0)
				clfs[0].push_back(i);
		}
		else
		clfs[i % 5].push_back(i);
	}
	for (int j = 0; j < clfs.size(); j++)
	{
		int sig = 1;
		int sum = 0;
		double avr;
		if (clfs[j].size() != 0)
		{
			switch (j) {
			case 0:
				cout << accumulate(clfs[j].begin(), clfs[j].end(),0);
				break;
			case 1:
				for (auto i : clfs[j])
				{
					sum += sig*i;
					sig *= -1;
				}
				cout << " " << sum;
				break;
			case 2:
				cout << " " << clfs[j].size();
				break;
			case 3:
				avr = accumulate(clfs[j].begin(), clfs[j].end(), 0.0) / clfs[j].size();
				cout << " " << setiosflags(ios::fixed) << setprecision(1) << avr;
				break;
			case 4:
				cout << " " << *(max_element(clfs[j].begin(), clfs[j].end()));
				break;
			default:
				;
			}
		}
		else
		{
			if (j == 0)
				cout <<'N';
			else
				cout <<" N";
		}
	}
	return 0;
}

1013,數素數

注意按規則輸出數字

#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
bool is_prim(int a);
int main(int argc, char*argv[])
{
	int begin = 0;
	int end = 0;
	cin >> begin >> end;
	int i = 0;
	int num = 2;
	vector<int>prims;
	while (i <= end)
	{
		if (is_prim(num))
		{
			prims.push_back(num);
			i++;
		}
		num++;
	}
	cout << prims[begin - 1];
	for (int i = begin; i < end; i++)
	{
		if ((i - begin) % 10 == 9)
			cout << "\n";
		else
			cout << " ";
		cout << prims[i];
	}
	return 0;
}
bool is_prim(int a)
{
	for (int i = 2; i <= sqrt(a); i++)
		if (a%i == 0)
			return false;
	return true;
}

1014,福爾摩斯的約會,(非原創)

注意格式化輸出字串。注意char字元與int型別的ascii碼的對換

#include <iostream>
#include <cstdio>

using namespace std;

int main(){
	char Day[7][4] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
	int flag(0) , H(0) , M(0) ,cnt(0);
	char L,DAY='A';
	
	string A,B,C,D;
	cin >> A >> B >> C >> D;
	int MinF = A.length() < B.length() ? A.length() : B.length();
	int MinT = C.length() < D.length() ? C.length() : D.length();
	
	for(int i=0;i<MinF;i++){
		if(A[i] == B[i] && flag == 0 && A[i] >= 'A' && A[i]<= 'G'){
			L=A[i];
			flag = 1;
			continue;
		}
		
		if(A[i]==B[i] && A[i] - '0' >= 0 && A[i]-'0'<=9 && flag==1){
			H = A[i]-'0';
			break;
		}else if(A[i]==B[i]&&A[i]>='A'&&A[i]<='N'&&flag==1){
			H = A[i] - 'A' +10; //×¢Òâ 
			break;
		}
	}
	
	cnt = L- 'A';
	
	for(int i=0;i<MinT;i++){
		if(C[i]==D[i]&& ( (C[i]>='A'&&C[i]<='Z') || (C[i]>='a' && C[i]<='z') ) ){ //×¢Òâ 
			M=i;
			break;
		}
	}
	cout << Day[cnt];
	printf(" %02d:%02d",H,M);
	return 0;
} 

1015德才論

注意使用algrothm庫中的sort函式來進行自定義排序

注意使用scanf(在ide中是scanf_s)來解決執行超時問題,不要用iostream

#include<cstdio>
#include<vector>

#include<algorithm>
struct stu {
	int num;
	int de;
	int cai;
	int lei;
};
bool compare(stu a,stu b);
using namespace std;
int main(int argc, char*argv[])
{
	int N, L, H;
	scanf_s("%d %d %d", &N, &L, &H);
	vector<stu>stus(N);
	int M=0;
	for (int i = 0; i < N; i++)
	{
		//cin >> stus[i].num >> stus[i].de >> stus[i].cai;
		scanf_s("%d %d %d", &stus[i].num, &stus[i].de, &stus[i].cai);
		if (stus[i].de >= H&&stus[i].cai >= H)
			stus[i].lei = 0;
		else if (stus[i].de >= H&&stus[i].cai < H&&stus[i].cai >= L)
			stus[i].lei = 1;
		else if (stus[i].de < H&&stus[i].de >= L&&stus[i].cai < H&&stus[i].cai >= L&&stus[i].de>=stus[i].cai)
			stus[i].lei = 2;
		else if (stus[i].de < H&&stus[i].de >= L&&stus[i].cai >= L&&stus[i].de < stus[i].cai)
			stus[i].lei = 3;
		else
		{
			stus[i].lei = 4;
			M++;
		}
	}
	sort(&stus[0], &stus[0] + N, compare);
	printf("%d\n", N - M);
	for (int i = 0; i < N - M; i++)
		printf("%d %d %d\n", stus[i].num, stus[i].de, stus[i].cai);
		//cout << stus[i].num << " " << stus[i].de << " " << stus[i].cai << endl;
	return 0;
}
bool compare(stu a, stu b)
{
	if (a.lei < b.lei)
		return true;
	else if (a.lei>b.lei)
		return false;
	else {
		if (a.de + a.cai > b.de + b.cai)
			return true;
		else if (a.de + a.cai < b.de + b.cai)
			return false;
		else {
			if (a.de > b.de)
				return true;
			else if (a.de < b.de)
				return false;
			else
				return a.num < b.num;
		}
	}
}

1016部分A+B

採用求餘遍歷來找數字

#include<iostream>
using namespace std;
int main(int argc, char*argv[])
{
	long long A, B;
	int Da, Db;
	cin >> A >> Da >> B >> Db;
	int Pa = 0;
	int Pb = 0;
	int ina = 1;
	int inb = 1;
	while (A)
	{
		if (A % 10 == Da)
		{
			Pa += Da*ina;
			ina *= 10;
		}
		A /= 10;
	}
	while (B)
	{
		if (B % 10 == Db)
		{
			Pb += Db*inb;
			inb *= 10;
		}
		B /= 10;
	}
	cout << Pa + Pb;
	return 0;
}

1017A除以B(非原創)

注意最好不要分段計算,而是逐位的計算並輸出商數和餘數

#include <iostream>
#include <string>

using namespace std;

int main(){
	string s;
	cin >> s;
	int a;
	cin >> a;
	int len = s.length();
	int t = (s[0]-'0')/a;
	if((t!=0&&len>1) || len==1){
		cout << t;
	}
	int temp = (s[0]-'0') % a;
	for(int i=1;i<len;i++){
		t = (temp*10 + s[i]-'0')/a;
		cout << t;
		temp = (temp*10 + s[i]-'0')%a;
	}
	cout << " " << temp;
	return 0;
}

1018錘子剪刀布

我的辦法既愚蠢又笨拙,有更好的方法嗎,

#include<iostream>
using namespace std;
int main(int argc, char*argv[])
{
	int win(0), loss(0), ping(0);
	int jia[3] = { 0,0,0 };
	int yi[3] = { 0,0,0 };
	int num;
	cin >> num;
	char a, b;
	for (int i = 0; i < num; i++)
	{
		cin >> a >> b;
		switch (a - b)
		{
		case 0:
			ping++; break;
		case 1:
			yi[0]++; loss++; break;
		case -1:
			jia[0]++; win++; break;
		case 8:
			jia[2]++; win++; break;
		case -8:
			yi[2]++; loss++; break;
		case 7:
			yi[1]++; loss++; break;
		case -7:
			jia[1]++; win++; break;
		default:
				;
		}
	}
	cout << win << " " << ping << " " << loss << endl;
	cout << loss << " " << ping << " " << win << endl;
	int a1(0), a2(0);
	int b1(0), b2(0);
	for (int i = 0; i < 3; i++)
	{
		if (jia[i] > a1)
		{
			a2 = i; a1 = jia[i];
		}
		if (yi[i] > b1)
		{
			b2 = i; b1 = yi[i];
		}
	}
	switch (a2)
	{
	case 0:
		cout << "B "; break;
	case 1:
		cout << "C "; break;
	case 2:
		cout << "J "; break;
	default:;
	}
	switch (b2)
	{
	case 0:
		cout <<"B"; break;
	case 1:
		cout <<"C"; break;
	case 2:
		cout <<"J"; break;
	default:;
	}
}

1019數字黑洞(非原創)

注意stoi函式和string的insert函式的使用

#include <iostream> 
#include <algorithm>
using namespace std;
bool cmp(char a,char b){return a>b;}
int main(){
	string s;
	cin >> s;
	s.insert(0,4-s.length(),'0');
	do{
		string a=s,b=s;
		sort(a.begin(),a.end(),cmp);
		sort(b.begin(),b.end());
		int result = stoi(a)-stoi(b);
		s = to_string(result);
		s.insert(0,4-s.length(),'0');
		cout << a << " - " << b << " = " << s << endl;
	}while(s!="6174" && s!="0000");
	return 0;
}

1020月餅

注意輸出格式即可,還有就是最好避免強制型別轉換,而是一開始就用float

#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;
struct goods {
	float s;
	float price;
};
bool compare(goods a, goods b)
{
	return a.price / a.s > b.price / b.s;
}
int main(int argc, char*argv[])
{
	int sum, max;
	cin >> sum >> max;
	vector<goods>stock(sum);
	for (int i = 0; i < sum; i++)
		cin >> stock[i].s;
	for (int i = 0; i < sum; i++)
		cin >> stock[i].price;
	sort(stock.begin(), stock.end(), compare);
	float maxprofit = 0;
	for (int i = 0; i < sum; i++)
	{
		if (stock[i].s <= max)
			maxprofit += stock[i].price;
		else
		{
			maxprofit += stock[i].price / stock[i].s*max;
			break;
		}
		max -= stock[i].s;
	}
	cout <<setiosflags(ios::fixed)<<setprecision(2)<< maxprofit;
	return 0;
}