PAT B1032 挖掘机技术哪家强

本文提供了一段解决PATB1032问题的C语言代码实现,该程序通过读取输入数据来确定哪所学校在挖掘机技术方面最强。通过遍历和比较各个学校的数据,最终输出得分最高的学校及其分数。

PAT B1032 挖掘机技术哪家强

#include<stdio.h>
#define MAXN 100010//没有0号,遍历枚举最大值时需要注意 
//我一开始这里写的是#define maxn 100010,就报错,看来只有大写MAXN才行 
//书上的写法是 const int maxn=100010;
//#define LOCAL
int a[MAXN]={0};//是可以定义的同时赋值的  
int main(){
	#ifdef LOCAL
	freopen("B1032data.in","r",stdin);
	freopen("B1032data.out","w",stdout);
	#endif 
	int n=0,tempn=0,tempa=0,max,maxn;
	scanf("%d",&n);
	while(n--){
		scanf("%d%d",&tempn,&tempa);
		a[tempn]+=tempa;//因为学校号是从1开始连续编号的,所以肯定不会超过10000个,只能这样理解了 
	}
	max=a[1];
	maxn=1;//假设第一个学校是得分最大的 
	for(int i=1;i<100001;i++){
		if(a[i]>max)
			{
				max=a[i];
			    maxn=i; //注意这里一定要加花括号,是在一个if钟赋值的,一开始错误就出在这里,后来才ac了 
			}
	}
	printf("%d %d",maxn,max); 
}


### 关于 PAT 乙级真题解析 以下是针对您提到的几道 PAT 乙级真题的具体解答和思路: --- #### 题目 1040:统计字符串中的 PAT 数量 此题的核心在于找到所有可能组成单词 "PAT" 的字符组合。通过遍历输入字符串,记录 P 和 A 出现的位置,并计算 T 后面有多少种合法的 PA 组合。 ```cpp #include <iostream> #include <vector> using namespace std; int main() { string s; cin >> s; long long count_P = 0, count_PA = 0, result = 0; // 使用 long long 避免溢出 for (char c : s) { if (c == 'P') { count_P++; } else if (c == 'A') { count_PA += count_P; } else if (c == 'T') { result += count_PA; } } cout << result % 1000000007; // 取模防止过大 return 0; } ``` 上述代码实现了动态规划的思想[^1],逐步累积符合条件的子序列数量。 --- #### 题目 1026:程序运行时间 该问题主要涉及浮点数处理以及四舍五入操作。可以通过简单的数学运算实现精确的结果转换。 ```cpp #include <cstdio> int main(){ double timeInSeconds; scanf("%lf", &timeInSeconds); printf("%.0f\n", (timeInSeconds / 3600.0 + 0.5)); // 加上偏移量完成四舍五入 return 0; } ``` 这里采用了 `(double + 0.5)` 技巧来简化逻辑[^2],从而达到标准输出的要求。 --- #### 题目 1032挖掘机组队比赛成绩汇总 这是一道典型的分数累加与比较题目。需要读取多组数据并维护每所学校的成绩表,最后找出最高分对应的学校编号。 ```cpp #include <bits/stdc++.h> using namespace std; struct SchoolScore{ int id; int score; }; bool cmp(const SchoolScore& a,const SchoolScore& b){ return a.score > b.score; } int main(){ vector<SchoolScore> schools(101,{0,0}); // 假设最多有100所不同学校参与竞赛 int n,m,tempId,tempScore; cin>>n>>m; while(m--){ cin>>tempScore>>tempId; schools[tempId].id=tempId; schools[tempId].score+=tempScore; } sort(schools.begin(),schools.end(),cmp); cout<<schools[0].id<<" "<<schools[0].score; return 0; } ``` 这段代码利用结构体存储信息,并借助 STL 排序功能快速定位最优解[^3]。 --- #### 题目 1029:检测损坏按键 本题要求识别哪些字母无法正常打印出来。可以采用集合去重的方式收集异常字符集。 ```cpp #include<stdio.h> #define MAX_LEN 1000 void findBrokenKeys(char* typed,char* expected){ char brokenSet[MAX_LEN]={}; bool isUpper=false; for(int i=0;i<strlen(typed)||i<strlen(expected);i++){ if(i>=strlen(typed)){ addIfNotExists(brokenSet,toupper(expected[i])); } else if(i>=strlen(expected)){ addIfNotExists(brokenSet,toupper(typed[i])); } else if(typed[i]!=expected[i]){ addIfNotExists(brokenSet,toupper(typed[i])); addIfNotExists(brokenSet,toupper(expected[i])); } } printUniqueChars(brokenSet); } // 辅助函数定义省略... ``` 以上片段展示了如何逐字对比两串文本差异之处[^4],并通过辅助方法整理最终答案。 --- ### 总结 这些示例覆盖了多种基础算法技术要点,包括但不限于字符串匹配、数值变换、数组管理及错误排查等领域知识点。希望它们能帮助理解相关概念并提升实战能力!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值