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

AC掉了,mid(x, y, z)写的有点问题WA的

Posted by Sempr at 2006-03-08 23:01:06 on Problem 2313
In Reply To:我想可能是这样,将输入数据看成从小到大有序的是没有影响的,而且相应的点b 放在a上,应该是这个|a0-an| Posted by:sunmoonstar at 2006-03-08 22:54:32
#include <stdio.h>
#define SIZE 101

int N;
int A[SIZE], B[SIZE];

int abs(int x)
{
	return (x > 0) ? x : -x;
}

int mid(int a, int b, int c)
{
	int t;
	if (a > b)
	{
		t = a;
		a = b;
		b = t;
	}
	if (a > c)
	{
		t = a;
		a = c;
		c = t;
	}
	if (b > c)
	{
		t = c;
		c = b;
		b = t;
	}
	return b;
}

int main()
{
	int i, ans;
	while (scanf("%d", &N) != -1 && N > 0)
	{
		ans = 0;
		for (i = 1; i <= N; i++)
		{
			scanf("%d", &A[i]);
		}
		if (N == 1)
		{
			printf("0\n");
			continue;
		}
		B[1] = A[1];
		for (i = 2; i < N; i++)
		{
			B[i] = mid(A[i], B[i - 1], A[i + 1]);
			ans += abs(A[i] - B[i]);
			ans += abs(B[i] - B[i - 1]);
		}
		ans += abs(A[N] - B[N - 1]);
		B[N] = B[N - 1];
		printf("%d\n", ans);
	}
	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