lsmCC 发表于 2023-10-11 15:31:15

改为c++

#include <stdio.h>
#include <algorithm>

typedef struct time
{
    int hour;
    int second;
    int minute;
}Time;
int cmp(Time t1,Time t2 )
{
    if(t1.hour!=t2.hour)
      return t1.hour<t2.hour;
    else
    {
            if(t1.minute!=t2.minute)
               return t1.minute<t2.minute;
         else
            return t1.second<t2.second;
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    Time t;
    for(int i=0;i<n;i++)
    {
      scanf("%d%d%d",&t.hour,&t.minute,&t.second);
    }
    std::sort(t,t+n,cmp);
    for(int i=0;i<n;i++)
    {
      printf("%d %d %d\n",t.hour,t.minute,t.second);
    }
    return 0;
}

isdkz 发表于 2023-10-11 15:31:34

这段代码是一个使用C++实现的时间排序程序。代码中定义了一个结构体Time,包含小时、分钟和秒三个成员变量。然后定义了一个比较函数cmp,用于比较两个Time类型的变量大小。在主函数中,首先读入n个时间,然后使用STL库中的sort函数对这些时间进行排序,最后按照格式输出排序后的结果。

具体实现上,sort函数的第三个参数是一个比较函数,用于定义排序规则。在本代码中,比较函数cmp先比较小时,如果小时相等则比较分钟,如果分钟也相等则比较秒。最后使用printf函数按照题目要求输出即可。

陶远航 发表于 2023-10-11 15:32:10

The code you provided is already in C++, but it uses some C-style syntax and functions. Here is a more C++-like version of your code, using std::vector instead of C-style arrays, std::cin and std::cout instead of scanf and printf, and a lambda function for the comparison:

#include <iostream>
#include <vector>
#include <algorithm>

struct Time {
    int hour;
    int minute;
    int second;
};

int main() {
    int n;
    std::cin >> n;

    std::vector<Time> times(n);
    for(int i = 0; i < n; i++) {
      std::cin >> times[ i].hour >> times[ i].minute >> times[ i].second;
    }

    std::sort(times.begin(), times.end(), [](const Time& t1, const Time& t2) {
      if(t1.hour != t2.hour)
            return t1.hour < t2.hour;
      else if(t1.minute != t2.minute)
            return t1.minute < t2.minute;
      else
            return t1.second < t2.second;
    });

    for(const auto& time : times) {
      std::cout << time.hour << \" \" << time.minute << \" \" << time.second << \"\
\";
    }

    return 0;
}


This code does the same thing as your original code: it reads a number of time records from the input, sorts them, and then prints them out. The sorting is done first by hour, then by minute, and finally by second.
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案

Mike_python小 发表于 2023-10-11 15:32:34

定义结构体数组并初始化示例(C++版本)

以下是你提供的代码的修正版本,使用C++语法进行修改和完善。


#include <iostream>
#include <algorithm>

struct Time {
    int hour;
    int minute;
    int second;
};

bool cmp(Time t1, Time t2) {
    if (t1.hour != t2.hour)
      return t1.hour < t2.hour;
    else {
      if (t1.minute != t2.minute)
            return t1.minute < t2.minute;
      else
            return t1.second < t2.second;
    }
}

int main() {
    int n;
    std::cin >> n;
   
    Time t;
   
    for (int i = 0; i < n; i++) {
      std::cin >> t.hour >> t.minute >> t.second;
    }
   
    std::sort(t, t + n, cmp);
   
    for (int i = 0; i < n; i++) {
      std::cout << t.hour << " " << t.minute << " " << t.second << std::endl;
    }
   
    return 0;
}


这个示例代码演示了如何使用C++语法定义一个包含时间信息的结构体Time,并创建一个结构体数组t[]来存储多个时间数据。通过std::sort()函数对结构体数组进行排序,使用自定义的比较函数cmp来指定排序规则。最后,使用循环遍历结构体数组并输出排序后的结果。

注意,在C++中我们使用#include <iostream>代替#include <stdio.h>,并使用std::cin和std::cout作为输入输出流,使用std::endl代替\n来换行。

希望这个示例代码对你有所帮助。如果还有其他问题,请继续提问。

球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}
页: [1]
查看完整版本: 改为c++