惯看花开花又谢,却怕缘起缘又灭…….成为你的护身符 shuangjian_wei@qq.com

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

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

 

posted @ 2017-02-22 10:03  护身符  阅读(133)  评论(0编辑  收藏  举报
宝剑锋从磨砺出 梅花香自苦寒来!
联系我: shuangjian_wei@qq.com