C++の回文の2乗であるすべての回文を数えます
このチュートリアルでは、回文の二乗である回文の数を見つけるプログラムについて説明します。
このために、2つの値LとRが提供されます。私たちのタスクは、指定された範囲内のスーパーパリンドロームの数を見つけることです。スーパーパリンドロームとは、数とその正方形の両方がパリンドロームであるものです。
例
#include <bits/stdc++.h>
using namespace std;
//checking if the number is a palindrome
bool if_palin(int x){
int ans = 0;
int temp = x;
while (temp > 0){
ans = 10 * ans + temp % 10;
temp = temp / 10;
}
return ans == x;
}
//returning the count of palindrome
int is_spalin(int L, int R){
// Upper limit
int LIMIT = 100000;
int ans = 0;
for (int i = 0 ;i < LIMIT; i++){
string s = to_string(i);
string rs = s.substr(0, s.size() - 1);
reverse(rs.begin(), rs.end());
string p = s + rs;
int p_sq = pow(stoi(p), 2);
if (p_sq > R)
break;
if (p_sq >= L and if_palin(p_sq))
ans = ans + 1;
}
//counting even length palindromes
for (int i = 0 ;i < LIMIT; i++){
string s = to_string(i);
string rs = s;
reverse(rs.begin(), rs.end());
string p = s + rs;
int p_sq = pow(stoi(p), 2);
if (p_sq > R)
break;
if (p_sq >= L and if_palin(p_sq))
ans = ans + 1;
}
return ans;
}
int main(){
string L = "4";
string R = "1000";
printf("%d\n", is_spalin(stoi(L), stoi(R)));
return 0;
} 出力
4
-
C ++ではXで割り切れるが、Yでは割り切れない1からNの範囲の数値をカウントします。
数値Nが提供されます。目標は、YではなくXで割り切れ、[1、N]の範囲にある数値を見つけることです。 例を挙げて理解しましょう。 入力 N=20 X=5 Y=20 出力 Numbers from 1 to N divisible by X not Y: 2 説明 Only 5 and 15 are divisible by 5 and not 10. 入力 N=20 X=4 Y=7 出力 Numbers from 1 to N divisible by X not Y: 5 説明 Numbers 4, 8, 12, 16 and 20 are divisible by
-
C++の平面内の平行四辺形の数
平行四辺形を形成する点を含む平面が与えられます。タスクは、与えられた点を使用して形成できる平行四辺形の数を計算することです。平行四辺形では、四辺形の反対側は平行であるため、反対の角度は等しくなります。 入力 − int a[] = {0, 2, 5, 5, 2, 5, 2, 5, 2} Int b[] = {0, 0, 1, 4, 3, 8, 7, 11, 10} 出力 −平面内の平行四辺形の数− 3 説明 −(x、y)点が与えられ、これらの点を使用して、図に示すように3つの平行四辺形のカウントを形成できます。 入力 − a[] = {0, 3, 1, 4, 1, 5} b