原文:
给定一个由 n 个整数和一个整数 x 组成的arr【】】,任务是通过 x 对数组元素执行整数除法,并按照获得的商的非递减顺序打印数组的索引。
示例:
输入: n = 3,x = 3,顺序[] = {2,7,4} 输出: 1 3 2 解释:** 将数组元素除以 3 后,数组修改为{0,2,1}。因此,所需的输出顺序是 1 3 2。
输入: n = 5,x = 6,顺序[] = {9,10,4,7,2} 输出: 3 5 1 2 4 解释:** 将数组元素除以 6 后,将数组元素修改为 1 1 0 1 0。因此,所需的序列是 3 5 1 2 4。
方法:按照以下步骤解决问题:
- 遍历数组
- 初始化的。
- 对于每个数组元素,将除以 x 得到的商的值存储为中的的第一个元素,将第二个元素存储为所需顺序中整数的位置。
- 遍历结束后,进行排序,最后打印出对的所有第二个元素。
下面是上述方法的实现:
c
// c program for the above approach
#include
using namespace std;
// function to print the order of array
// elements generating non-decreasing
// quotient after division by x
void printorder(int order[], int n, int x)
{
// stores the quotient and the order
vector > vect;
// traverse the array
for (int i = 0; i < n; i ) {
if (order[i] % x == 0) {
vect.push_back({ order[i] / x,
i 1 });
}
else {
vect.push_back({ order[i] / x 1,
i 1 });
}
}
// sort the vector
sort(vect.begin(), vect.end());
// print the order
for (int i = 0; i < n; i ) {
cout << vect[i].second << " ";
}
cout << endl;
}
// driver code
int main()
{
int n = 3, x = 3;
int order[] = { 2, 7, 4 };
printorder(order, n, x);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
class gfg {
// function to print the order of array
// elements generating non-decreasing
// quotient after division by x
static void printorder(int order[], int n, int x)
{
// stores the quotient and the order
arraylist vect = new arraylist<>();
// traverse the array
for (int i = 0; i < n; i ) {
if (order[i] % x == 0) {
vect.add(new int[] { order[i] / x, i 1 });
}
else {
vect.add(
new int[] { order[i] / x 1, i 1 });
}
}
// sort the vector
collections.sort(vect, (a, b) -> a[0] - b[0]);
// print the order
for (int i = 0; i < n; i ) {
system.out.print(vect.get(i)[1] " ");
}
system.out.println();
}
// driver code
public static void main(string args[])
{
int n = 3, x = 3;
int order[] = { 2, 7, 4 };
printorder(order, n, x);
}
}
// this code is contributed by hemanth gadarla
python 3
# python3 program for the above approach
# function to print the order of array
# elements generating non-decreasing
# quotient after division by x
def printorder(order, n, x):
# stores the quotient and the order
vect = []
# traverse the array
for i in range(n):
if (order[i] % x == 0):
vect.append([order[i] // x, i 1])
else:
vect.append([order[i] // x 1,i 1])
# sort the vector
vect = sorted(vect)
# print the order
for i in range(n):
print(vect[i][1], end = " ")
# driver code
if __name__ == '__main__':
n, x = 3, 3
order = [2, 7, 4]
printorder(order, n, x)
# this code is contributed by mohit kumar 29
c
// c# program to implement
// the above approach
using system;
using system.collections.generic;
class gfg
{
// function to print the order of array
// elements generating non-decreasing
// quotient after division by x
static void printorder(int[] order, int n, int x)
{
// stores the quotient and the order
list> vect = new list>();
// traverse the array
for (int i = 0; i < n; i )
{
if (order[i] % x == 0)
{
vect.add(new tuple((order[i] / x), i 1));
}
else
{
vect.add(new tuple((order[i] / x 1), i 1));
}
}
// sort the vector
vect.sort();
// print the order
for (int i = 0; i < n; i )
{
console.write(vect[i].item2 " ");
}
console.writeline();
}
// driver code
public static void main()
{
int n = 3, x = 3;
int[] order = { 2, 7, 4 };
printorder(order, n, x);
}
}
// this code is contributed by code_hunt.
java 描述语言
output:
1 3 2
时间复杂度: o(n) 辅助空间: o(n)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处