问题:我试图在随机整数数组中找到最常见的六个数字,但我只是找到了我的循环计数。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//Salih Atacan Karagöz 210201055
int main()
{
long x=210201055; //password
long y=0;
printf("Enter password:");
scanf("%d",&y);
if(x==y) //password check if statement is true progrom continues
{
int tekrar[49]= {0}; //array for counting repeating numbers
int numbers[49]= {0}; //array for creating random numbers
int z; //integer for loop counter
int counter=0; //counter for loop
int i;
int j=0;
int high=0;
printf("Enter loop count:");
scanf("%d",&z);
srand(time(NULL));
for(counter=1; counter<=z; counter++) //loop will do under these lines z times
{
i=0;
while(i<6) //every time we need 6 random numbers so loop need to create max 6 numbers (0,1,2,3,4,5,)
{
i++;
numbers[i]=rand()%50+1; //+1 for prevent creating zeros mod 49 for creating numbers below 49
tekrar[numbers[i]]++; //array +1 t its value every time same number comes
}
}
for(i=1; i<=49; i++)
{
printf("%d->%d\t",i,tekrar[i]); //for printing created numbers
}
printf("\n -----------\tMost Frequent Six Numbers\t-------------\n");
printf(":(");
int count;
int maxCount = 0;
int maxValue = 0;
for(j=0;j<9;j++){
count=0;
for(i=0; i<49; i++){
if (numbers[i]==numbers[i+1]){
count++;
}
if(count>maxCount){
maxCount==count;
}
}
maxValue=numbers[i];
}
printf("Number: %d", maxValue);
printf("\n-----------GUESS-----------\n"); //we will use bouble sort here
printf(":(");
}
else //if password is wrong program continues from here
{
printf("Wrong Password");
}
}
答案 0 :(得分:0)
这应该适合你:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 50
int main() {
//Access
int numbers[SIZE+1], randomNumbers, count, innerCount;
int pcPassword = 2, userPassword = 0, frequentNumber[6], frequentNumberCount[6];
printf("Enter password:");
scanf("%d", &userPassword);
if(pcPassword != userPassword) {
printf("Wrong Password");
exit(0);
}
printf("How many random Numbers do you want:\n>");
scanf(" %d", &randomNumbers);
//initlaized
for(count = 0; count <= SIZE; count++)
numbers[count] = 0;
//Random Numbers
srand(time(NULL));
for(count = 0; count < randomNumbers; count++)
numbers[rand() % 50 + 1]++;
//Print
for(count = 1; count <= SIZE; count++) {
if((count-1) % 6 == 0)
printf("\n");
printf("%4d -> %3d", count, numbers[count]);
}
//initlaized
for(count = 0; count < 6; count++) {
frequentNumber[count] = 0;
frequentNumberCount[count] = 0;
}
//Get frequent numbers
for(count = 1; count <= SIZE; count++) {
for(innerCount = 0; innerCount < 6; innerCount++) {
if(numbers[count] > frequentNumberCount[innerCount]) {
if(innerCount+1 <= 5)
frequentNumber[innerCount+1] = frequentNumber[innerCount];
frequentNumber[innerCount] = count;
frequentNumberCount[innerCount] = numbers[count];
break;
}
}
}
//Print
printf("\n\nThe 6 most frequent numbers are:\n");
for(count = 0; count < 6; count++)
printf("%d. -> Number: %3d Count: %3d\n", count+1, frequentNumber[count], frequentNumberCount[count]);
return 0;
}
输入:
2 //Password
1200 //Random Numbers
输出:
Enter password:2
How many random Numbers do you want:
>1200
1 -> 23 2 -> 21 3 -> 24 4 -> 24 5 -> 25 6 -> 28
7 -> 22 8 -> 22 9 -> 25 10 -> 27 11 -> 27 12 -> 20
13 -> 14 14 -> 29 15 -> 28 16 -> 20 17 -> 22 18 -> 20
19 -> 29 20 -> 25 21 -> 35 22 -> 21 23 -> 24 24 -> 23
25 -> 21 26 -> 24 27 -> 12 28 -> 33 29 -> 23 30 -> 17
31 -> 33 32 -> 21 33 -> 24 34 -> 23 35 -> 33 36 -> 21
37 -> 22 38 -> 28 39 -> 30 40 -> 18 41 -> 16 42 -> 29
43 -> 24 44 -> 29 45 -> 30 46 -> 20 47 -> 19 48 -> 26
49 -> 18 50 -> 28
The 6 most frequent numbers are:
1. -> Number: 21 Count: 35
2. -> Number: 28 Count: 33
3. -> Number: 31 Count: 33
4. -> Number: 35 Count: 33
5. -> Number: 39 Count: 30
6. -> Number: 45 Count: 30