现在的位置: 首页 > 综合 > 正文

支配值数目

2013年01月21日 ⁄ 综合 ⁄ 共 472字 ⁄ 字号 评论关闭

题目:已知f[]与g[]两个整数数组,元素都已经从小到大排列,请写一个程序,算出f[]中比g[]大的对数。

#include <iostream>

using namespace std;

int f[] = {1, 3, 5, 7, 9};
int g[] = {2, 4, 6, 8, 10};
const int size1 = sizeof f / sizeof *f;
const int size2 = sizeof g / sizeof *g;

int getDominate(int *f, int size1, int *g, int size2)
{
	if (f == NULL || g == NULL || size1 <= 0 || size2 <= 0)
		return -1;

	int i = 0;
	int j = 0;
	int count = 0;

	while (i < size1 && j < size2)
	{
		if (f[i] <= g[j])
			i++;
		else
		{
			count += size1 - i;
			j++;
		}
	}

	return count;
}

void main()
{
	int result = getDominate(f, size1, g, size2);
	cout << "result = " << result << endl;
}

抱歉!评论已关闭.