与えられた数を単語に変換するCプログラム
数値で構成される文字列が与えられた場合、タスクはそれらの与えられた数値を単語で隠すことです。
入力「361」があるように。その場合、出力は「三百六十一」という言葉である必要があります。次の問題を解決するには、1、数万、数千などの数と場所を覚えておく必要があります。
コードは最大4桁の数字、つまり0〜9999のみをサポートします。したがって、入力は0〜9999である必要があります。
場所が-
のようになるように1,111を考えてみましょう
例
Input: “1234” Output: one thousand two hundred thirty four Input: “7777” Output: seven thousand seven hundred seventy seven
特定の問題を解決するために使用するアプローチ −
- 入力を文字列として受け取ります。
- さまざまな値の配列を作成します。
- 長さに応じて入力の長さを確認し、どの場所まで出力を表示するかを決定します。
- 場所によると、出力が表示されます。
アルゴリズム
Start
Step 1 → In function convert(char *num)
Declare and initialize int len = strlen(num)
If len == 0 then,
fprintf(stderr, "empty string\n")
Return
End If
If len > 4 then,
fprintf(stderr, "Length more than 4 is not supported\n")
Return
End If
Declare and initialize a char *single_digit[] = { "zero", "one", "two","three", "four","five","six", "seven", "eight", "nine"}
Declare and initialize a char *tens_place[] = {"", "ten", "eleven", "twelve","thirteen", "fourteen","fifteen", "sixteen","seventeen", "eighteen", "nineteen"}
Declare and Initialize a char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty","sixty", "seventy", "eighty", "ninety"}
Declare and initialize char *tens_power[] = {"hundred", "thousand"}
Print num
If len == 1 then,
Print single_digit[*num - '0']
Return
End If
While *num != '\0
If len >= 3
If *num -'0' != 0
Print single_digit[*num - '0']
Print tens_power[len-3]
End If
Decrement len by 1
End If
Else
If *num == '1' then,
Set sum = *num - '0' + *(num + 1)- '0'
Print tens_place[sum]
Return
End If
Else If *num == '2' && *(num + 1) == '0' then,
Print “twenty”
Return
End else If
Else
Set i = *num - '0'
Print i? tens_multiple[i]: ""
Increment num by 1
If *num != '0' then,
Print single_digit[*num - '0']
End If
End Else
Increment num by 1
End Else
End while
Step 2 → In function main()
Call function convert("9132")
Stopを呼び出します 例
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//function to print the given number in words
void convert(char *num) {
int len = strlen(num);
// cases
if (len == 0) {
fprintf(stderr, "empty string\n");
return;
}
if (len > 4) {
fprintf(stderr, "Length more than 4 is not supported\n");
return;
}
// the first string wont be used.
char *single_digit[] = { "zero", "one", "two", "three", "four","five", "six", "seven", "eight", "nine"};
// The first string is not used, it is to make
// array indexing simple
char *tens_place[] = {"", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
// The first two string are not used, they are to make
// array indexing simple
char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty","sixty", "seventy", "eighty", "ninety"};
char *tens_power[] = {"hundred", "thousand"};
// Used for debugging purpose only
printf("\n%s: ", num);
// For single digit number
if (len == 1) {
printf("%s\n", single_digit[*num - '0']);
return;
}
// Iterate while num is not '\0'
while (*num != '\0') {
// Code path for first 2 digits
if (len >= 3) {
if (*num -'0' != 0) {
printf("%s ", single_digit[*num - '0']);
printf("%s ", tens_power[len-3]); // here len can be 3 or 4
}
--len;
}
// Code path for last 2 digits
else {
// Need to explicitly handle 10-19. Sum of the two digits is
//used as index of "tens_place" array of strings
if (*num == '1') {
int sum = *num - '0' + *(num + 1)- '0';
printf("%s\n", tens_place[sum]);
return;
}
// Need to explicitely handle 20
else if (*num == '2' && *(num + 1) == '0') {
printf("twenty\n");
return;
}
// Rest of the two digit numbers i.e., 21 to 99
else {
int i = *num - '0';
printf("%s ", i? tens_multiple[i]: "");
++num;
if (*num != '0')
printf("%s ", single_digit[*num - '0']);
}
}
++num;
}
}
int main() {
convert("9132");
return 0;
} 出力
nine thousand one hundred thirty two
-
Pythonで特定の数のグレイコードを変換するプログラム
番号nがあるとすると、その指定された番号のグレイコード(つまり、n番目のグレイコード)を見つける必要があります。私たちが知っているように、グレイコードは、連続する各数値の値が正確に1ビット異なるように2進数を並べ替える方法です。いくつかのグレイコードは次のとおりです:[0、1、11、10、110、111など] したがって、入力がn =12の場合、12はバイナリで(1100)であるため、出力は10になり、対応するグレイコードは(1010)になり、10進数は10に相当します。 これを解決するには、次の手順に従います。 関数solve()を定義します。これにはnがかかります nが0と同じ場
-
Pythonで数値を整数のリストに変換する
Pythonでのデータ操作の一環として、特定の数値をその数値の数字を含むリストに変換する必要がある場合があります。この記事では、これを実現するためのアプローチについて説明します。 リスト内包表記付き 以下のアプローチでは、str関数を指定された数値に適用してから、恒等関数を介して整数に変換します。最後に、結果をリストにラップします。 例 numA = 1342 # Given number print("Given number : \n", numA) res = [int(x) for x in str(numA)] # Result print("List