我需要使用while和for循环将用户输入的数字设置为单个数字。
我使用while循环来计算数字中的位数,但对于我的for循环,我似乎无法对其进行处理。我究竟做错了什么?
我知道我需要做10^
个数字,但我不知道怎么做,因为C ++中不存在^
。
#include <iostream>
using namespace std;
int main(){
int n;
cout<< "please enter an integer: ";
cin>>n;
int a=n;
int count=0;
while(a>0){
a/=10;
count++;
}
int num= 10*(count-1);
for (int n=0;num<0;n++){
cout<<n/num<<endl;
n%=num;
num/=10;
}
return 0;
}
答案 0 :(得分:0)
对于大于2位的数字,代码失败,因为$scope.results.forEach(function (element) {element.replace(" ","_").toLowercase()});
的值必须num
而不是10^(count-1)
C ++中有一个函数10*(count-1)
,在这种情况下可以用作pow(base,exp)
或者您可以尝试在计算位数时创建num变量:
num=pow(10,count-1)
返回0;
添加最后一行只是为了删除额外的10,因为int count=0,num=1;
while(a>0){
a/=10;
count++;
num*=10;
}
num = num/10;
for (int i=0;i<count;i++){
cout<<n/num<<endl;
n%=num;
num/=10;
}