PAT 乙级 ------ 1056 ~ 1060 组合数的和、数零壹、选择题、C语言竞赛、爱丁顿数附思路及代码

今天都四月三十号了,真快。 看帖子的你今天也要开心!PEACE & LOVE


1056 组合数的和
思路: 每个数字在十位,个位均出现(n-1)次,依此求和。

#include<stdio.h>

int main() {
	int n, tmp, sum = 0;
	
	scanf("%d", &n);
	for(int i=0; i<n; i++) {
		scanf("%d", &tmp);
		sum += (tmp*10+tmp)*(n-1);
	}
	printf("%d", sum);
	return 0;
}

在这里插入图片描述
1057 数零壹
思路: 通过getchar()对字符依次处理,是字母则求和(a是1),通过短除法确定 零 一 的个数。

#include<stdio.h>
#include<ctype.h>

int main() {
	int ch, sum, cnt0, cnt1;

	sum = cnt0 = cnt1 =0;
	while((ch=getchar()) != '\n') {
		if(isalpha(ch))
			sum += tolower(ch) - 'a'+1;
	}
	while(sum != 0) {
		if(sum%2 == 0)
			cnt0++;
		else
			cnt1++;
		sum /= 2;
	}
	printf("%d %d", cnt0, cnt1);
	return 0;
}

在这里插入图片描述
1058 选择题
思路: 定义结构体存储题目的分值,选项个数,正确答案。因为只有和答案一致才得分,且答案是abcd顺序排放,所以可以将正确选项个数和正确答案放在一个字符数组中,直接将学生答案和正确答案通过strcmp()比较即可。就是麻烦了些,没什么说的。

#include<stdio.h>
#include<string.h>
#define LEN 101

typedef struct _project {
	int grade;//分数
	int options;//选项个数
	int numErrors;//本题错误次数
	char currentOptions[15];//正确答案
} Project;

int main() {
	int n, m, max = 0;//n个学生, m道题, max错误次数最大值
	char answer[15];
	Project project[LEN]= {0};

	scanf("%d%d", &n, &m);
	for(int i=0; i<m; i++) {
		scanf("%d %d ", &project[i].grade, &project[i].options);
		fgets(project[i].currentOptions, 16, stdin);//选项之间有空格,不能用scanf
		project[i].currentOptions[ strlen( project[i].currentOptions )-1 ] = '\0';//若字符长度不够16,fgets会将末尾的\n也写入到字符串中。
	}
	for(int i=0; i<n; i++) {
		int ch, j, sum;
		for(j=0, sum=0; (ch=getchar())!='\n';) {
			if(ch == '(') {
				scanf("%[^)]", &answer);
				if( strcmp( answer, project[j].currentOptions ) == 0)//学生答案和正确答案相同
					sum += project[j].grade;
				else {
					project[j].numErrors++;
					max = project[j].numErrors > max ? project[j].numErrors : max;
				}
				j++;//下一题
			}
		}
		printf("%d\n", sum);
	}
	if(max == 0)//学生全对
		printf("Too simple");
	else {
		printf("%d", max);
		for(int i=0; i<m; i++) {
			if(project[i].numErrors == max)
				printf(" %d",i+1);
		}
	}
	return 0;
}

在这里插入图片描述

1059 C语言竞赛
思路: 存储排名情况后根据输入进行查找确定该学生排名。开始用字符数组顺序存储排名情况,之后顺序查找结果超时,只能用哈希表:将选手编号当数组下标,存储内容为排名的方式记录排名情况。空间换时间

#include<stdio.h>
#include<math.h>
#define LEN 10000

int isPrime(int x) {
	for(int i=2; i<=sqrt(x); i++)
		if(x%i == 0)
			return 0;
	return 1;
}

int main() {
	int n, k, tmp, rank[LEN]= {0};

	scanf("%d",&n);
	for(int i=1; i<=n; i++) {
		scanf("%d",&tmp);
		rank[tmp]=i;
	}
	scanf("%d", &k);
	for(int i=1; i<=k; i++) {
		scanf("%d",&tmp);
		printf("%.4d: ",tmp);
		if(!rank[tmp])
			puts("Are you kidding?");
		else {
			if(rank[tmp]==-1)
				puts("Checked");
			else if(rank[tmp]==1)
				puts("Mystery Award");
			else if(isPrime(rank[tmp]))
				puts("Minion");
			else
				puts("Chocolate");
			rank[tmp]=-1;
		}
	}
	return 0;
}

在这里插入图片描述
1060 爱丁顿数
思路: 将每天骑行的距离降序排列,从末尾遍历数组,对于某个arr[i],若arr[i] > i+1 (i从0开始),表明前边的值arr[0] ~ arr[i-1]都大于 i+1.,也就是满足爱丁顿数条件的最大值。

#include<stdio.h>
#define LEN 100000

int Partition(int arr[], int low, int high) {
	int pivot = arr[low];
	while(low < high) {
		while(arr[high]<=pivot && low<high)
			high--;
		arr[low] = arr[high];
		while(arr[low]>=pivot && low<high)
			low++;
		arr[high] = arr[low];
	}
	arr[low] = pivot;
	return low;
}

void QuickSort(int arr[], int low, int high) {
	if(low < high) {
		int pivotpos = Partition(arr, low, high);
		QuickSort(arr, low, pivotpos-1);
		QuickSort(arr,pivotpos+1, high);
	}
}

int main() {
	int n, i, arr[LEN];
	scanf("%d", &n);
	for(int i=0; i<n; i++)
		scanf("%d", &arr[i]);
	QuickSort(arr, 0, n-1);
	for(i=n-1; i>=0; i--) {
		if(arr[i] > i+1) {
			printf("%d", i+1);
			break;
		}
	}
	if(i == -1)
		putchar('0');
	return 0;
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值