An interview question

xiaoxiao2021-03-06  93

There are array a [0 ... n-1], its content is disorderly, another array B [0 ... M], where n> m, is now not allowed to use other groups, no change A [ 0 ... n], put the smallest number in the A array into B [0], put the second small number to B [1], and so on

This is the topic of Dongguan East Friendly Interview, I didn't make it for an hour, I hope that the expert guidance is one or two.

Foreach (int i in a) // Each value in A {for (int J = 0; j = b [J] && i

Public void insert (int [] b, int index, int value) {for (INT i = B.LENGTH-1; I> = index; I -) {b [i] = b [i-1]; / / Insert a rearward shift one} b [index] = value;}

An interview question request N! (The class of n) needs to be spilled. Let's write a program together

/ *

This program calculates all the effective numbers of the output K! (K = 1, 2, ..., n) for a given N (n <= 100), because K!'S value may be large, so one-dimensional Array storage calculation results. Set one of the elements of the group to store a number of K!, And agree to the first location of the array from the low to the high position, the second position .... In the program, calculate K! Using the result of the obtained (K-1)! Since the result of the obtained (k-1), it is obtained, for example, 4! = 24, calculate 5! After the original 24 is added 4 times 120. In order to control the number of points, the integer variable C is introduced to record the current (k-1)!

* / # include

#define maxn 1000

INT A [MAXN], B [MAXN];

Void PNext (INT K, INT * CP) {INT C = * CP, I, M, R; For (i = 0; i 0) {a [C ] = R% 10; r = r / 10;} * CP = C;

Void Write (int * a, int K, int C) {INT i; Printf ("% 4d! =", k); for (i = c - 1; i> = 0; i -) Printf ("% D ", A [I]); Printf (" / n / n ");}

转载请注明原文地址:https://www.9cbs.com/read-98482.html

New Post(0)