python转c++语言_求大神将一段python 语言改写成 C++语言急急急

展开全部

class Solution(object):

def findKthNumber(self, n, k):

1653L = len(str(n))

onezeros = pow(10, L - 1)

weight = int(''.join(['1'] * L)) - 1

path = 0

index = 0

while True:

if index == k:

return path

index += 1

for child in range(10):

if path == 0 and child == 0:

continue

value = path * 10 + child

max_value = (value + 1) * onezeros - 1

if index == k:

return value

if max_value <= n:

max_index = index + weight

else:

if value * onezeros <= n:

dw = weight // 10 + n - value*onezeros

else:

dw = weight // 10 - 1

max_index = index + dw

if max_index >= k:

path = path * 10 + child

onezeros = onezeros // 10

weight = weight // 10 - 1

break

index = max_index + 1

python靠缩进来区分语句块,你的代码没有缩进,假设按照上面这个缩进方式来写的话,转成c++就是:#include

#include

using namesapce std;

class Solution : object

{

public:

int findKthNumber(string n, int k)

{

int L = n.size();

int onezeros = pow(10, L-1);

string str;

for (int i=0; i

{

strcat(str, "1");

}

int weight = atoi(str.c_str()) - 1;

int path =0;

int index = 0;

while (1)

{

if (index == k)

{

return path;

}

index += 1;

for (int child=0; child<10; child++)

{

if ((path==0) && (child==0))

continue;

int value = path*10 + child;

int max_value = (value + 1) * onezeros -1;

if (index == k)

{

return value;

}

if (max_value <= n)

{

max_index = index + weight;

}

else

{

if (value *onezeros <= n)

{

dw = weight/10 + n - value*onezeros;

}

else

{

dw = weight/10 - 1;

}

max_index = index + dw;

}

if (max_index >= k)

{

path = path * 10 + child;

onezeros = onezeros / 10;

weight = weight/10 - 1;

break;

}

}

index = max_index + 1;

}

}

}

继承object看是共有还是私有

要是缩进不一样,类似改下c++就行。

c++里面取的都是int的类型,随需求变化下

findKthNumber是public函数,可随需求改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值