期末复习-----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;
}
相关推荐
清泓y15 小时前
RAG 技术
算法·ai
阿慧今天瘦了嘛15 小时前
计算机组成原理概述:从硬件到软件的桥梁
计算机网络·算法
网站优化(SEO)专家16 小时前
SEO核心算法拆解:网站排名快速提升的武林秘籍!
算法·搜索引擎·网站排名·核心算法
惊讶的猫16 小时前
CLGSI
人工智能·算法·机器学习
ysa05103017 小时前
【板子】二分答案(最大最小?)
c++·笔记·算法·板子
科技之门17 小时前
百公里管网漏损分级定位实战方案2026
前端·人工智能·算法
木木子2217 小时前
# 猜数字游戏 — HarmonyOS交互逻辑与随机算法实现
算法·游戏·华为·交互·harmonyos
stolentime18 小时前
SP8549 MAIN75 - BST again题解
c++·算法·二叉树·深度优先·图论·记忆化搜索·组合数学
stolentime18 小时前
AT_pakencamp_2020_day1_k Gcd of Sum题解
c++·算法
2601_9561219719 小时前
map_计蒜客T1271 完美K倍子数组
c++·算法