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

Re:帮我看一下程序为什么总是Runtime Error

Posted by ths at 2007-05-24 08:51:56 on Problem 1007
In Reply To:帮我看一下程序为什么总是Runtime Error Posted by:youngtrips at 2007-05-18 07:27:45
strcpy(temp,strings[i]);
strcpy(strings[i],strings[j]);
strcpy(strings[j],temp);

这三行,temp的长度是n,而strings[i]的长度会大于n.改为如下: 
#include <string.h>
#include <iostream>
using namespace std;

#define MaxLines 100
#define MaxLen 50
int main()
{
//	char strings[MaxLines][MaxLen];
	char *temp;
	char ch;
	int *order;
	int n;//the length of the strings
	int m;//the number of the strings
	int i;
	int j;
	int k;
	int sum = 0;
	cin >>n>>m;
	
	char **strings=new char *[m];

	order = new int[m];
	temp = new char[n+1];
	for (i = 0;i < m;i++)
	{
		strings[i]=new char [n+1];
		cin >>strings[i];		
	}
	for (i = 0;i < m;i++)
	{
		
		for (j = 0;j <= n;j++)
		{
			ch = strings[i][j];
			for (k = j+1;k <= n;k++)
			{
				if (ch > strings[i][k])
					sum = sum + 1;
				
			}
		}
		order[i] = sum;
		sum = 0;
	}
	for (i=0;i<m;i++)
	{
		for (j=i+1;j<m;j++)
		{
			if (order[i] > order[j])
			{
				strcpy(temp,strings[i]);
				strcpy(strings[i],strings[j]);
				strcpy(strings[j],temp);
				k = order[j];
				order[j] = order[i];
				order[i] = k;
			}
			if (order[i] == order[j])
				continue;
		}
	}
	for (i = 0;i < m;i++)
		cout <<strings[i]<<endl;
	
	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