c++输入若干个数,将这些数打印输出为4列若干行的形式,要求每列对齐,如果输入的有小数,则保留小数点后2位。

一、输入若干个数,将这些数打印输出为4列若干行的形式,要求每列对齐,如果输入的有小数,则保留小数点后2位。
#include

/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
using namespace std;
#include

using std::setw;
int main() {
cout.precision(2);
cout.setf(ios::fixed);//保留两位小数
int m=0,n;
cout<<“请输入要输入的个数:”;
cin>>n;
double a[10]={};
for(int i=0;i<n;i++){
cin>>a[i];
}
// int n=sizeof(a)/sizeof(int);
for(int j=0;j<n;j++){
cout<<setw(10)<<a[j];
m++;
if(m%4==0){
cout<<“\n”;
}

}
	
return 0;

}
二、输入图形的行数,利用循环打印输出以下形式的图形(以下图形是以输入5为例)
*
**






**
*
#include

/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
using namespace std;
int main() {
int n;
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++)
{

		cout<<"*";
	}
	cout<<"\n";
}
for(int m=n;m>0;m--){
	for(int n=1;n<m;n++)
	{
		cout<<"*";
	}
	cout<<"\n";
}
return 0;

}
三、输入一个整数,输出该整数所有素数因子。
#include

/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */

using namespace std;

int main()
{
int n;
cout<<“请输入一个整数:”;
cin>>n;
for(int i=2;i<=n;){

	if(n%i==0)
	{	
		n=n/i;
	    cout<<"素数因子为:"<<i<<" "<<endl;		
	    
	}else{
		i++;
	
	}
} 
return 0;

}

四、“水仙花数”是指一个三位数,其各位数字立方和等于该数本身,编程求出所有的水仙花数。
#include

/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */

using namespace std;

int main()
{
int m=1000;
int a,b,c,d;
for (int n=100;n<m;n++)
{
a=n/100;//第一位数字
b=n/10%10;
c=n%100%10;
d=aaa+bbb+ccc;
if(d==n){
cout<<n<<“\t”;
}
}
return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值