C ++ STLを使用して、指定された範囲の素数を出力します
与えられた範囲の素数を印刷するプログラムです。
アルゴリズム
Begin Declare a user define datatype stl. Declare a vector number to the stl datatype. Declare variable a to the stl datatype. Declare vector Prime_Number to the Boolean datatype. Prime_Number[0] = false. Prime_Number[1] = false. Declare b to the integer datatype. Initialize b = sqrt(a). for (stl pr=2; pr<=b; pr++) if (Prime_Number[pr]) then for (stl i=pr*2; i<=a; i += pr) Prime_Number[i] = false. Declare result to the vector type. for (int i=0;i<a;i++) if (Prime_Number[i]) then result.push_back(i). return result. End Begin Declare function remove_zero() to the Boolean type. Declare i to the stl datatype. Return i = 0. End Begin Declare a vector Number_Range to the stl datatype. Declare First_Num, Last_Num to the stl datatype. Pass them as parameter. Declare two vector s1 and s2 to the stl datatype. Initialize s1 = number(First_Num) to find primes from o to First_Num. Initialize s2 = number(Last_Num) to find primes from o to Last_Num. Declare vector result(Last_Num-First_Num) to the stl datatype. Call set_difference(s2.begin(), s2.end(), s1.begin(), s2.end(), result.begin()) to find set difference of two vectors. Declare an iterator itr to the vector type. Initialize itr = remove_if(result.begin(),result.end(),remove_zero) to remove extra zeros. Call resize (itr-result.begin()) to resize the result. Return result. Declare First_Num, Last_Num to the stl datatype. Initialize First_Num = 20, Last_Num = 50. Declare a vector result to the stl datatype. Initialize result = Number_Range(First_Num,Last_Num). Print "The Prime Numbers from 20 to 50 are: “ for (auto i:result) Print the values of i. End.
例
#include<bits/stdc++.h> using namespace std; typedef unsigned long long int stl; vector<stl> number(stl a) { vector<bool> Prime_Number(a+1,true); Prime_Number[0] = false; Prime_Number[1] = false; int b = sqrt(a); for (stl pr=2; pr<=b; pr++) { if (Prime_Number[pr]) { for (stl i=pr*2; i<=a; i += pr) Prime_Number[i] = false; } } vector<stl> result; for (int i=0;i<a;i++) if (Prime_Number[i]) result.push_back(i); return result; } bool remove_zero(stl i) { // Used to remove zeros from a vector return i == 0; } vector<stl> Number_Range(stl First_Num,stl Last_Num) { vector<stl> s1 = number(First_Num); // find primes from o to First_Num vector<stl> s2 = number(Last_Num); // find primes from o to Last_Num vector<stl> result(Last_Num-First_Num); set_difference(s2.begin(), s2.end(), s1.begin(), s2.end(), result.begin()); // find set //difference of two vectors vector<stl>::iterator itr = remove_if(result.begin(),result.end(),remove_zero); //remove extra zeros. result.resize(itr-result.begin()); return result; } int main(void) { stl First_Num = 20, Last_Num = 50; vector<stl> result = Number_Range(First_Num,Last_Num); cout<<"The Prime Numbers from "<<First_Num<<" to "<<Last_Num<< " are: "; for (auto i:result) cout<<i<<' '; return 0; }
出力
The Prime Numbers from 20 to 50 are: 23 29 31 37 41 43 47
-
C++で指定された範囲内のK個の奇数の約数を持つ数を検索します
この問題では、L、R、およびkの3つの整数値が与えられます。私たちの仕事は、与えられた範囲でK個の奇数の約数を持つ数を見つけることです。正確にk個の除数を持つ[L、R]の範囲の数の数を見つけます。 1と数自体を除数として数えます。 問題を理解するために例を見てみましょう 入力 a = 3, b = 10, k = 3 出力 2 説明 Numbers with exactly 3 divisors within the range 3 to 10 are 4 : divisors = 1, 2, 4 9 : divisors = 1, 3, 9 ソリューションアプローチ この問題の簡単な
-
指定された範囲でBSTキーを出力します-O(1)スペース(C ++)
この問題では、2つの値k1とk2(k1