当前位置:网站首页>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;
}
边栏推荐
- Bug of Dom4j
- 什么是 CI/CD? | 实现更快更好的软件交付
- Zcmu--5066: dark corridor
- Eureka registers with each other, only showing each other or only showing problems in one
- SQL Server 数据库之备份和恢复数据库
- protobuf 中基础数据类型的读写
- MySQL sorts out the review content -- with mind map
- source insight 使用快捷键
- 两款吾爱破解优秀软件,批量查找文本,图像视频画质增强
- 百度搜索符合预期,但涉及外链黑帽策略,什么原因?
猜你喜欢

Maintenance of delta hot metal detector principle analysis of v5g-jc-r1 laser measurement sensor / detector

(turn) bubble sorting and optimization details

After Europe, it entered Japan and South Korea again, and the globalization of Pico consumer VR accelerated

两款吾爱破解优秀软件,批量查找文本,图像视频画质增强

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

Unity3d tutorial notes - unity initial 04

Eureka registers with each other, only showing each other or only showing problems in one

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

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

SharkTeam完成Flow生态NFT市场MatrixMarket的安全审计
随机推荐
实现瀑布流效果
【云原生】什么是 CI/CD ? | 摆平交付障碍的 CI/CD
Buuctf questions upload labs record pass-01~pass-10
Niuke turns on the camera and the picture disappears a few seconds later | the picture flashes when the camera is turned on
Timing analysis and constraints based on Xilinx
云安全核心技术
SharkTeam完成Flow生态NFT市场MatrixMarket的安全审计
在子组件中使用el-date-picker报错
SQL Server 数据库之备份和恢复数据库
取色器实战(Qt含源码)
[cloud native] what is ci/cd| Ci/cd to smooth delivery obstacles
Maxwell 一款简单易上手的实时抓取Mysql数据的软件
Moco V3: visual self supervision ushers in transformer
【Bluetooth蓝牙开发】八、BLE协议之传输层
微服务架构下的系统集成
ABB电磁流量计维修信号变送器维修41F/E4技术参数
小程序容器技术,让移动研发效率提升500%
After Europe, it entered Japan and South Korea again, and the globalization of Pico consumer VR accelerated
Tested interviewed Zuckerberg: reveal more details of four VR prototypes
Sharkteam completes the safety audit of flow ecological NFT market matrixmarket