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

一定要注意,声明字符数组时长度为51

Posted by freshvictor at 2010-06-20 17:33:47 on Problem 1007
#include <iostream>
using namespace std;

int GetInverse(char *str)
{
	int rst = 0;
	int l = (int)strlen(str);
	for (int i = 0; i < l; i++)
		for (int j = i + 1; j < l; j++)
			if (str[i] > str[j])
				rst++;
	return rst;
}

int sort_function(const void *a, const void *b)
{
	if (((int*)a)[1] < ((int*)b)[1])
		return -1;
	else if (((int*)a)[1] > ((int*)b)[1])
		return 1;
	else
		return 0;
}

int main()
{
	int n, m, sort[100][2];
	char src[100][51];    //这里一定要声明51,否则Output Limit Exceeded	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> src[i];
		sort[i][0] = i;
		sort[i][1] = GetInverse(src[i]);
	}
	qsort((void*)sort, m, sizeof(sort[0]), sort_function);
	for (int i = 0; i < m; i++)
		cout << src[sort[i][0]] << endl;
	return 1;
}

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