期末复习-----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=stu[i]}很好的解决了我的问题

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

想要用stu[i].sum,结构体中就必须要加入sum;

想要用max.name,主函数中定义初始化结构体的时候就需要struct student stu[n],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;
}
相关推荐
黄昏ivi1 小时前
电力系统最小惯性常数解析
算法
独家回忆3641 小时前
每日算法-250425
算法
烁3471 小时前
每日一题(小白)模拟娱乐篇33
java·开发语言·算法
Demons_kirit2 小时前
LeetCode 2799、2840题解
算法·leetcode·职场和发展
软行2 小时前
LeetCode 每日一题 2845. 统计趣味子数组的数目
数据结构·c++·算法·leetcode
永远在Debug的小殿下2 小时前
查找函数【C++】
数据结构·算法
我想进大厂2 小时前
图论---染色法(判断是否为二分图)
数据结构·c++·算法·深度优先·图论
油泼辣子多加2 小时前
【风控】稳定性指标PSI
人工智能·算法·金融
雾月553 小时前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
知来者逆5 小时前
计算机视觉——速度与精度的完美结合的实时目标检测算法RF-DETR详解
图像处理·人工智能·深度学习·算法·目标检测·计算机视觉·rf-detr