结构体&冒泡排序——1147最高分数

求分数最高学生名字

本题我使用了结构体

结构体变量为数组a,每个元素为student类型

在进行过对结构体成员赋值后

之后进行排序

我参考了一下课本

也使用了冒泡排序

但冒泡排序的时间复杂度较高,可能数据过大后需要对程序进行优化

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;

struct student
{
    string name;
    int score;
} a[100];

int main()
{
    int n;
    int i,j;

    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i].score;
        cin>>a[i].name;
    }

    for(i=0; i<n-1; i++)
        for(j=0; j<n-1-i; j++)
            if(a[j].score<a[j+1].score)
                swap(a[j],a[j+1]);

    cout<<a[0].name<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/-Iris-/p/12250659.html

时间: 2024-08-28 16:40:02

结构体&冒泡排序——1147最高分数的相关文章

结构体数组排序

using System;using System.Collections;using System.Collections.Generic;using System.Text; namespace 结构体冒泡排序{    class Program    {        struct student        {            public string name;            public int age;        }        static void Ma

c#---部分;把数组或者结构体存入集合里,然后再从结合中取出之后,输出;

1.输入班级人数,统计每个人的姓名,性别,年龄:集合与数组 //Console.Write("请输入班级人数:"); //int a = int.Parse(Console.ReadLine()); //ArrayList al = new ArrayList(); //for (int i = 0; i < a;i++ ) //{ // string [] name =new string[3]; // Console.Write("请输入第{0}个人的姓名:&quo

例题:输入学生的各项资料,然后根据学生的分数,重新排序。重新复习结构体,集合,数组,for循环,冒泡排序,水平符的使用。

class Program { struct student     //定义一个student的结构体 { public string name;   //基本格式 public int code; public int age; public int fenshu; } static void Main(string[] args) { while (true) { ArrayList al = new ArrayList();  //定义一个新的集合 Console.Write("请输入人

代码案例(结构体,函数指针,指针函数,冒泡排序) 修改

#import <Foundation/Foundation.h> typedef struct {     char name[20];     int age;     float score; }Stu; //建立字符串和函数之间的一一对应关系. typedef BOOL (*PStu) (Stu stu1,Stu stu2) ; typedef struct nameFunctionPair{     char name[20]; //存储函数对应的字符串     PStu funct

c#部分---用结构体的题目- //请输入班级人数,输入每个人的学号,姓名,和语文分数、数学分数和英语分数(要求使用结构体)

//请输入班级人数,输入每个人的学号,姓名,和语文分数.数学分数和英语分数(要求使用结构体), //求班级里两个语文分数是最高分的学生的所有信息:数学分数是最高分的两个学生的所有信息:英语平均分 建立结构体 class Program { struct student { public int xuehao ; public string name; public score score; } struct score { public double yufen; public double s

代码案例(结构体,函数指针,指针函数,冒泡排序)

typedef struct {     char name[20];     int age;     float score; }Stu; #import <Foundation/Foundation.h> //姓名升序 void sortByName(Stu *p , int count ) {     for (int i = 0 ; i < count -1; i ++) {         for (int j= 0 ; j < count -1-i; j ++) {

c#例题:输入学生的学号,姓名,分数,然后根据分数进行排序再输出最终结果。重点学习用结构体定义数组的表达方式

class Program { struct student //定义一个结构体 { public int code; public string name; public int fenshu; } static void Main(string[] args) { Console.Write("请输入人数:"); int renshu = Convert.ToInt32(Console .ReadLine ()); student[] r = new student[renshu]

C#例题:输入学生学号,姓名,分数,然后根据分数进行排序。这个题是用集合和结构体来做,与上一题不同。掌握基础知识很重要

class Program { struct student { public string name; public int code; public int age; public double fenshu; } static void Main(string[] args) { ArrayList al = new ArrayList(); //定义一个新的集合 Console.Write("请输入人数:"); int renshu = Convert.ToInt32(Cons

使用结构体变量存储学生姓名和课程成绩,计算平均分及查找不及格同学姓名及分数

1 struct Student //声明Student类型结构体变量 2 { 3 char name[20]; 4 int score[5]; 5 }; 6 typedef struct Student Stu; 7 8 void main() 9 { 10 11 Stu stu[3]; 定义Student类型变量数组 12 int i,j; 13 for(i=0;i<3;i++) //输入学生姓名和成绩 14 { 15 printf("please input student[%d]'