期末复习-----4(PTA之前的编程测试题)

复制代码
#include<stdio.h>
#include<string.h>

struct student{
	char num[6];
	char name[11];
	int a,b,c;
	int sum;
};
int main()
{
	int n;
	int i;
	scanf("%d",&n);
	struct student stu[n],max;
	for(i=0;i<n;i++)
	{
		scanf("%s %s %d %d %d",stu[i].num,stu[i].name,&stu[i].a,&stu[i].b,&stu[i].c);
		stu[i].sum=stu[i].a+stu[i].b+stu[i].c;
		
		
		if(i==0)
		{
			max=stu[i];
		}
		
		if(stu[i].sum>max.sum)
		max=stu[i];
	}
	
	printf("%s %s %d",max.name,max.num,max.sum);
    return 0;
}

存在的问题:

1.怎么样用第一个是总分最高的学生,输出时候不只能输出最高总分,还能对应学生,小杜 if(i==0){max=stui}很好的解决了我的问题

2.循环中要不要加stui.还是知己stu,答案是需要加

想要用stui.sum,结构体中就必须要加入sum;

想要用max.name,主函数中定义初始化结构体的时候就需要struct student stun,max;想、连同max一起定义

找出最长的字符串

复制代码
#include<stdio.h>
#include<string.h>
int main()
{
	int n;
	scanf("%d",&n);
	char str[80],maxstr[80];
	int len=0,maxlen=0;
	for(int i=0;i<n;i++)
	{
		gets(str);
		len=strlen(str);
		if(len>maxlen)
		{
			maxlen=len;
			strcpy(maxstr,str);
		}
	}
	printf("%s",maxstr);
	return 0;
}

小杜写的:

复制代码
#include <stdio.h>
#include <string.h>
int main()
{
    int n;
    char p[81],max[81];
    scanf("%d",&n);
    scanf("%s",p);
    strcpy(max,p);
    for(int i=1; i<n; i++)
    {
        scanf("%s",p);
        if(strlen(p)>strlen(max))
            strcpy(max,p);
    }
    printf("The longest is: %s",max);
    return 0;
}
相关推荐
电科_银尘9 小时前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法
郝学胜-神的一滴12 小时前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
秋天的怀念_David12 小时前
算法一千零一夜 · 第三十三夜 · 跳表
java·程序人生·算法
Pniubi15 小时前
力扣21合并两个有序链表
算法·leetcode·职场和发展
AIgorithmGEEK15 小时前
std::unordered_map 与 std::unordered_set:接口、算法技巧与权衡 (速查)
c++·算法·哈希表·负载因子·桶·unordered_map·unordered_set
上海潭龙东海科技有限公司17 小时前
智慧堆场动态模型三维实时孪生重构智能管控
大数据·网络·数据库·算法·重构
阿庆_AI研发工程师19 小时前
Stanford CS336 Lecture 10:大语言模型推理与性能优化
算法
用户73416810354819 小时前
基于MindSpore的深度残差收缩网络程序
算法
Pniubi20 小时前
力扣239.滑动窗口最大值(优先队列)
算法·leetcode·职场和发展
青少儿编程课堂21 小时前
第十八届蓝桥杯青少组图形化真题 + 全国青少年信息素养大赛 + 电子学会图形化考级:校园泡泡堂大挑战解析
c++·python·算法·bfs·信息学竞赛