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

求教runtime error 本地运行正常

Posted by freelark at 2016-11-01 18:57:42 on Problem 1009
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
using namespace std;

void math(int width);
void analysis(int *grid, int *gridDec, int line = 1);
int writeLine(int* grid, int* gridDec, int pixel, int n, int m);

int width = 0;
bool firstLineCaculate = false;   //首行计算标志
vector<vector<long int>>resVec;
vector<long int>tempV;

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


void analysis(int *grid, int *gridDec, int line){
	int x = 0;
	int *res = new int[8];
	memset(res, 0, 8 * sizeof(int));

	for (int i = 0; i<width; i++){
		memset(res, 0, 8 * sizeof(int));
		x = line*width + i;
		if (line > 0){
			if (i>0)
				res[0] = abs(grid[x - width - 1] - grid[x]);
			res[1] = abs(grid[x - width] - grid[x]);
			if (i<width - 1)
				res[2] = abs(grid[x - width + 1] - grid[x]);
		}
		if (i > 0)
			res[3] = abs(grid[x - 1] - grid[x]);
		if (i < width - 1)
			res[4] = abs(grid[x + 1] - grid[x]);
		if (line < 2){
			if (i > 0)
				res[5] = abs(grid[x + width - 1] - grid[x]);
			res[6] = abs(grid[x + width] - grid[x]);
			if (i < width - 1)
				res[7] = abs(grid[x + width + 1] - grid[x]);
		}
		for (int j = 1; j<8; j++){
			if (res[0]<res[j])
				res[0] = res[j];
		}
		gridDec[i] = res[0];
	}

	//分析并读入vector
	for (int i = 0; i<width; 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);
		}
	}
	delete[]res;
}

int writeLine(int* grid, int* gridDec, int pixel, int n, int m)
{
	for (int i = 0; i < n; i++)
	{
		if (m + i < 3 * width){
			grid[m + i] = pixel;
			if (m + i == 3 * width - 1){
				if (!firstLineCaculate){ 				
					analysis(grid, gridDec, 0);
					firstLineCaculate = true;
				}
				analysis(grid, gridDec);
			}
		}
		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 + i - width] = pixel;
			m = m - width;
		}
	}

	return m;
}

void math(int width){
	long int pixel, n;
	int toInputVol = 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) {  //中止后,分析最后一行
			analysis(grid, gridDec, 2);
			break;
		}
		if (n >= 3 * width){   //n大于3倍行数时,直接写0
			toInputVol = writeLine(grid, gridDec, pixel, 2 * width, toInputVol); 
			toInputVol += 2 * width;
			tempV.clear();
			tempV.push_back(0);
			tempV.push_back(n - 3 * width);
			resVec.push_back(tempV);
			n = width; //满员,触发计算
		}
		toInputVol = writeLine(grid, gridDec, pixel, n, toInputVol);
		toInputVol += n;                                          //已写入数量 
	}
	//输出结果
	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;
	delete[] grid;
	delete[] gridDec;
}

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