期末复习-----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;
}
相关推荐
一只齐刘海的猫1 天前
【Leetcode】找到字符串中所有字母异位词
算法·leetcode·职场和发展
海清河晏1111 天前
数据结构 | 八大排序
数据结构·算法·排序算法
IronMurphy1 天前
【算法五十七】146. LRU 缓存
算法·缓存
凌波粒1 天前
LeetCode--108.将有序数组转换为二叉搜索树(二叉树)
算法·leetcode·职场和发展
liulilittle1 天前
KCC:在 BBR 思路上的一次探索
网络·tcp/ip·算法·bbr·通信·拥塞控制·kcc
浦信仿真大讲堂1 天前
达索系统SIMULIA Abaqus 2026接触和约束的增强新功能介绍
人工智能·python·算法·仿真软件·达索软件
点云侠1 天前
PCL 生成三棱锥点云
c++·算法·最小二乘法
兰令水1 天前
leecodecode【面试150】【2026.6.13打卡-java版本】
java·算法·leetcode
临沂堇1 天前
刷题日志 | Leetcode Hot 100 哈希
算法·leetcode·哈希算法