9.28巨人网络笔试

  • Post author:
  • Post category:其他


题目描述:

1.输入一个数组nums,对于其中每个元素nums[i],请统计数组中比它小的所有数字的数目。并且以数组的形式返回。

输入:

[6,5,4,8,6]

输出:

[2,1,0,4,2]

说明:

nums[0]=6 数组中小于6的数字数目为2

nums[1]=5 数组中小于6的数字数目为1

nums[2]=4 数组中小于6的数字数目为0

nums[3]=8 数组中小于6的数字数目为4

nums[4]=6 数组中小于6的数字数目为2

函数代码:

#include <iostream>
using namespace std;
int main()
{
	int nums[]={6,5,4,8,6};
	int n=sizeof(nums)/sizeof(nums[0]);
	int res[n];
	int k=0;
	int cnt=0;
	for(int i=0;i<n;i++)
	{
		cnt=0;
		for(int j=0;j<n;j++)
		{
			if(nums[j]<nums[i])
			{
				cnt++;
			}
			
		}
		res[k++]=cnt;	
	}
	
	for(int l=0;l<n;l++)
	{
		cout<<res[l]<<" ";
	}
	return 0;
	
} 



版权声明:本文为chenmi123321原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。