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 本地运行正常(已经解决,附上全部代码C++)

Posted by freelark at 2016-11-03 21:35:09 on Problem 1009
In Reply To:求教runtime error 本地运行正常 Posted by:freelark at 2016-11-01 18:57:42
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;

void math();
void mathBig(); //处理n过宽的数据--直接写入
void analysis(int *grid, int *gridDec, int lW, int lN, int line = 1,int downEdge = 2);//lW行宽,lN计算宽,downEdge下边界
int writeLine(int* grid, int* gridDec, int pixel, int n, int m, int sum);//向表中写入数据
void printVec();  //打印并清空
void printV(int*v, int row, int col);

int width = 0;
vector<vector<int>>resVec;
vector<int>tempV;

int main(void)
{
	while (cin >> width){
		if (width == 0) break;
		else if (width < 100000 )	math();
		else mathBig() ;
        }
		cout << "0";
		system("pause");
		return 0;	
}

void analysis(int *grid, int *gridDec, int lW, int lN, int line, int downEdge){	
	int x = 0;
	int *res = new int[8];
	memset(res, 0, 8 * sizeof(int));
	//cout << "------计算用表--------" << endl;
	//printV(grid, 3, width);
	for (int i = 0; i<lN; i++){
		memset(res, 0, 8 * sizeof(int));
		x = line*lW + i;
		if (line > 0){
			if (i>0)
				res[0] = abs(grid[x - lW - 1] - grid[x]);
			res[1] = abs(grid[x - lW] - grid[x]);
			if (i<lW - 1)
				res[2] = abs(grid[x - lW + 1] - grid[x]);
		}
		if (i > 0)
			res[3] = abs(grid[x - 1] - grid[x]);
		if (i < lW - 1)
			res[4] = abs(grid[x + 1] - grid[x]);
		if (line < downEdge){
			if (i > 0)
				res[5] = abs(grid[x + lW - 1] - grid[x]);
			res[6] = abs(grid[x + lW] - grid[x]);
			if (i < lW - 1)
				res[7] = abs(grid[x + lW + 1] - grid[x]);
		}
		for (int j = 1; j<8; j++){
			if (res[0]<res[j])
				res[0] = res[j];	
			//cout << setw(4) << res[j];
		}
		//cout << endl;
		gridDec[i] = res[0];
	}
	//分析并读入vector,这里是全行写入,不足也要把0写进去
	for (int i = 0; i<lN; i++){
		if (resVec.size()>0 && gridDec[i] == resVec.back()[0]){
			resVec.back()[1]++;
		}
		else{
			tempV.clear();
			tempV.push_back(gridDec[i]);
			tempV.push_back(1);
			resVec.push_back(tempV);
		}
	}
	//cout << "------结果-------" << endl;
	//printV(gridDec, 1, width);
	delete[]res;
}

int writeLine(int* grid, int* gridDec, int pixel, int n, int m,int sum)//n:写入总量,m:表中现有数量,sum:总数据量
{
	//cout << "n=" << n << endl;
	for (int i = 0; i < n; i++)
	{
		if (m < 3 * width){			
			grid[m] = pixel;			
			if (m == 3 * width - 1){
	//			cout << "已写入数据总量sum-n+i=" << sum - n + i << endl;
				if (sum-n+i == 3 * width-1) analysis(grid, gridDec, width, width, 0); //计算首行				
				analysis(grid, gridDec, width, width);  //满行时发生计算	
			}
			m++;
		}
		else          
		{
			for (int k = 0; k < width; k++){ 
				grid[k] = grid[k + width];
				grid[k + width] = grid[k + 2 * width];
				grid[k + 2 * width] = 0;
			}
			grid[m - width] = pixel;
			m = m - width;
			m++;
		}
	}
	return m;
}

void math(){
	int pixel, n; //像素,数量
	int gridNumbers = 0; //表1数据总量
	int sum = 0;//记录要计算的数据总数      
	int* grid = new int[width * 3];  //分析三排
	memset(grid, 0, 3 * width*sizeof(int));
	int* gridDec = new int[width];  //目标列表
	memset(gridDec, 0, width*sizeof(int));

	while (cin >> pixel >> n){  
		if (pixel == 0 && n == 0) { 
			if (sum < 3 * width) { 				
				if (sum <=  width) analysis(grid, gridDec, width, sum, 0, 0); 
				else  analysis(grid, gridDec, width, width, 0, 1);

				if (sum <=2 * width&& sum >  width) analysis(grid, gridDec, width, sum - width,  1, 1);
				else if (sum >2 * width)  analysis(grid, gridDec, width, width, 1, 1);

				if (sum > 2 * width) analysis(grid, gridDec, width, sum-2*width,2);
				break;
			}
			analysis(grid, gridDec, width, width, 2);
			break;
		}
		if (n >= 3 * width){   	
			sum += 2 * width;
			gridNumbers = writeLine(grid, gridDec, pixel, 2 * width, gridNumbers, sum); //grid数满时计算第2行,sum小于3倍width时计算第1行			
			tempV.clear();
			tempV.push_back(0);
			tempV.push_back(n - 3 * width);
			resVec.push_back(tempV);
			n = width; //满员,触发计算
		}
		sum += n;   //总计算数据量
		gridNumbers = writeLine(grid, gridDec, pixel, n, gridNumbers, sum);            
	}
	//输出结果
	printVec();
	delete[] grid;
	delete[] gridDec;
	sum = 0;
}
void mathBig(){
	int pixel, n;
	while (cin >> pixel >> n){
		if (pixel == 0 && n == 0) break;
		tempV.clear();
		tempV.push_back(0);
		tempV.push_back(n);
		resVec.push_back(tempV);
	}
	printVec();
}
void printVec(){
	cout << width << endl;
	for (unsigned int i = 0; i < resVec.size(); i++)
		cout << resVec[i][0] << " " << resVec[i][1] << endl;
	resVec.clear();
	cout << "0 0" << endl;
}
void printV(int*v, int row, int col){
	for (int i = 0; i < row; i++){
		for (int j = 0; j < col; j++)
			cout << setw(5) << v[j + col*i];
		cout << endl;
	}
}

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