配列に指定された値が含まれているかどうかを確認するJavaプログラム
この記事では、配列に指定された値が含まれているかどうかを確認する方法を理解します。これは、配列要素を反復処理し、指定された入力を配列要素と比較することで実現されます。
以下は同じのデモンストレーションです-
入力
入力が-
であると仮定しますEnter the number to be searched: 25 The elements in the integer array: 15 20 25 30 35
出力
必要な出力は-
になりますThe array contains the given value
アルゴリズム
Step 1 - START Step 2 - Declare three integer values namely my_input , i, array_size. A Boolean value my_check is defined and an integer array my_array is defined Step 3 - Read the required values from the user/ define the values Step 4 - Iterate the elements using a for loop and compare the values of the given input with in array values. Step 5 - If the values match, the element is present. If not, the element is not present. Step 6 - Display the result Step 7 - Stop
例1
ここでは、プロンプトに基づいてユーザーが入力を入力しています。この例は、コーディンググラウンドツールでライブで試すことができます
。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int my_input , i, array_size;
boolean my_check = false;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("A reader object has been defined ");
System.out.println("Enter the number to be searched ");
my_input = my_scanner.nextInt();
System.out.print("Enter the value of array_size : ");
array_size = my_scanner.nextInt();
int my_array[] = new int[array_size];
System.out.println("Enter the elements of the array :" );
for ( i = 0 ; i < array_size ; i++ ){
my_array[i] = my_scanner.nextInt();
}
for ( i = 0 ; i < array_size ; i++ ) {
if (my_array[i] == my_input) {
my_check = true;
break;
}
}
if(my_check)
System.out.println("\nThe array contains the given value");
else
System.out.println("\nThe array doesnot contain the given value");
}
} 出力
Required packages have been imported A reader object has been defined Enter the number to be searched 25 Enter the size of array : 5 Enter the elements of the array : 10 15 20 25 30 The array contains the given value
例2
ここでは、整数は事前に定義されており、その値にアクセスしてコンソールに表示されます。
public class Main {
public static void main(String[] args) {
int[] my_array = {15, 20, 25, 30, 35, 40};
int my_input , i, array_size;
array_size = 5;
my_input = 25;
boolean my_check = false;
System.out.println("The number is defined as " +my_input);
System.out.println("The elements in the integer array is defined as :" );
for ( i = 0 ; i < array_size ; i++ ){
System.out.print(my_array[i] +" ");
}
for ( i = 0 ; i < array_size ; i++ ) {
if (my_array[i] == my_input) {
my_check = true;
break;
}
}
if(my_check)
System.out.println("\nThe array contains the given value");
else
System.out.println("\nThe array doesnot contain the given value");
}
} 出力
The number is defined as 25 The elements in the integer array is defined as : 15 20 25 30 35 The array contains the given value
-
指定された文字列がパングラムであるかどうかを確認するPythonプログラム
この記事では、特定の問題ステートメントを解決するための解決策とアプローチについて学習します。 問題の説明 文字列入力が与えられた場合、その文字列がパングラムであるかどうかを確認するPythonプログラムを生成する必要があります。 パングラムは、英語のアルファベットコレクションのすべての文字を含む文/一連の単語です。 では、問題を解決する方法を見てみましょう 入力文字列に存在する各文字が、手動で宣言するアルファベットセットに属しているかどうかをチェックするループを使用します。 上記のアプローチの実装は、-によって与えられます。 例 import string def ispangram
-
指定された配列が単調であるかどうかを確認するPythonプログラム
この記事では、特定の問題ステートメントを解決するための解決策とアプローチについて学習します。 問題の説明 n個の整数を含む配列入力Arrが与えられます。入力配列が本質的に単調であるかどうかを確認する必要があります。 アレイが継続的に増加または継続的に減少している場合、そのアレイは本質的に単調であると言われます。 数学的に すべてのi<=j、の場合、配列Aは継続的に増加します。 A[i] <= A[j]. すべてのi<=j、の場合、配列Aは継続的に減少しています。 A[i] >= A[j]. ここでは、隣接するすべての要素が上記の条件のいずれかを満たしているかどうかを確