Problem C: 一切皆对象

Problem C: 一切皆对象

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 2822   Solved: 1874
[ Submit][ Status][ Web Board]

Description

一切都是对象 —— Everything is an object。 所以,现在定义一个类Thing,来描述世界上所有有名字的事物。该类只有构造函数、拷贝构造函数和析构函数,并具有一个字符串数据成员来存储其名字。

Input

输入只有1行,是一个没有空白符的字符串。

Output

见样例。

Sample Input

NAME

Sample Output

A thing without name is created!A thing without name is copied!A thing named by NAME is created!A thing named by NAME is copied!A thing named by NAME is erased!A thing named by NAME is erased!A thing without name is erased!A thing without name is erased!

HINT

Append Code

[ Submit][ Status][ Web Board]
#include <iostream>
using namespace std;
class Thing
{
public :
     string name;
     Thing():name( "without name" ){cout<< "A thing without name is created!" <<endl;}
     Thing(string name):name(name){cout<< "A thing named by " <<name<< " is created!" <<endl;}
     Thing( const Thing &t)
     {
         name = t.name;
         if (name == "without name" )
             cout<< "A thing " <<name<< " is copied!" <<endl;
         else
             cout<< "A thing named by " <<name<< " is copied!" <<endl;
     }
   ~Thing(){ if (name == "without name" ) cout<< "A thing " <<name<< " is erased!" <<endl; else cout<< "A thing named by " <<name<< " is erased!" <<endl;}
};
 
int main()
{
     string name;
     Thing Thing1, Thing2(Thing1);
     cin>>name;
     Thing Thing3(name);
     Thing Thing4(Thing3);
     return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 题目描述: 定义一个结构体,包含学生的姓名和成绩,输入多个学生的信息,按照成绩从高到低排序输出。 输入格式: 第一行输入一个整数n,表示学生的数量。 接下来n行,每行输入一个字符串和一个整数,表示学生的姓名和成绩。 输出格式: 按照成绩从高到低排序输出每个学生的姓名和成绩,每个学生的姓名和成绩之间用一个空格隔开。 如果有多个学生成绩相同,则按照姓名的字典序从小到大排序。 样例输入: 5 Tom 80 Jerry 90 Bob 80 Alice 85 John 90 样例输出: Jerry 90 John 90 Alice 85 Bob 80 Tom 80 解题思路: 本题需要用到结构体和排序,首先定义一个结构体,包含学生的姓名和成绩,然后输入多个学生的信息,将其存储在结构体数组中,最后按照成绩从高到低排序输出。 排序时需要自定义比较函数,先按照成绩从高到低排序,如果成绩相同,则按照姓名的字典序从小到大排序。 代码实现: ### 回答2: 这道题目要求我们按照学生的成绩从高到低排序,我们可以使用结构体来存储每个学生的信息,包括姓名、学号和成绩。然后我们使用冒泡排序算法来排序,要求按照成绩从高到低排列。 首先,我们需要定义一个结构体来存储学生的信息。假设我们需要存储的三个属性为name, id, score,那么我们可以这样定义结构体: ``` struct Student { char name[20]; char id[20]; int score; }; ``` 然后我们就可以定义一个数组用来存储多个学生的信息: ``` Student students[100]; ``` 接下来,我们需要读入每个学生的信息,存储到这个数组中。假设我们需要读入n个学生的信息,那么我们可以使用循环来读入每个学生的信息: ``` for (int i = 0; i < n; i++) { scanf("%s %s %d", students[i].name, students[i].id, &students[i].score); } ``` 接下来,我们使用冒泡排序算法来排序。对于每一次冒泡,我们将会通过比较相邻的两个元素,如果顺序不对就交换它们的位置。我们需要多次执行这个操作,直到所有元素都按照从大到小的顺序排序完毕。 ``` for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (students[j].score < students[j + 1].score) { Student temp = students[j]; students[j] = students[j + 1]; students[j + 1] = temp; } } } ``` 在最后,我们需要输出排序后的结果。假设我们想要输出每个学生的信息,那么我们可以使用循环来输出: ``` for (int i = 0; i < n; i++) { printf("%s %s %d\n", students[i].name, students[i].id, students[i].score); } ``` 这样就完成了问题d:结构体按成绩排序的实现。 ### 回答3: 这道题的主要任务是将一个包含学生信息的结构体按照成绩从高到低进行排序。我们可以使用冒泡排序或快速排序等算法来实现。 首先我们需要定义一个结构体,包含学生姓名、学号和成绩。 ```c++ struct Student{ string name; string num; int score; }; ``` 接下来,我们定义一个函数,该函数接收一个指向结构体数组的指针,还需要一个整数类型的形参来表示数组的长度,函数的返回值为 void。该函数会根据成绩从高到低排序结构体数组。 ```c++ void sortStudents(Student* arr, int len){ for(int i = 0; i < len - 1; i++){ for(int j = i+1; j < len; j++){ if(arr[i].score < arr[j].score){ Student temp; temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } } ``` 接下来,我们先在主函数中定义一个结构体数组并填充数据。 ```c++ int main(){ Student s[5] = { {"Tom", "001", 85}, {"Lily", "002", 90}, {"Bob", "003", 76}, {"Lucy", "004", 92}, {"John", "005", 88} }; // 调用函数进行排序 sortStudents(s, 5); // 输出排序结果 for(int i = 0; i < 5; i++){ cout << s[i].name << " " << s[i].num << " " << s[i].score << endl; } return 0; } ``` 运行程序后,我们可以看到按照成绩从高到低排序的结果输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值