求大神将一段python 语言改写成 C++语言急急急 5
classSolution(object):deffindKthNumber(self,n,k):L=len(str(n))onezeros=pow(10,L-1)wei...
class Solution(object):
def findKthNumber(self, n, k):
L = 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 展开
def findKthNumber(self, n, k):
L = 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 展开
1个回答
展开全部
class Solution(object):
def findKthNumber(self, n, k):
L = 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 <string>
#include <math.h>
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<L; 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函数,可随需求改
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询