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 kamake at 2012-01-06 12:21:51 on Problem 1009
import java.io.*;
import java.util.*;
public class Main {

	//代表读入的pair
	private ArrayList<Point> pair=null;
	//代表结果图
	private ArrayList<Point> vMap=null;
	//代表图的宽度
	private int iWidth;
	//代表读入的值
	private int iParam;
	//代表读入的数量
	private int iCount;
	//代表总数目
	private int iNum;
	//代表当前位置
	private int iPos;
	
	class Point{
		
		private int firstVal;
		private int secondVal;
		
		public Point(int firstVal,int secondVal)
		{
			this.firstVal=firstVal;
			this.secondVal=secondVal;
		}
		
		public int getFirstVal() {
			return firstVal;
		}
		public void setFirstVal(int firstVal) {
			this.firstVal = firstVal;
		}
		public int getSecondVal() {
			return secondVal;
		}
		public void setSecondVal(int secondVal) {
			this.secondVal = secondVal;
		}
	}
	public Main()
	{
		this.pair=new ArrayList<Point>();
		this.vMap=new ArrayList<Point>();
		init();
	}
	//进行初始化
	public void init()
	{
		this.pair.clear();
		this.vMap.clear();
		this.iPos=1;
		this.iCount=0;
		this.iParam=0;
		this.iWidth=0;
		this.iNum=0;
	}
	
	public void acm1009()
	{
		Scanner scan=new Scanner(System.in);
		iWidth=scan.nextInt();
		iNum=0;
		while(iWidth!=0){
			
			iParam=scan.nextInt();
			iCount=scan.nextInt();
			while(iParam!=0||iCount!=0)
			{
				this.pair.add(new Point(iParam,iCount));
				iNum+=iCount;
				iParam=scan.nextInt();
				iCount=scan.nextInt();
			}
			calPixel();
			
			
			//对于vMap按照iPos从小到大进行排序
			sort();
			//输出结果
			System.out.println(iWidth);
			int iCur=0;
			
			int i;
			
			for(i=0;i<this.vMap.size();i++)
			{
				if(this.vMap.get(i).secondVal==this.vMap.get(iCur).secondVal)
				{
					continue;
				}
				System.out.println(this.vMap.get(iCur).getSecondVal()+" "+(this.vMap.get(i).firstVal-this.vMap.get(iCur).firstVal));
				
				iCur=i;
			}
			System.out.println(this.vMap.get(iCur).getSecondVal()+" "+(iNum-this.vMap.get(iCur).getFirstVal()+1));
			System.out.println("0 0");
			//清空,并重新开始进行计算
			init();
			
			iWidth=scan.nextInt();
		}
		System.out.println(0);
	}
	//冒泡排序
	public void sort()
	{
		for(int i=0;i<this.vMap.size()-1;i++)
		{
			for(int j=0;j<this.vMap.size()-i-1;j++)
			{
				if(this.vMap.get(j).getFirstVal()>this.vMap.get(j+1).getFirstVal())
				{
					int temp=this.vMap.get(j).getFirstVal();
					int temp1=this.vMap.get(j).getSecondVal();
					
					this.vMap.get(j).setFirstVal(this.vMap.get(j+1).getFirstVal());
					this.vMap.get(j).setSecondVal(this.vMap.get(j+1).getSecondVal());
					this.vMap.get(j+1).setFirstVal(temp);
					this.vMap.get(j+1).setSecondVal(temp1);
				}
			}
		}
	}
	//计算像素
	public void calPixel()
	{
		//初始时,位置为1
		iPos=1;
		
		//遍历pair,当有一个新的pair时,就证明有值改变了
		//我们只计算改变的值影响的周围的节点
		for(int i=0;i<this.pair.size();i++)
		{
			//计算出iPos位置所对应的节点所处的行和列
			//行和列均从0开始
			int row=(iPos-1)/iWidth;
			int col=(iPos-1)%iWidth;
			
			// 此处处理1号点问题
			if (col == iWidth - 1)
			{
				if ((row + 2)*iWidth < iNum)
				{
					Point tmpPix=new Point(iPos + iWidth+1, calNode(iPos + iWidth + 1, row + 2, 0));
					vMap.add(tmpPix);
				}
			}

			
			//计算被改变的节点所影响的节点 
			for(int m=row-1;m<=row+1;m++)
			{
				for(int n=col-1;n<=col+1;n++)
				{
					int myPos=m*iWidth+n+1;
					
					if(m>=0&&n>=0&&n<iWidth&&myPos<=iNum)
					{
						Point myPoint=new Point(myPos,calNode(myPos,m,n));
						this.vMap.add(myPoint);
					}
				}
			}
			
			iPos+=this.pair.get(i).getSecondVal();
		}
	}
	//计算节点
	public int calNode(int iPos,int row,int col)
	{
		int result=0;
		
		for(int i=row-1;i<=row+1;i++)
		{
			for(int j=col-1;j<=col+1;j++)
			{
				int temp=iWidth*i+j+1;
				if(temp==iPos)
				{
					continue;
				}
				if(i>=0&&j>=0&&j<iWidth&&temp<=iNum)
				{
					if(Math.abs(getValue(temp)-getValue(iPos))>result)
					{
						result=Math.abs(getValue(temp)-getValue(iPos));
					}
				}
			}
		}
		
		return result;
	}//calNode
	
	//获得值
	public int getValue(int iPos)
	{
		int i=0;
		int iN=0;
		for(i=0;iN<iPos;i++)
		{
			iN+=this.pair.get(i).getSecondVal();
		}
		return this.pair.get(i-1).getFirstVal();
	}
	public static void main(String[] args) throws Exception
	{
		Main mainf=new Main();
		mainf.acm1009();
	}
}

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