N 0 এবং M 1 এর ক্রমটি এমন হওয়া উচিত যাতে এইভাবে গঠিত ক্রমটিতে তিনটি পরপর 1 এর সাথে পরপর দুটি 0 না থাকে।
ইনপুট − N=5 M=9
আউটপুট − 1 1 0 1 1 0 1 1 0 1 0 1 0 1
দ্রষ্টব্য − উপরের ক্রমটি তৈরি করতে, বিবৃতিটি (m
নীচে দেওয়া সমাধানে সরাসরি ঝাঁপ না দিয়ে প্রথমে প্রশ্নের যুক্তির মধ্য দিয়ে যাওয়া এবং নিজেকে চেষ্টা করার পরামর্শ দেওয়া হচ্ছে৷
অ্যালগরিদম
START Step 1 -> take values in ‘n’ and ‘m’ Step 2 -> Loop IF m=n-1 Loop While m>0 and n>0 Print 01 Decrement m and n by 1 End Loop While Loop IF n!=0 Print 0 End IF Loop IF m!=0 Print 1 End IF Step 3-> Else (m < n-1) || m >= 2 * (n + 1) Print cn’t have sequence for this Step 4 -> Else Loop While m-n > 1 && n > 0 Print 1 1 0 Decrement m by 2 and n by 1 End While Loop While n>0 Print 1 0 Decrement m and n by 1 End While Loop While m>0 Print 1 Decrement m by 1 End While Step 5-> End Else STOP
উদাহরণ
#include <stdio.h>
#include <math.h>
int main() {
int n =5, m=9;
if( m == n-1 ) { //If m is 1 greater than n then consecutive 0's and 1's
while( m > 0 && n > 0 ) { //Loop until all m's and n's
printf("01");
m--;
n--;
}
if ( n!=0 ) //Print the remaining 0
printf("0");
if( m!=0 ) //Print the remaining 1
printf("1");
}
else if ( (m < n-1) || m >= 2 * (n + 1) ) { //If this is true the sequence can't be made
printf("Can't have sequence for this\n");
} else {
while( m-n > 1 && n > 0 ) {
printf("1 1 0 ");
m -= 2;
n--;
}
while ( n > 0 ) {
printf("1 0 ");
n--;
m--;
}
while ( m > 0 ) {
printf("1 ");
m--;
}
}
return 0;
} আউটপুট
যদি আমরা উপরের প্রোগ্রামটি চালাই তবে এটি নিম্নলিখিত আউটপুট তৈরি করবে।
1 1 0 1 1 0 1 1 0 1 0 1 0 1