栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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


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

#include

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


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

#include
#include

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
#include
#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
		scanf("%d %d ", &project[i].grade, &project[i].options);
		fgets(project[i].currentOptions, 16, stdin);//选项之间有空格,不能用scanf
		project[i].currentOptions[ strlen( project[i].currentOptions )-1 ] = '';//若字符长度不够16,fgets会将末尾的n也写入到字符串中。
	}
	for(int i=0; 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("%dn", sum);
	}
	if(max == 0)//学生全对
		printf("Too simple");
	else {
		printf("%d", max);
		for(int i=0; i
			if(project[i].numErrors == max)
				printf(" %d",i+1);
		}
	}
	return 0;
}

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

#include
#include
#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
#define LEN 100000

int Partition(int arr[], int low, int high) {
	int pivot = arr[low];
	while(low < high) {
		while(arr[high]<=pivot && low=pivot && low
	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=0; i--) {
		if(arr[i] > i+1) {
			printf("%d", i+1);
			break;
		}
	}
	if(i == -1)
		putchar('0');
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/846814.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号