#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;
}
}
不知道你这个问题是否已经解决, 如果还没有解决的话:问题:按如下格式输出时间
YY:MM:SS
//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具体实现类
#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主函数调用
#include <iostream>
#include "Time.h"
using namespace std;
int main()
{
Time t1;
Time t2(22,59,59);
cout << ++t2 << endl;
return 0;
}
运行结果:
//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具体实现类
#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主函数调用
#include <iostream>
#include "Time.h"
using namespace std;
int main()
{
Time t1;
Time t2(22,59,59);
cout << ++t2 << endl;
return 0;
}
运行结果: