#16578: 給延平的同仁 c++ AC 用string解


089487 (089487)

學校 : 國立臺灣師範大學附屬高級中學
編號 : 82069
來源 : [140.112.150.137]
最後登入時間 :
2024-10-03 16:28:37
a528. 大數排序 | From: [42.70.226.227] | 發表日期 : 2019-01-16 20:06

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	while(cin>>t)
	{
		int m=0,n;
		string s[t],a[t];
		for(int i=0;i<t;i++)
		{
			cin>>s[i]; 
		}
		if(t==2&&(s[0][0]=='-'&&s[1][0]==s[0][0])) 
		{
			sort(s,s+t);
			cout<<s[1]<<endl;
			cout<<s[0]<<endl;
			continue;
		}
		sort(s,s+t);
		for(int i=t-1;i>=0;i--)
		{
			for(int j=t-1;j>=0;j--)
			{
				if((s[i][0]=='-'&&s[i][0]==s[j][0])&&s[i].length()<s[j].length())
				{
					string p=s[i];
					s[i]=s[j];
					s[j]=p;
					//cout<<s[i]<<" "<<s[j]<<endl;
					continue;
				}
				else if((s[i][0]=='-'&&s[i][0]==s[j][0])&&s[i].length()>s[j].length()) continue;
				if(s[i][0]!='-'&&s[j][0]=='-')
				{
					string p=s[i];
					s[i]=s[j];
					s[j]=p;
					//cout<<s[i]<<" "<<s[j]<<endl;
					continue;
				}
				else if(s[i][0]=='-'&&s[j][0]!='-') continue;
				if(s[i].length()>s[j].length())
				{
					string p=s[i];
					s[i]=s[j];
					s[j]=p;
					//cout<<s[i]<<" "<<s[j]<<endl;
					continue;
				}
				if(s[i].length()==s[j].length())
				{
					//printf("1111\n");
					for(int l=0;l<s[i].length();l++)
					{
					//	printf("%c %c\n",s[i][0],s[j][0]);
						if(s[i][l]<s[j][l]&&(s[i][0]=='-'&&s[j][0]=='-')) 
						{
					//		printf("sa\n");
							string p=s[i];
							s[i]=s[j];
							s[j]=p;
							//rintf("break\n");
							break;
						}
						if(s[i][l]>s[j][l]) 
						{
							string p=s[i];
							s[i]=s[j];
							s[j]=p;
							//rintf("break\n");
							break;
						}
						else if(s[i][l]<s[j][l]) break;
					}
				}
			}
		}
		for(int i=0;i<t;i++) cout<<s[i]<<endl;
	}	
}
 
ZeroJudge Forum