题目描述:
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;
}
该程序实现了一个功能,输入一个整数数组,对于数组中的每个元素,统计并返回小于它的数字个数。代码通过两层循环遍历数组,计算每个元素前面有多少个比它小的数字,然后存储到结果数组中。最后输出结果数组。
806

被折叠的 条评论
为什么被折叠?



