在向量B中,从其开头插入向量a的偶数,从其末尾插入向量a的偶数奇数向量A(这一项必须只用单一结构解)
重复);我不知道我应该在do while loop
中输入什么条件#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int i, j, A[25], B[25], testeNumeroIgual = 0, aux;
srand(time(NULL));
i = 0;
do {
A[i] = rand() % 40;
testeNumeroIgual = 0;
for (j = 0; j < i; j++) {
if (A[j] == A[i]) {
testeNumeroIgual = 1;
}
}
if (testeNumeroIgual == 0)
i++;
} while(i < 25);
i = 0;
do {
if (A[i] % 2 == 0) {
B[i] = A[i];
}
else {
B[25 - i] = A[i];
}
i++;
} while(i < 25);
for (i = 0; i < 25; i++) {
printf("%dn", B[i]);
}
return 0;
}
生成随机数到特定循环中,以隔离算法
然后使用tail和head两个索引将其放在正确的位置。插入时增加头部和减小尾部
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define SIZE 25
int main()
{
int i, A[SIZE], B[SIZE], tail, head ;
srand(time(NULL));
/* generate random number for A */
for (i = 0; i < SIZE; i++) {
A[i] = rand() % 40;
}
tail = SIZE - 1;
head = 0;
for (i = 0; i < SIZE; i++) {
if (A[i] % 2) {
B[tail--] = A[i];
} else {
B[head++] = A[i];
}
}
printf("A: n");
for (i = 0; i < SIZE; i++) {
printf("%d %dn",i, A[i]);
}
printf("nB: n");
for (i = 0; i < SIZE; i++) {
printf("%i %dn", i, B[i]);
}
return 0;
}