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

高手给指点一下,附程序 WA

Posted by baichen at 2006-10-30 20:45:48 on Problem 2092
#include<stdio.h>
main()
{
	int n,m,i,j,a[501][501];
	while(scanf("%d%d",&n,&m)!=EOF&&n&&m)
	{
		int b[501]={0},c[501]={0},d[501]={0};
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			{
				scanf("%d",&a[i][j]);//先输入值
				b[a[i][j]]++;//纪录相应人的编号出现的次数
				d[a[i][j]]++;//下面排序之后用的
			}
		//现在要在b数组里面找到第二大的值
		//想法是冒泡仅两次即可
		bool change=true;
		int t;
		for(i=1;i<=2&&change;i++)
		{
			change=false;
			for(j=1;j<=500-i;j++)
				if(b[j]>b[j+1])
				{
					t=b[j];b[j]=b[j+1];b[j+1]=t;
					change=true;
				}
		}//那么b[499]中就是第二大的数据了 
		//注意这里经过排序之后,下标已经变了
		j=1;
		for(i=1;i<=500;i++)
		{
			if(d[i]==b[499])
			{
				c[j]=i;//纪录次大的编号
				//printf("%d ",c[j]);
				//printf("%d ",d[i]);
				j++;
			}
		}
		j--;n=j;//n为c中可用元素的个数 
		//下面对c排序即可
		change=true;
		for(i=1;i<=n-1&&change;i++)
		{
			change=false;
			for(j=1;j<=n-i;j++)
				if(c[j]>c[j+1])
				{
					t=c[j];c[j]=c[j+1];c[j+1]=t;
					change=true;
				}
		}
		for(i=1;i<=n;i++)
			printf("%d ",c[i]);
		printf("\n");
	}
	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