Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

有比我的方法还愚蠢的么,不过一次提交就AC了,真高兴

Posted by earnAlive at 2014-04-14 20:12:09 on Problem 1007
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int inverse(char *str)
{
	int len=strlen(str);
	int i,j;
	int num=0;
	for(i=0;i<len-1;i++)
		for(j=i+1;j<len;j++)
		{
			if(str[i]>str[j])
			num++;
		}	
	return num;
}

int cmp(char *str1,char *str2)
{
	int num1=inverse(str1);
	int num2=inverse(str2);
	if(num1>num2)
	return 1;
	else if(num1<num2)
	return -1;
	else 
	return 0;	
}

int main()
{
	int m,n;
	int i,j;
	char *str[100];
	char *temp;
	scanf("%d%d",&n,&m);
	for(i=0;i<m;i++)
	{
		str[i]=(char *)malloc((n+1)*sizeof(char));
	}
	temp=(char *)malloc((n+1)*sizeof(char));
	for(i=0;i<m;i++)
	{
		scanf("%s",str[i]);
	}
	for(i=0;i<m;i++)
		for(j=0;j<m-i-1;j++)
		{
			if(cmp(str[j],str[j+1])>0)
			{
				temp=str[j];
				str[j]=str[j+1];
				str[j+1]=temp;
			}
		}
	for(i=0;i<m;i++)
	{
		printf("%s\n",str[i]);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator