最近的字符 | 时间限制:1秒 | 内存限制:262144K 编辑
限定语言:不限
请使用程序求出字符串str中每个字符距离制定字符c 最近的距离,返回int数组表示字符串所有字符到指定字符的距离。
备注:
请参照样例理解题目意思。
示例1
输入
"nowcode",w
输出
[2,1,0,1,2,3,4]
说明
字符串为nowcode,指定字符为w,字符串中每个字符到w的距离为字符下标的之差,即返回:[2,1,0,1,2,3,4]
示例2
输入
"nowcode",o
输出
[1,0,1,1,0,1,2]
说明
字符串为nowcode,指定字符为w,字符串中每个字符到w的距离为字符下标的之差,即返回:[1,0,1,1,0,1,2]
| 时间限制:1秒 | 内存限制:262144K 编辑
限定语言:不限
请使用程序求出字符串str中每个字符距离制定字符c 最近的距离,返回int数组表示字符串所有字符到指定字符的距离。
备注:
请参照样例理解题目意思。
示例1
输入
"nowcode",w
输出
[2,1,0,1,2,3,4]
说明
字符串为nowcode,指定字符为w,字符串中每个字符到w的距离为字符下标的之差,即返回:[2,1,0,1,2,3,4]
示例2
输入
"nowcode",o
输出
[1,0,1,1,0,1,2]
说明
字符串为nowcode,指定字符为w,字符串中每个字符到w的距离为字符下标的之差,即返回:[1,0,1,1,0,1,2]
#include <iostream>
#include <string>
using namespace std;
int t3(int num)
{
if(to_string(num).length() == 1)
{
return num;
}
string s = to_string(num);
int j = 0;
for(int i = 0;i < s.length();i++)
{
j= j+s[i] -'0';
}
return t3(j);
}
int main() {
cout << t3(258) << endl; // 输出 6
return 0;
}