在 C++ 中将长度为“k”的所有子串从基数“b”转换为十进制数
在本教程中,我们将讨论将长度为“k”的所有子串从基数“b”转换为十进制数的程序。
为此,我们将获得一定长度的字符串。我们的任务是从给定的大小为“k”的字符串中获取子串,并将其从“b”基数转换为十进制数。
示例
#include <bits/stdc++.h>
using namespace std;
//converting the substrings to decimals
int convert_substrings(string str, int k, int b){
for (int i=0; i + k <= str.size(); i++){
//getting the substring
string sub = str.substr(i, k);
//calculating the decimal equivalent
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP