发布网友 发布时间:2022-04-23 12:55
共5个回答
热心网友 时间:2023-05-21 14:37
一刷新这么多答案了,看了下他们的代码顿时无地自容了。。既然写好了我还是贴下。。
import java.util.HashMap;热心网友 时间:2023-05-21 14:37
package caikai;
import java.util.Arrays;
import java.util.Random;
public class Ran {
public static void main(String[] args) {
Random random = new Random();
int[] arr = new int[20];
boolean h = true;
do {
boolean j = true;
do {
for (int i = 0; i < arr.length; i++) {
int number = random.nextInt(4) + 1;
arr[i] = number;
}
int a = 0, b = 0, c = 0, d = 0;
for (int i = 0; i < arr.length; i++) {
switch (arr[i]) {
case 1:
a++;
break;
case 2:
b++;
break;
case 3:
c++;
break;
case 4:
d++;
break;
}
}
if (a < 4 || a > 6 || b < 4 || b > 6 || c < 4 || c > 6 || d < 4
|| d > 6) {
j = false;
}
for (int k = 0; k < arr.length - 2; k++) {
if (arr[k] == arr[k + 1] && arr[k + 1] == arr[k + 2]) {
j = false;
break;
}
}
if (arr[4] == arr[5] || arr[9] == arr[10] || arr[14] == arr[15]) {
j = false;
}
for (int i1 = 0; i1 < 4; i1++) {
int a1 = 0, b1 = 0, c1 = 0, d1 = 0;
for (int i = i1 * 5; i <= 5 * (i1 + 1)-1; i++) {
switch (arr[i]) {
case 1:
a1++;
break;
case 2:
b1++;
break;
case 3:
c1++;
break;
case 4:
d1++;
break;
}
}
if (a1 > 3 || b1 > 3 || c1 > 3 || d1 > 3
|| a1 + b1 + c1 + d1 != 5) {
j = false;
}
}
if (j == true) {
System.out.println(Arrays.toString(arr));
j = false;
h = false;
break;
}
} while (j);
} while (h);
}
}
(输出的是一个数组,按照1=a,2=b,3=c,4=d自己把数字改成字母.)
热心网友 时间:2023-05-21 14:38
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include<time.h>
int main ()
{
char str[21]={0};
int cnt[4]={0};
int loop=0;
int num;
int flag=0;
int five[4]={0};
int loop1, time1=0;
char c;
srand((int)time(0));
while(loop<20)
{
num = rand()%4; //获得一个0-3的随机数
if(cnt[num]>=6)
{
continue;
}
if(flag>=2 && cnt[num]>=4) //出现两个6的话,后面必须是两个4
{
continue;
}
c = num + 65; //变成大写字母
if(loop >= 2)
{
if(c == str[loop-1] && c == str[loop-2]) //三个连续字符相同
{
continue;
}
}
if(loop == 5 || loop == 10 || loop == 15)
{
if(c == str[loop-1]) //和前一个相同
{
continue;
}
}
if(loop%5==4)
{
for(loop1=0;loop1<4;loop1++)
{
if(five[loop1] != 0)
{
time1++;
}
}
}
if(time1<3)
{
if(five[num]!=0)
{
continue;
}
}
cnt[num]++; //字符出现次数自增
five[num]++;
if(cnt[num] == 6) //出现6次的字符个数
{
flag++;
}
str[loop] = c;
loop++;
if(loop%5==0)
{
five[0] = 0;
five[1] = 0;
five[2] = 0;
five[3] = 0;
}
}
printf("%s\n", str);
return 0;
}
热心网友 时间:2023-05-21 14:39
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main()
{
srand(time(NULL));
char x[4] = {'A','B','C','D'};
char buffer[20];
int i,c[4];
while(1)
{
//1
for(i=0;i<20;++i)
buffer[i] = x[(int)(((double)rand()/RAND_MAX)*4)];
//4
if(buffer[4] == buffer[5] || buffer[9] == buffer[10] || buffer[14] == buffer[15])
continue;
//2
c[0] = c[1] = c[2] = c[3] = 0;
for(i=0;i<20;++i)
++c[buffer[i]-'A'];
if(c[0] > 6 || c[1] > 6 || c[2] > 6 || c[3] > 6)
continue;
if(c[0] < 4 || c[1] < 4 || c[2] < 4 || c[3] < 4)
continue;
//3
for(i=2;i<20;++i)
{
if(buffer[i] == buffer[i-1] && buffer[i] == buffer[i-2])
break;
}
if(i<20)
continue;
//5
c[0] = c[1] = c[2] = c[3] = 0;
for(i=0;i<5;++i)
++c[buffer[i]-'A'];
if(c[0]*c[1]==0 && c[2]*c[3]==0 || c[0]*c[2]==0 && c[1]*c[3]==0)
continue;
c[0] = c[1] = c[2] = c[3] = 0;
for(i=6;i<10;++i)
++c[buffer[i]-'A'];
if(c[0]*c[1]==0 && c[2]*c[3]==0 || c[0]*c[2]==0 && c[1]*c[3]==0)
continue;
c[0] = c[1] = c[2] = c[3] = 0;
for(i=10;i<15;++i)
++c[buffer[i]-'A'];
if(c[0]*c[1]==0 && c[2]*c[3]==0 || c[0]*c[2]==0 && c[1]*c[3]==0)
continue;
c[0] = c[1] = c[2] = c[3] = 0;
for(i=15;i<20;++i)
++c[buffer[i]-'A'];
if(c[0]*c[1]==0 && c[2]*c[3]==0 || c[0]*c[2]==0 && c[1]*c[3]==0)
continue;
break;
}
printf("%20.20s\n",buffer);
}
热心网友 时间:2023-05-21 14:39
条件这么多,这100分得死多少脑细胞才赚的到啊。。。。没人答了