当前位置:网站首页>1945. 字符串转化后的各位数字之和
1945. 字符串转化后的各位数字之和
2022-07-28 19:20:00 【Mr Gao】
1945. 字符串转化后的各位数字之和
给你一个由小写字母组成的字符串 s ,以及一个整数 k 。
首先,用字母在字母表中的位置替换该字母,将 s 转化 为一个整数(也就是,‘a’ 用 1 替换,‘b’ 用 2 替换,… ‘z’ 用 26 替换)。接着,将整数 转换 为其 各位数字之和 。共重复 转换 操作 k 次 。
例如,如果 s = “zbax” 且 k = 2 ,那么执行下述步骤后得到的结果是整数 8 :
转化:"zbax" * "(26)(2)(1)(24)" * "262124" * 262124
转换 #1:262124 * 2 + 6 + 2 + 1 + 2 + 4 * 17
转换 #2:17 * 1 + 7 * 8
返回执行上述操作后得到的结果整数。
示例 1:
输入:s = “iiii”, k = 1
输出:36
解释:操作如下:
- 转化:“iiii” * “(9)(9)(9)(9)” * “9999” * 9999
- 转换 #1:9999 * 9 + 9 + 9 + 9 * 36
因此,结果整数为 36 。
示例 2:
输入:s = “leetcode”, k = 2
输出:6
解释:操作如下:
- 转化:“leetcode” * “(12)(5)(5)(20)(3)(15)(4)(5)” * “12552031545” * 12552031545
- 转换 #1:12552031545 * 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 * 33
- 转换 #2:33 * 3 + 3 * 6
因此,结果整数为 6 。
这题还是很有趣的解题代码如下:
int f(int num){
int sum=0;
while(num){
sum=sum+num%10;
num=num/10;
}
return sum;
}
int getLucky(char * s, int k){
int sum=0;
int i;
for(i=0;s[i]!='\0';i++){
int num=s[i]-'a'+1;
if(num<10){
sum=sum+num;
}
else{
while(num){
sum=sum+num%10;
num=num/10;
}
}
}
while(k>=2){
sum=f(sum);
k--;
}
return sum;
}
边栏推荐
- ctfshow 做题 web模块 web11~web14
- 《软件设计师考试》易混淆知识点
- ctfshow 网络迷踪做题记录(2)
- 蓝队入门之效率工具篇
- Huawei cloud digital asset chain, "chain" connects the digital economy, infinite splendor
- Explain prefabrication in unity in detail
- Space shooting Lesson 11: sound and music
- What is low code? Which platforms are suitable for business personnel? Is it reliable to develop the system?
- Unity foundation 5-optimization strategy
- Job CE
猜你喜欢

Applet container technology improves mobile R & D efficiency by 500%

Space shooting Lesson 10: score (painting and writing)

Moco V2: further upgrade of Moco series

Unity - Fundamentals of 3D mathematics

一名在读研究生的自白:我为什么会沉迷于openGauss 社区?

Job CE

【云原生】什么是 CI/CD ? | 摆平交付障碍的 CI/CD

Interesting pictures and words

广和通&高通物联网技术开放日成功举办

Lazada店铺如何产号高效补单?(测评自养号技术详解篇)
随机推荐
High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation
Lvs+keepalived high availability deployment practical application
What is low code? Which platforms are suitable for business personnel? Is it reliable to develop the system?
Baklib|为什么说企业需要重视客户体验?
EfficientFormer:轻量化ViT Backbone
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
Link with Bracket Sequence I(状态基多维dp)
SSM-使用@Async和创建ThreadPoolTaskExecutor线程池
Looking at SQL optimization from the whole process of one query
Explain mesh Collider in unity
setPreviewCallbackWithBuffer的出帧效率会变低
Guo Mingxuan: meta contraction is conducive to the development of VR competitors, and apple XR headshow will change the industry rules
Reading and writing basic data types in protobuf
Integrating database Ecology: using eventbridge to build CDC applications
4.2 Virtual Member Functions
融合数据库生态:利用 EventBridge 构建 CDC 应用
Integrating database Ecology: using eventbridge to build CDC applications
Database -- use of explain
mysql还有哪些自带的函数呢?别到处找了,看这个就够了。
蓝队入门之效率工具篇