gesp的python二级题目
参考代码1:
#include<iostream>
using namespace std;
int main() {int num; //123cin>>num;int sum = 0;int len = 0;// 判断是否为负数的情况bool b = false;if(num<0) {b = true;num*=-1;}int temp = num;while(temp>0) {temp = temp/10;len+=1; //len用于统计这个数有多少位}while(num>0) {int t = num%10; //3 2 1int a = 1;//记得每次循环都赋值为 1for(int i=1; i<len; i++) {a = a*10;}sum = sum+a*t;len = len-1;num = num/10;}
// 如果为负数,添加符号if(b==true) {cout<<sum*(-1);}
}
参考代码2:
#include<iostream>
using namespace std;
int main() {int num; //123cin>>num;int res = 0;bool b = false;if(num<0) {b = true;num*=-1;}while(num>0) {int t = num%10;//3 2 1res = res*10+t;//3 32 321 【每一次更新res的值】num = num/10;}if(b) {cout<<res*(-1);}
}