已知兩已排序陣列,求第一個陣列元素比第二個陣列元素大的個數總和。
#include < iostream >
using namespace std;
int GreateCount(int f[], int g[], int numf, int numg)
{
int length = 0;
int sum = 0;
for(int i = 0; i < numf; ++i)
{
while(length < numg)
{
if(f[i] > g[0 + length])
length++;
else
break;
}
sum += length;
}
return sum;
}
int main(void)
{
int f[] = {1, 3, 5, 7, 9, 10, 11};
int g[] = {2, 3, 4, 7, 8};
int sum = GreateCount(f, g, 7, 5);
cout << "Sum = " << sum << endl;
}
沒有留言:
張貼留言