C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

特定のケースに対してN文字のシーケンスを生成するC++プログラム


これは、特定のケースに対してN文字のシーケンスを生成するC++プログラムです。

アルゴリズム

Begin
   function GenerateSequence() generate a Sequence of N Characters for a Given Specific Case:
      Use rand() for generating random indexes.
      Store the first character directly into the sequence.
      If that sequence is used earlier, then it discards that and generates random index again.
End

#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
void GenerateSequence(char string[], int n, int l, char *sequence)
{
   int i, j=0, k,in;
   for(i = 0; i < n; i++) {
      //store first character directly into sequence
      if(j == 0)
         sequence[j++] = string[rand()%l];
      else {
         h:
         in = rand()%l;
         for(k = 0; k < j; k++) {
            if(string[in] == sequence[k])
               goto h;
         }
         sequence[j++] = string[in];
      }
   }
   sequence[j] = '\0'; //end the sequence with null character
}
int main() {
   int n, m, l, i;
   char string[100];
   cout<<"Enter the original string: ";
   cin>>string;
   cout<<"\nEnter the number of strings to be generated from the Base string: ";
   cin>>n;
   cout<<"\nEnter the length of each string to be generated: ";
   cin>>m;
   l = strlen(string);
   for(i = 0; i < n; i++) {
      char sequence[m];
      GenerateSequence(string, m, l, sequence);
      cout<<"\nSequence "<<i+1<<": "<<sequence;
   }
   return 0;
}

出力

Enter the original string: tutorialspoint
Enter the number of strings to be generated from the Base string: 7
Enter the length of each string to be generated: 6
Sequence 1: tuanol
Sequence 2: itlurp
Sequence 3: tonaiu
Sequence 4: untlri
Sequence 5: liorpt
Sequence 6: liusto
Sequence 7: luisot

  1. 与えられた数のエッジに対してランダムな有向非巡回グラフDACを生成するC++プログラム

    このプログラムでは、指定されたエッジ「e」に対してランダムな有向非巡回グラフを生成します。このプログラムの時間計算量はO(e * v * e)です。 アルゴリズム Begin    function GenerateRandomGraphs(), has ‘e’ as the number edges in the argument list.    generate a connection between two random numbers, for sample a small case, limit the number

  2. 特定の検索シーケンスのバイナリ検索アルゴリズムを実装するC++プログラム

    このプログラムでは、配列内の検索シーケンスの存在を見つけるために二分探索を実装する必要があります。二分探索の時間計算量はO(log(n))です。 必要な手順と擬似コード Begin    BinarySearch() function has ‘arr’ the array of data and ‘n’ the number of values, start and end index, iteration count and b[0] be the element to be searched in the argumen