一、十四届C/C++程序设计C组试题
十四届程序C组试题A#include <stdio.h>
int main()
{long long sum = 0;int n = 20230408;int i = 0;// 累加从1到n的所有整数for (i = 1; i <= n; i++){sum += i;}// 输出结果printf("%lld\n", sum);return 0;
}
//十四届程序C组试题B
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<time.h>// 时间字符串解析为结构体 tm
void parseTime(char* timeString, struct tm* timeStruct) {sscanf(timeString, "%d-%d-%d %d:%d:%d",&timeStruct->tm_year, &timeStruct->tm_mon, &timeStruct->tm_mday,&timeStruct->tm_hour, &timeStruct->tm_min, &timeStruct->tm_sec);// tm_year表示的是自1900年以来的年数,需要减去1900timeStruct->tm_year -= 1900;// tm_mon表示的是0-11的月份,需要减去1timeStruct->tm_mon -= 1;
}// 每一对相邻的上下班打卡之间的时间差
int calculateTimeDifference(char* time1, char* time2) {struct tm start, end;// 解析时间字符串为结构体 tmparseTime(time1, &start);parseTime(time2, &end);// 使用 mktime 将 tm 结构体转换为时间戳time_t startTime = mktime(&start);time_t endTime = mktime(&end);// 计算时间差return difftime(endTime, startTime);
}int main() {// 打卡记录数组char* punchRecords[] = {"2022-01-01 07:58:02","2022-01-01 12:00:05","2022-01-01 16:01:35","2022-01-02 00:20:05"};int numRecords = sizeof(punchRecords) / sizeof(punchRecords[0]);// 按照时间顺序对打卡记录进行排序for (int i = 0; i < numRecords - 1; i++) {for (int j = 0; j < numRecords - i - 1; j++) {if (strcmp(punchRecords[j], punchRecords[j + 1]) > 0) {// 交换记录char* temp = punchRecords[j];punchRecords[j] = punchRecords[j + 1];punchRecords[j + 1] = temp;}}}// 计算总工作时长int totalWorkDuration = 0;for (int i = 0; i < numRecords - 1; i += 2) {totalWorkDuration += calculateTimeDifference(punchRecords[i], punchRecords[i + 1]);}// 输出总工作时长printf("小蓝在2022年度的总工作时长是%d秒。\n", totalWorkDuration);return 0;
}
十四届程序C组试题C#include <stdio.h>
int main() {int n; // 事件数量scanf("%d", &n);int A[n], B[n], C[n]; // 存储每个事件中的A、B、C值int maxEvents = -1; // 最多发生的事件数量int X = 0, Y = 0, Z = 0; // 初始士兵数量for (int i = 0; i < n; ++i) {scanf("%d %d %d", &A[i], &B[i], &C[i]);// 计算每个国家的士兵数量X += A[i];Y += B[i];Z += C[i];// 判断是否有国家获胜if ((X > Y + Z) || (Y > X + Z) || (Z > X + Y)) {// 更新最多发生的事件数量maxEvents = i + 1;}}printf("%d\n", maxEvents);return 0;
}