一、输入若干个数,将这些数打印输出为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;
}