Cプログラムの文に最長の回文語を印刷する
与えられた文と課題は、与えられた文から最長の回文を見つけることです
回文とは何ですか?
回文は、文字列を逆にしても意味が同じである単語またはシーケンスです
例 −ニチン、文字列を逆にした後も、その意味は変わりません。
課題は、与えられた文から最長の回文を見つけることです。
文のように:マラヤーラム語liemadameil iji
3つの回文の単語が含まれていますが、最長は-liemadameil
アルゴリズム
START STEP 1 -> Declare start variables I, j, k, l, max to 0, index to -1, check to 0, count to 0 Step 2 -> Loop For i to 0 and i<strlen(str) and i++ Set max =0, k =i and j=i+1 Loop While str[j]!=' ' and str[j]!='\0' Increment j by 1 End While Set l=j-1 IF str[k]!=' ' and str[k]!='\0' Loop While k<=1 If str[k]==str[l] Increment max by 1 If count<=max Set index=i and count = max End If End IF Else Set max = 0, count = -1 Break End Else Increment k and I by 1 End Loop While End If Set i=j Step 3 -> End Loop For Step 4 -> Loop For i = index and i!=-1 && str[i]!=' ' && str[i]!='\0' and i++ Print str[i] Step 5 -> End Loop For STOP
例
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[]) {
char str[] = {"malayalam liemadameil iji"};
int i, k, l, j, max =0, index = -1, check = 0, count = 0;
for(i=0; i<strlen(str); i++) {
max = 0;
k = i;
j = i+1;
while(str[j]!=' ' && str[j]!='\0'){
j++;
}
l = j-1;
if(str[k]!=' ' && str[k]!='\0') {
while(k<=l) {
if (str[k]==str[l]) {
max++;
if(count<=max) {
index = i;
count = max;
}
} else {
max = 0;
count = -1;
break;
}
k++;
l--;
}
}
i = j;
}
for (i = index; i!=-1 && str[i]!=' ' && str[i]!='\0'; i++) {
printf("%c", str[i]);
}
return 0;
} 出力
上記のプログラムを実行すると、次の出力が生成されます。
liemadameil
-
Cで数値列を賢く印刷するプログラム
プログラムの説明 以下に示すように、自然数の列を賢く印刷します 1 2 6 3 7 10 4 8 11 13 5 9 12 14 15 アルゴリズム i stands for rows and j stands for columns. 5 stands for making pattern for 5 Rows and Columns Loop for each Row (i) K is initialized to i Loop for each Column (j) Do the Pattern for the current Column (j) Display the Value
-
Cで数字パターンを印刷するプログラム
プログラムの説明 数値パターンは、パターンルールと呼ばれるルールに基づいて作成された一連の数字です。パターンルールでは、1つ以上の数学演算を使用して、シーケンス内の連続する数字間の関係を記述できます。 パターンの例 パターン1 1 2 6 3 7 10 4 8 11 13 5 9 12 14 15 パターン2 1 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 1 2 3 4 5 1 2 3 1 アルゴリズム Pattern 1