小编dev*_*eop的帖子

对数组进行分区

给定一个由 n 个元素组成的随机排序数组 (arr),函数 partitionArray(int arr[], int n, int x) 将元素划分为两个子集,使得元素 <= x 在左侧子集中,元素 > x 在右侧子集。

测试用例的第一行将包含两个由空格分隔的数字 n(列表中的元素数)和 x(用于分区的数字)。下一行将包含 N 个空格分隔的整数。

对于某些情况,我从以下函数中得到了错误的输出。

这是我的代码:

void partitionArray(int arr[], int n, int x)
{
    int i, j, temp;
    i = 0;
    j = n-1;
  
    while (i < j)
    {
        while (arr[i] <=x)
            i++;
        while (arr[j] > x)
            j--;
    
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    
        i++;
        j--;
    }  
}
Run Code Online (Sandbox Code Playgroud)

对于我得到正确输出的情况是:

10 6
28 26 25 5 6 7 24 29 …
Run Code Online (Sandbox Code Playgroud)

c

9
推荐指数
1
解决办法
170
查看次数

JavaScript 订单列表处理

需要 3 个参数:订单列表 OrderList;订单 ID 号;& 一个“正在处理”或“已交付”的字符串状态。OrderList 是订单对象的数组。

根据状态更新订单列表并返回更新的列表。

-> 如果状态为“处理中”,则更新列表中 id 为 OrderId 的对象,使其状态为“处理中”。

-> 如果状态为“Delivered”,则从列表中删除 ID 为 orderId 的对象。

如果没有给定 orderId 的订单,则该函数将返回列表 orderList 不变。

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding("ascii");
let inputString = "";
let currentLine = 0;

process.stdin.on("data", function(chunk) {
  inputString += chunk;
});
process.stdin.on("end", function() {
  inputString = inputString.split('\n');
  main();
});

function readLine() {
  return inputString[currentLine++];
}


function processOrderList(orderList, orderId, state) {
  // Write your code here
  console.log(orderId);
  console.log(orderList);
  console.log(state);

}


function main() { …
Run Code Online (Sandbox Code Playgroud)

javascript

0
推荐指数
1
解决办法
6325
查看次数

标签 统计

c ×1

javascript ×1