C++で数値を負数進表現に変換します
このチュートリアルでは、数値を負数進表現に変換するプログラムについて説明します。
このために、数値とそれに対応する負数進法が提供されます。私たちの仕事は、与えられた数をその負数進に相当するものに変換することです。負数進値には、-2から-10までの値のみを許可しています。
例
#include <bits/stdc++.h>
using namespace std;
//converting integer into string
string convert_str(int n){
string str;
stringstream ss;
ss << n;
ss >> str;
return str;
}
//converting n to negative base
string convert_nb(int n, int negBase){
//negative base equivalent for zero is zero
if (n == 0)
return "0";
string converted = "";
while (n != 0){
//getting remainder from negative base
int remainder = n % negBase;
n /= negBase;
//changing remainder to its absolute value
if (remainder < 0) {
remainder += (-negBase);
n += 1;
}
// convert remainder to string add into the result
converted = convert_str(remainder) + converted;
}
return converted;
}
int main() {
int n = 9;
int negBase = -3;
cout << convert_nb(n, negBase);
return 0;
} 出力
100
-
Nの基数B表現で後続ゼロの数を見つけます! C++を使用する
この記事では、階乗のベースB表現で特定の数Nの後続ゼロを見つける問題を理解します。例 Input : N = 7 Base = 2 Output : 4 Explanation : fact(7) = 5040 in base10 and 1001110110000 in base16 having 4 trailing zero. Input : N = 11 Base = 5 Output : 2 Explanation : fact(11) = 39916800 in base10 and 40204314200 in base16 having 2 trailing zeroes.
-
Nの基数16表現で後続ゼロの数を見つけます! C++を使用する
この記事では、たとえば階乗の基数16の表現で特定の数Nの後続ゼロを見つける問題を理解します Input : N = 7 Output : 1 Explanation : fact(7) = 5040 in base10 and 13B0 in base16 having 1 trailing zero. Input : N = 11 Output : 2 Explanation : fact(11) = 39916800 in base10 and 2611500 in base16 having 2 trailing zeroes. まず、10進数を1つの基数から別の基数に変換するプロセ