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

答案错误,错在何处? (附代码)

Posted by qinghailake at 2009-08-04 18:01:14 on Problem 2371
#include <iostream>
#include <string>

using namespace std;

template<typename T>
class sort
{
public:
		void  static binary_insert_sort(T data[], const int n);
	
};

template<typename T>
void sort<T>::binary_insert_sort(T data[], const int n)
{
		T key;
		int i, j, low, high, m;
		for (i=1; i<n; ++i)
		{
			key = data[i];
			low = 0;
			high = i - 1;			
			while (	low <= high)
			{
				m = (low + high) / 2;	
				if (key < data[m])
				{
					high = m - 1;		
				}
				else
				{
					low = m + 1;	
				}
			}
			for (j=i-1; j>=high+1; --j)  // j>=high+1 ?
			{
				data[j+1] = data[j];	
			}
			data[j+1] = key;
		}
}

int main()
{
	
	int n;	
	cin >> n; //5
    int* data = new int[n];	
	for (int i=0; i<n; ++i)
	{
		cin >> data[i];		
	}
	
	string str;
	cin >> str;
	
	int m;
	cin >>m ;
	int* idx = new int[m] ;
	for (int i=0; i<m; ++i)
	{
		cin >> idx[i] ;		
	}
		
	sort<int>::binary_insert_sort(data, 5);
		
	for (int i=0; i<m; ++i)
	{
		cout << data[idx[i] - 1] << 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