我的思路是利用while循环不断求出余数然后依次输出每一位数字,但运行结果没有达到我预期的效果
#include
#include<math.h>
using namespace std;
int main()
{
int n,x,y,i=0;
cout<<"请输入一个数的位数:";
cin>>n;
cout<<"请输入一个"<<n<<"位数:";
cin>>x;
int t= pow(10,n);
while(i<=n)
{
y=x%t;
i++;
}
cout<<"这个数每一位上的数字为:"<<y;
return 0;
}
你题目的解答代码如下:(如有帮助,望采纳!谢谢! 点击我这个回答右上方的【采纳】按钮)
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int n,x,y,t;
cout<<"请输入一个数的位数:";
cin>>n;
cout<<"请输入一个"<<n<<"位数:";
cin>>x;
cout<<"这个数每一位上的数字为:";
while(n-->0)
{
t= pow(10,n);
y=x/t%10;
cout<< y <<" ";
}
return 0;
}
运行结果
请输入一个数的位数:5
请输入一个5位数:87654
这个数每一位上的数字为:8 7 6 5 4
你的t也需要变化的。而且输出在循环内
int main()
{
int n,x,y,i=0;
cout<<"请输入一个数的位数:";
cin>>n;
cout<<"请输入一个"<<n<<"位数:";
cin>>x;
int t= pow(10,n-1);
while(i<n)
{
y=x/t;
x=x%t;
t=t/10;
i++;
cout<<"这个数每一位上的数字为:"<<y<<" ";
}
return 0;
}
#include "iostream"
#include<math.h>
using namespace std;
int main()
{
int n,x,y,i=0;
cout<<"请输入一个数的位数:";
cin>>n;
cout<<"请输入一个"<<n<<"位数:";
cin>>x;
while(n>=0)
{
y=n%10;
cout<<"这个数每一位上的数字为:"<<y;
n=n/10;
}
return 0;
}