北京理工计算机 上机复试2001年(2)

本文提供了三个C++编程实例:绘制等腰梯形、计算阶乘及字符串ASCII排序,涵盖基本输入输出、循环与条件判断、字符串操作等知识点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、  请输入高度h,输入一个高为h,上底边长为h的等腰梯形(例如h=4,图形如下)。

    ****

  ******

 ********

**********

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main(){
 5 int h;
 6 cout<<"please input a num of heigh"<<endl;
 7 cin>>h;
 8 int max=h+2*(h-1);
 9 for(int i=0;i<h;i++){
10     int num=h+i*2;
11     int blank=(max-num)/2;
12     for(int j=0;j<blank;++j)cout<<" ";
13     for(int k=0;k<num;k++)cout<<"*";
14     cout<<endl;
15 }
16 return 0;
17 }//main
2001_01.cpp

2、  请编写一个程序,从键盘上输入n(n的范围是1~20),求n的阶乘。

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main(){
 5 int n;
 6 cout<<"please input a num 1-20"<<endl;
 7 cin>>n;
 8 double res=1;//用int会溢出
 9 for(int i=1;i<=n;i++)res*=i;
10 cout<<res<<endl;
11 
12 return 0;
13 }//main
2001_02.cpp

3、  从键盘上任意输入一个长度不超过20的字符串,对所输入的字符串,按照ASCII码的大小从小到大进行排序,请输出排序后的结果。

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 #include<algorithm>
 5 
 6 
 7 
 8 int main(){
 9 string s;
10 cout<<"please input a string "<<endl;
11 cin>>s;
12 sort(s.begin(),s.end());
13 cout<<s<<endl;
14 return 0;
15 }//
2001_03.cpp

 

转载于:https://www.cnblogs.com/Alan-Wei/p/6427558.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值