How can I get rid of repetitions in my code? Give advice or advise where you can read about how to solve my problem. Thank you for your help.

#include <iostream> #include <stdlib.h> #include <time.h> #include <string.h> #include <stdio.h> using namespace std; int main() { int m,k,i,z,j,h=0,kolvo,f,g=0, massiv[g]; char str1[100]; srand(time(NULL)); kolvo=0; while (kolvo<15) { for(i=0;i<9;i++) { m = 97+rand()%3; str1[i]=m; } massiv[g]={str1[0],str1[1],str1[2],str1[3],str1[4],str1[5],str1[6],str1[7],str1[8]}; k = 3+rand()%9; str1[k]='\0'; z=0; j=0; f=0; int length_string = strlen(str1); //----------------------------- {break;} if(*(str1)=='a'&&*(str1+1)=='c') for(i=0;i<(length_string);i++) { if(*(str1+i)=='c') z++; if(*(str1+i)=='a') { if(*(str1+i+1)!='a') // if(*(str1+i+2)!='b') j++; f++; } } //----------------------------- if(z==2&&f%2==0&&j==f) { printf("%s\n",str1); kolvo++; g++; } //----------------------------- } } 
  • And what is the main task of your program? What should she do by condition? And what kind of code is k = 3+rand()%8; nblu;/mjhng b k = 3+rand()%8; nblu;/mjhng b ?? - Harry
  • In that line, I don’t know where the character set came from, probably pressed the keyboard and didn’t notice (I corrected the code). The point is to generate and output a sequence of letters according to a given condition. I did everything, but the sequences that are output sometimes repeat. It should not be. And how to get rid of repetitions, I just can not think of it - Alexey
  • What are the conditions? Maybe an accident to anything? :) - Harry
  • Each sequence begins with ac, in a sequence no more than two seconds, and in a sequence it cannot stand several in a row, only one at a time. The goal is to create a code that selects and displays only what meets the conditions and does not allow the answers to be repeated - Alexey
  • Remember, for example, in an array of the resulting sequence. When you create a new one, see if it already exists and add it if it isn’t found - avp 7:04 pm

1 answer 1

Here is the simplest code that generates 16 non-repeating numbers of length 9 that match your rules:

 char s[] = "acxbxbxbx"; for(int i = 0; i < 16; ++i) { for(int j = 0; j < 4; ++j) s[2+j*2] = 'a' + ((i >> j)&1); cout << s << endl; } 

If you are very attracted to randomness - well, add an insert to a random place of the second character c , for example ...

Well, if you really want your version, then @avp has already suggested to you how ...

For example:

 int main() { set<string> already; for(int i = 0; i < 15; ++i) { string s; for(;;) { s = "ac"; bool c2 = false; for(int l = rand()%5+3; l >= 0; --l) { string choice = "b"; if (!c2) choice += 'c'; if (s.back() != 'a') choice += 'a'; char c = choice[rand()%choice.length()]; if (c == 'c') c2 = true; s += c; } if (!already.count(s)) break; }; already.insert(s); } for(const auto& s: already) cout << s << endl; } 
  • Well, I was initially interested in this, I want to make it so that each set of random numbers is preserved, and then checked against a new random, and in case of a match, it causes a random call again, and so on until the sequence stops repeating. I just can not understand how this can be done ... - Alexey
  • Well, look, so come down? (supplemented the answer). Sorry, but my method did not begin to understand your code ... The main thing is to check if there is already such a line - if (!already.count(s)) break; , and if not, add to the set already.insert(s) . At the exit - in this set the required lines ... - Harry
  • Thank you, I will use your code if I can’t figure it out. But I had an idea to create another array of this type (but I don’t know if you can do this at all?) 'Massiv [g] = {{str1 [0]}, {str1 [1]}, {str1 [2]}, {str1 [3]}, {str1 [4]}, {str1 [5]}, {str1 [6]}, {str1 [7]}, { str1 [8]}}; ' those. This array contains all random numbers that will eventually be displayed. It turns out that it will be possible to create such an array after each randomization and compare it with a new one, and thus avoid duplicates. But there is a problem — I cannot understand what kind of data it is. - Alexey
  • Gives the error "cannot convert brace enclosed initializer list to * (data type) I changed my code so that I could see what I did - Alexey
  • Either it asks to connect -std = c ++ 11, but I don’t know and can’t find how to do it - Alexey