PAT b1031-b1035题解

120 篇文章 0 订阅
109 篇文章 0 订阅

1、b1031

#include <cstdio>
#include <cstring>

int w[20] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char change[15] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

int main(){
	int n;
	scanf("%d", &n);
	bool flag = true;
	char str[20];
	for(int i = 0; i < n; i++){
		scanf("%s", str);
		int j, last = 0;
		for(j = 0; j < 17; j++){
			if(!(str[j] >= '0' && str[j] <= '9'))
			break;
			last = last + (str[j] - '0') * w[j];
		}
		if(j < 17){
			flag = false;
			printf("%s\n", str);
		}
		else{
			if(change[last % 11] != str[17]){
				flag = false;
				printf("%s\n", str);
			}
		}
	}
	if(flag == true){
		printf("All passed\n");
	}
	return 0;
}

2、b1032

#include <cstdio>

const int MAXN = 100005;

int main(){
	int a[MAXN] = {0};
	int n;
	int index;
	int max = -1;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		int schoolNum, score;
		scanf("%d%d", &schoolNum, &score);
		a[schoolNum] += score;
	}
	for(int i = 0; i < 100005; i++){
		if(a[i] > max){
			max = a[i];
			index = i;
		}
	}
	printf("%d %d", index, max);
} 


3、b1033

#include <cstdio>
#include <cstring>

const int maxn = 100010;
bool hashTable[256];
char str[maxn];

int main(){
	memset(hashTable, true, sizeof(hashTable));
	gets(str);
	int len = strlen(str);
	for(int i = 0; i < len; i++){
		if(str[i] >= 'A' && str[i] <= 'Z'){
			str[i] = str[i] + 32;
		}
		hashTable[str[i]] = false;
	}
	gets(str);
	len = strlen(str);
	for(int i = 0; i < len; i++){
		if(str[i] >= 'A' && str[i] <= 'Z'){
			int low = str[i] + 32;
			if(hashTable[low] == true && hashTable['+'] == true){
				printf("%c", str[i]);
			}
		}
		else if(hashTable[str[i]] == true){
			printf("%c", str[i]);
		}
	}
	printf("\n");
	return 0;
} 

4、b1034

#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b){
	return b == 0 ? a : gcd(b, a % b);
}

struct Fraction{
	ll up, down;
}a, b;

Fraction reduction(Fraction result){
	if(result.down < 0){
		result.up = -result.up;
		result.down = -result.down;
	}
	if(result.up == 0){
		result.down = 1;
	}
	else{
		int d = gcd(abs(result.up), abs(result.down));
		result.up /= d;
		result.down /= d;
	}
	return result;
}

Fraction add(Fraction f1, Fraction f2){
	Fraction result;
	result.up = f1.up * f2.down + f2.up * f1.down;
	result.down = f1.down * f2.down;
	return reduction(result);
}

Fraction minu(Fraction f1, Fraction f2){
	Fraction result;
	result.up = f1.up * f2.down - f2.up * f1.down;
	result.down = f1.down * f2.down;
	return reduction(result);
}

Fraction multi(Fraction f1, Fraction f2){
	Fraction result;
	result.up = f1.up * f2.up;
	result.down = f1.down * f2.down;
	return reduction(result);
}

Fraction divide(Fraction f1, Fraction f2){
	Fraction result;
	result.up = f1.up * f2.down;
	result.down = f1.down * f2.up;
	return reduction(result);
}

void showResult(Fraction r){
	r = reduction(r);
	if(r.up < 0) printf("(");
	if(r.down == 1) printf("%lld", r.up);
	else if(abs(r.up) > r.down){
		printf("%lld %lld/%lld", r.up / r.down, abs(r.up) % r.down, r.down);
	}
	else{
		printf("%lld/%lld", r.up, r.down);
	}
	if(r.up < 0) printf(")");
}

int main(){
	scanf("%lld/%lld %lld/%lld", &a.up, &a.down, &b.up, &b.down);
	showResult(a);
	printf(" + ");
	showResult(b);
	printf(" = ");
	showResult(add(a, b));
	printf("\n");
	showResult(a);
	printf(" - ");
	showResult(b);
	printf(" = ");
	showResult(minu(a, b));
	printf("\n");
	showResult(a);
	printf(" * ");
	showResult(b);
	printf(" = ");
	showResult(multi(a, b));
	printf("\n");
	showResult(a);
	printf(" / ");
	showResult(b);
	printf(" = ");
	if(b.up == 0) printf("Inf");
	else showResult(divide(a, b));
	return 0; 
}

5、b1035

#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 111;
int origin[N], tempOri[N], changed[N];
int n;

bool isSame(int A[], int B[]){
	for(int i = 0; i < n; i++){
		if(A[i] != B[i]) return false;
	} 
	return true;
}

bool showArray(int A[]){
	for(int i = 0; i < n; i++){
		printf("%d", A[i]);
		if(i < n - 1) printf(" ");
	}
	printf("\n");
}

bool insertSort(){
	bool flag = false;
	for(int i = 1; i < n; i++){
		if(i != 1 && isSame(tempOri, changed)){
			flag = true;
		}
		int temp = tempOri[i], j = i;
		while(j > 0 && tempOri[j - 1] > temp){
			tempOri[j] = tempOri[j - 1];
			j--;
		}
		tempOri[j] = temp;
		if(flag == true){
			return true;
		}
	}
	return false;
}

void mergeSort(){
	bool flag = false;
	for(int step = 2; step / 2 <= n; step *= 2){
		if(step != 2 && isSame(tempOri, changed)){
			flag = true;
		}
		for(int i = 0; i < n; i += step){
			sort(tempOri + i, tempOri + min(i + step, n));
		}
		if(flag == true){
			showArray(tempOri);
			return;
		}
	}
} 

int main(){
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d", &origin[i]);
		tempOri[i] = origin[i];
	}
	for(int i = 0; i < n; i++){
		scanf("%d", &changed[i]);
	}
	if(insertSort()){
		printf("Insertion Sort\n");
		showArray(tempOri);
	}
	else{
		printf("Merge Sort\n");
		for(int i = 0; i < n; i++){
			tempOri[i] = origin[i];
		}
		mergeSort();
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值