在本教程中,我们将讨论一个程序,该程序将所有长度为'k'的子字符串从基数'b'转换为十进制。
为此,我们将提供一定长度的字符串。我们的任务是从大小为'k'的给定字符串中获取子字符串,并将其从基数'b'转换为十进制数。
#include <bits/stdc++.h> using namespace std; //将子字符串转换为小数 int convert_substrings(string str, int k, int b){ for (int i=0; i + k <= str.size(); i++){ //获取子字符串 string sub = str.substr(i, k); //计算十进制当量 int sum = 0, counter = 0; for (int i = sub.size() - 1; i >= 0; i--){ sum = sum + ((sub.at(i) - '0') * pow(b, counter)); counter++; } cout << sum << " "; } } int main(){ string str = "12212"; int b = 3, k = 3; convert_substrings(str, b, k); return 0; }
输出结果
17 25 23