在数字上划分数组元素-崩溃问题

时间:2018-11-23 16:27:35

标签: arrays sorting crash

我是一个初学者,很不幸遇到了一个我无法解决的问题:)我事先表示歉意,因为英语不是我的母语。

我的任务是对数组进行排序,以使具有相同数字的元素排在最前面,然后是数组的其余部分。

示例: 输入数组:1 22 43 444 51 16 7 8888 90 11 -1 排序后:1,22,444,7,8888,11,43,51,16,90

输入数组:12,33,1,19,44,11,27,76,13 排序后:33、1、44、11、12、19、27、76、13

问题是,我们不允许使用函数或多个数组来解决问题。

这是我已经尝试过的:

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml')

cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
    for(x,y,w,h) in faces:
        print(x,y,w,h)
    cv2.imshow('frame',frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break


cap.release()
cv2.destroyAllWindows()

我不确定代码有什么问题以及为什么程序崩溃。谢谢。

2 个答案:

答案 0 :(得分:0)

这可能有帮助,

    for (int i = 0; i < in.length; i++) {
        for (int j = in.length - 1; j > i; j--) {
            if (in[j] < in[j - 1]) {
                int temp = in[j];
                in[j] = in[j - 1];
                in[j - 1] = temp;
            }
        }
    }

答案 1 :(得分:0)

您对问题的定义与所提供的示例之间并不一致,例如:

Input array  : 12, 33, 1, 19, 44, 11, 27, 76, 13 
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13

但是根据您的陈述, 11 不应该是第一个要素吗?

如果我缺少什么,请告诉我。无论如何(并且您也没有提到该语言),我用C ++及其工作原理(据我所能理解的目标):

#include <iostream>
#include <vector>

using namespace std;


bool samedigits(int x)
{
    //if ( x < 10 ) return false;
    int digit = x%10;
    while(x>0)
    {
        if(x%10 != digit) return false;
        x = x/10;
    }
    return true;
}

int main() {
    int same = 0;
    int same1 = 1;
    int temp = 0;
    int n = 0;
    int digit = 0;
    int digit1 = 0;

    vector <int>a = { 1,22,43,444,51,16,7,8888,90,11, 99};

    for(int n : a) {
        cout << n << " ";
    }

     for (int i=0; i <= a.size() -1; i++)
        {

            for (int j=0; j <= a.size() - 1 ; j++)
            {
                if ( j != i) {

                    if (samedigits(a[j]) && samedigits(a[i])) {
                        if (a[j] > a[i]) {
                            int tmp = a[j];
                            a[j] = a[i];
                            a[i] = tmp;
                            continue;
                        }
                    }else if (! samedigits(a[j]) && samedigits(a[i]) ) {
                            int tmp = a[j];
                            a[j] = a[i];
                            a[i] = tmp;
                            continue;

                    } else if ( samedigits(a[j]) && ! samedigits(a[i]) ){
                        continue;
                    }else {
                        if (a[j] > a[i]) {
                            int tmp = a[j];
                            a[j] = a[i];
                            a[i] = tmp;
                            continue;
                        }
                    }
                }
            }
        }
    cout << endl;
    for(int n : a) {
        cout << n << " ";
    }

}

它返回:

enter image description here

如果您将单个数字视为“具有相同数字的元素”,我也不会。因此,我评论了一条可以很好地适合该观点的观点。随时发表评论。

您可以在这里进行测试:

https://www.jdoodle.com/online-compiler-c++

致谢!