Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c++;程序运行正常,但同一个python程序运行不正常_Python_C++ - Fatal编程技术网

c++;程序运行正常,但同一个python程序运行不正常

c++;程序运行正常,但同一个python程序运行不正常,python,c++,Python,C++,我的任务是: C2[9]。给定自然数N。从数字中删除第三位数字(从左起)。例#1。N=1256;答复:126。例2。N=432;答复:43。例如#3。N=98;答复:98 我在C++中编写了代码,并根据任务进行工作。我在python中尝试了相同的任务,但是输出错误 以下是python中的代码: x = 10292989 xx = x if x <= 99: print(x) else: d=1 while x > 999: x//10

我的任务是: C2[9]。给定自然数N。从数字中删除第三位数字(从左起)。例#1。N=1256;答复:126。例2。N=432;答复:43。例如#3。N=98;答复:98

我在C++中编写了代码,并根据任务进行工作。我在python中尝试了相同的任务,但是输出错误

以下是python中的代码:

x = 10292989
xx = x

if x <= 99:
    print(x)
else:
    d=1
    while x > 999:
        x//10
        d**10
    
    x//10
    fin = x * d + xx % d
    print(fin)
x=10292989
xx=x
如果x 999:
x//10
d**10
x//10
fin=x*d+xx%d
打印(财务)
以下是c++中的代码:

#include<iostream>
using namespace std;

int c2(int);

int main(){
int x = 10292989;

cout<<c2(x)<<endl;
}

int c2(int x){
    int d=1;
    int xx = x;
    int fin;
    if(x <= 99){
        return x;
    }
    else{
        while(x > 999){
            x /= 10;
            d *= 10;
        }

        x/=10;

        return fin = x * d + xx % d;
    }
}
#包括
使用名称空间std;
int c2(int);
int main(){
INTX=10292989;

cout此处
x//10
仅在内存中更改变量,而不覆盖变量,因此请尝试(
x=x//10
):

x=10292989
xx=x
如果x 999:
x=x//10
d=d**10
x=x//10
fin=x*d+xx%d
打印(财务)

x//10
不会改变
x
。它需要是
x=x//10
x//=10
我如何改变x,就像你改变任何其他变量一样:分配一些东西给它。就像你在程序开始时把x改成
10292989
一样,或者像你在程序附近改变
fin
一样e结束。看起来您正在尝试翻译代码。翻译很难。通常比从头开始编写代码更难。为了成功地翻译代码,您需要对两种语言都有足够深入的了解,以理解源语言中代码的行为,并在另一种语言中重新实现。这类似于翻译spoken language。如果你不明白如何用两种语言产生相同的潜台词和上下文,那么你最终会陷入一种风格的胡说八道。
d**10
应该是
d=d**10
@Barmar谢谢你的帮助,很遗憾它仍然不起作用
x = 10292989
xx = x

if x <= 99:
    print(x)
else:
    d=1
    while x > 999:
        x = x//10
        d = d**10
    
    x = x//10
    fin = x * d + xx % d
    print(fin)