当前位置:网站首页>1945. sum of digits after string conversion
1945. sum of digits after string conversion
2022-07-28 21:24:00 【Mr Gao】
1945. The sum of the numbers after string conversion
Give you a string of lowercase letters s , And an integer k .
First , Replace the letter with its position in the alphabet , take s conversion It's an integer ( That is to say ,‘a’ use 1 Replace ,‘b’ use 2 Replace ,… ‘z’ use 26 Replace ). next , The integer transformation For its The sum of your numbers . Repeat transformation operation k Time .
for example , If s = “zbax” And k = 2 , Then the result obtained after performing the following steps is an integer 8 :
conversion :"zbax" * "(26)(2)(1)(24)" * "262124" * 262124
transformation #1:262124 * 2 + 6 + 2 + 1 + 2 + 4 * 17
transformation #2:17 * 1 + 7 * 8
Returns the result integer after performing the above operation .
Example 1:
Input :s = “iiii”, k = 1
Output :36
explain : The operation is as follows :
- conversion :“iiii” * “(9)(9)(9)(9)” * “9999” * 9999
- transformation #1:9999 * 9 + 9 + 9 + 9 * 36
therefore , The result integer is 36 .
Example 2:
Input :s = “leetcode”, k = 2
Output :6
explain : The operation is as follows :
- conversion :“leetcode” * “(12)(5)(5)(20)(3)(15)(4)(5)” * “12552031545” * 12552031545
- transformation #1:12552031545 * 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 * 33
- transformation #2:33 * 3 + 3 * 6
therefore , The result integer is 6 .
This problem is still very interesting. The solution code is as follows :
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;
}
边栏推荐
- 怎么理解数据网格(Data Mesh)
- Unity - Fundamentals of 3D mathematics
- Mobilevit: challenge the end-to-side overlord of mobilenet
- 小程序容器技术,让移动研发效率提升500%
- Guo Mingxuan: meta contraction is conducive to the development of VR competitors, and apple XR headshow will change the industry rules
- Two excellent software of my love cracking, batch search text, image and video image quality enhancement
- Introduction to blue team: efficiency tools
- 向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
- 关键路径的分析
- (转)冒泡排序及优化详解
猜你喜欢

MySQL sorts out the review content -- with mind map

智能家居行业发展,密切关注边缘计算和小程序容器技术

Confession of a graduate student: why am I addicted to opengauss community?

到底为什么不建议使用SELECT * ?

Unity3d tutorial notes - unity initial 04

Mobilevit: challenge the end-to-side overlord of mobilenet

Why on earth is it not recommended to use select *?

小程序容器技术,让移动研发效率提升500%

Coding with these 16 naming rules can save you more than half of your comments!

BUUCTF做题Upload-Labs记录pass-11~pass-20
随机推荐
What is ci/cd| Achieve faster and better software delivery
Buuctf questions upload labs record pass-01~pass-10
Link with Bracket Sequence I(状态基多维dp)
什么是 CI/CD? | 实现更快更好的软件交付
(turn) bubble sorting and optimization details
30.学习Highcharts 标签旋转柱形图
The greatest romance of programmers~
证券企业基于容器化 PaaS 平台的 DevOps 规划建设 29 个典型问题总结
System integration under microservice architecture
Database -- use of explain
Unity3d tutorial notes - unity initial 03
How NPM switches Taobao source images
ABB electromagnetic flowmeter maintenance signal transmitter maintenance 41f/e4 technical parameters
A 58 year old native of Anhui Province, he has become the largest IPO investor in Switzerland this year
Color finder actual combat (QT including source code)
How to select and build the container cloud platform that hosts the key applications of the bank?
速卖通测评自养号,国外环境如何搭建?需要多少成本?
Dom4J的Bug
又一款装机神器
工业通讯领域的总线、协议、规范、接口、数据采集与控制系统