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 yanxiangtianji at 2008-08-16 14:50:15 on Problem 1002
我这个代码自认比网上绝大多数拿出来乱显的代码要精巧、要高效。
我和我的朋友拿了无数组数据测都没有问题,就是这PKU老说WA,我们都无奈了,大家帮看看(不客气的说:让很多学了点皮毛就自认很NB的人学习学习)。

基本思路:
1。读入,转换(乱显的人好好看看)
2。排序,出来(借鉴自STL中的upper_bound和lower_bound算法思路)
3。输出

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct Item{
	char number[8];
	int count;
}num[100000];
const char map[8]={'A','D','G','J','M','P','T','W'};
int Ocount(0),chong[50000];

inline int compare(const void *a,const void *b)
{
	return strcmp((*(Item*)a).number,(*(Item*)b).number);
}
char translate(char c)
{
	int i;
	for(i=1;i<8;i++)//it's impossible to break when i=0;
		if(c<map[i])
			break;
	return(i+1+'0');
}
void readin()
{
	char c;
	char *buf=num[Ocount++].number;
	int count(0);
	c=cin.get();
	while(c!='\n')
	{
		if(count<7)
			if(c>='0'&&c<='9')	buf[count++]=c;
			else if(c>='A'&&c<='Z')	buf[count++]=translate(c);
		c=cin.get();
	}
	buf[7]='\0';
//	cout<<"test:"<<buf<<endl;
}

int main(){
	int n,i,*p=chong;
	cin>>n;	cin.get();
	for(i=0;i<n;i++)
		readin();
	qsort((void *)num,n,sizeof(num[0]),compare);
//	cout<<"\nsorted"<<endl;
//	for(i=0;i<n;i++)
//		cout<<i<<ends<<num[i].number<<endl;
//	cout<<"chong="<<chong<<endl;
	bool bchong(false);
	for(i=n-1;i>=0;i--)
	{
		if(i && !strcmp(num[i].number,num[i-1].number))
		{
			num[i-1].count=num[i].count+1;
			bchong=true;
//			cout<<i-1<<" same to "<<i<<endl;
		}
		else if(bchong)
		{
			bchong=false;
			*p=i;
//			cout<<"recorded: p="<<p<<" num="<<num[*p].number<<" count="<<num[*p].count<<endl;
			p++;
		}
	}
	if(p==chong)
		cout<<"No duplicates.\n";
	else
		while((--p)>=chong)
		{
			int j;
			for(j=0;j<3;j++)
				cout<<num[*p].number[j];
			cout<<"-";
			for(;j<7;j++)
				cout<<num[*p].number[j];
			cout<<ends<<num[*p].count+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