c++中寻找一下错误

img


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
    int t,n,i,j;
    scanf("%d",&t);
    string f;
    string a[1005];
    while(t)
    {
        scanf("%d",&n);
        for(i=0;i<=n-1;i++)
        {
            cin>>a[i];
        }
        sort(a,a+n);
        f=a[n-1];
        for(j=n-2;j>=0;j--)
        {
            f=f+a[j];
        }
        cout<<f<<endl;
        t--;
    }
    
    return 0;
}

结果显示错误,请问哪里出错了

有帮助的话 采纳一下


#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main() {
    int T;
    cin >> T;
    
    while (T--) {
        int N;
        cin >> N;
        
        string nums;
        for (int i = 0; i < N; i++) {
            int num;
            cin >> num;
            nums += to_string(num);
        }
        
        sort(nums.begin(), nums.end(), greater<char>());
        cout << nums << endl;
    }
}
不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7562897
  • 这篇博客你也可以参考下:C++算法竞赛中避免大量输入输出导致超时方法广要
  • 除此之外, 这篇博客: c++自定义输出格式中的 按照要求格式输出常用方法有两种 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    问题:按如下格式输出时间
    YY:MM:SS


    • Time.h头文件
    //Time.h头文件
    #ifndef TIME_H_INCLUDED
    #define TIME_H_INCLUDED
    
    using namespace std;//必须要加
    class Time
    {
    private:
        int second;//秒
        int minute;//分
        int hour;//时
    public:
        Time();
        Time(int h,int m,int s);
        friend Time & operator++(Time &time);//返回值是一个对象,加个引用比较好
        friend ostream & operator<<(ostream & out,Time & time);//这里把重载的<<方法声明为友元函数是因为该方法需要访问Time类的私有属性
    };
    
    /*而且需要写成传入自身参数形式,因为这是友元函数重载一元运算符,不是成员函数方式重载的,
    成员函数这里就不用了传入参数,本身有this指针
    
    如果是二元运算符用友元函数重载二院运算符的话,则需要写成传入两个参数的形式
    成员函数重载二院运算符时就只需要传入一个参数就行,另一个是本身的this指针
    
    */
    
    
    
    
    #endif // TIME_H_INCLUDED
    
    • Time.cpp具体实现
    //Time.cpp具体实现类
    #include <iostream>
    #include <string>
    #include "Time.h"
    
    
    //必须使用c++2011版本编译器才能使用string的相关操作
    using namespace std;
    
    
    Time::Time()
        {
            this->second = 0;
            this->minute = 0;
            this->hour = 0;
        }
    
    Time::Time(int h,int m,int s)//需要加Time::
    {
        second = s;
        minute = m;
        hour = h;
    }
    
    Time & operator++(Time &time)//因为他不是成员函数,所以不要使用Time::(类名加作用域解析操作符)限定符。另外不要再定义中使用friend关键字
    {
        time.second += 1;//秒
        time.minute = time.minute + time.second/60;//分
        time.hour = time.hour + time.minute/60;//时
        if(time.second==60)
        {
            time.second = 0;//重置秒数
        }
        if(time.minute==60)
        {
            time.minute = 0;//重置分数
        }
        if(time.hour==24)//重置时
        {
            time.hour = 0;
        }
    
    
        return time;
    
    }
    
    //重载流操作符<< 实现自定义格式输出,另一种tostring方法见自己CSDN博客
    ostream & operator<<(ostream & out,Time & time)
    {
        out << time.hour << ":" << time.minute << ":" << time.second;
        return out;
    }
    
    • main.cpp主函数调用
    //main.cpp主函数调用
    #include <iostream>
    #include "Time.h"
    using namespace std;
    
    int main()
    {
        Time t1;
        Time t2(22,59,59);
        cout << ++t2 << endl;
        return 0;
    }
    

    运行结果:
    在这里插入图片描述


    • Time.h头文件
    //Time.h头文件
    #ifndef TIME_H_INCLUDED
    #define TIME_H_INCLUDED
    
    using namespace std;//必须要加
    class Time
    {
    private:
        int second;//秒
        int minute;//分
        int hour;//时
    public:
        Time();
        Time(int h,int m,int s);
        friend string operator++(Time &time);//返回值是一个对象,加个引用比较好,
    };
    
    /*而且需要写成传入自身参数形式,因为这是友元函数重载一元运算符,不是成员函数方式重载的,
    成员函数这里就不用了传入参数,本身有this指针
    
    如果是二元运算符用友元函数重载二院运算符的话,则需要写成传入两个参数的形式
    成员函数重载二院运算符时就只需要传入一个参数就行,另一个是本身的this指针
    
    */
    
    
    
    
    #endif // TIME_H_INCLUDED
    
    • Time.cpp具体实现类
    //Time.cpp具体实现类
    #include <iostream>
    #include <string>
    #include "Time.h"
    
    
    //必须使用c++2011版本编译器才能使用string的相关操作
    using namespace std;
    
    
    Time::Time()
        {
            this->second = 0;
            this->minute = 0;
            this->hour = 0;
        }
    
    Time::Time(int h,int m,int s)//需要加Time::
    {
        second = s;
        minute = m;
        hour = h;
    }
    
    string operator++(Time &time)//因为他不是成员函数,所以不要使用Time::(类名加作用域解析操作符)限定符。另外不要再定义中使用friend关键字
    {
        time.second += 1;//秒
        time.minute = time.minute + time.second/60;//分
        time.hour = time.hour + time.minute/60;//时
        if(time.second==60)
        {
            time.second = 0;//重置秒数
        }
        if(time.minute==60)
        {
            time.minute = 0;//重置分数
        }
        if(time.hour==24)//重置时
        {
            time.hour = 0;
        }
        string str = std::to_string(time.hour)  + ":" + std::to_string(time.minute) + ":" + std::to_string(time.second);
    
        return str;//也可以直接返回一个Time对象就是
    
    }
    
    • main.cpp主函数调用
    //main.cpp主函数调用
    #include <iostream>
    #include "Time.h"
    using namespace std;
    
    int main()
    {
        Time t1;
        Time t2(22,59,59);
        cout << ++t2 << endl;
        return 0;
    }
    
    

    运行结果:
    在这里插入图片描述

  • 您还可以看一下 malloc老师的c++游戏逆向内存封包课程中的 逆向体系课程介绍小节, 巩固相关知识点

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^