长沙网站建设做得好的/写软文
问题 A: 输出梯形
时间限制: 1 Sec 内存限制: 32 MB
题目描述
输入一个高度h,输出一个高为h,上底边为h的梯形。
输入
一个整数h(1<=h<=1000)。
输出
h所对应的梯形。
样例输入
5
样例输出
********************************
*************
代码:
#include <iostream>
using namespace std;
int main()
{int h;while(cin>>h){int m=h+(h-1)*2;for(int i=0;i<h;i++){for(int j=0;j<m-h-2*i;j++){cout<<" ";}for(int j=0;j<h+2*i;j++){cout<<"*";}cout<<endl;}}return 0;
}
问题 B: Hello World for U
时间限制: 1 Sec 内存限制: 32 MB
题目描述
Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. For example, "helloworld" can be printed as:
h d
e l
l r
lowo
That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters. And more, we would like U to be as squared as possible -- that is, it must be satisfied that n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.
输入
Each input file contains one test case. Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space.
输出
For each test case, print the input string in the shape of U as specified in the description.
样例输入
helloworld!
样例输出
e d
l l
lowor
分析:关键n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } 关键max
先求n1和n3,正好整除向下取整,然后n2向里面加肯定最大
答案:
#include<iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{string s;int N,n1,n2,n3;while(cin>>s){N=s.length(); n1=n3=(N+2)/3;n2=(N+2)-n1-n3;for(int i=0;i<n1-1;i++){cout<<s[i];for(int i=0;i<n2-2;i++){cout<<" ";}cout<<s[N-i-1]<<endl;}for(int i=n1-1;i<=N-n1;i++){cout<<s[i];}cout<<endl;}return 0;
}
问题 C: 等腰梯形
时间限制: 1 Sec 内存限制: 32 MB
提交: 329 解决: 209
[提交][状态][讨论版][命题人:外部导入]
题目描述
请输入高度h,输入一个高为h,上底边长为h 的等腰梯形(例如h=4,图形如下)。
****
******
********
**********
输入
输入第一行表示样例数m,接下来m行每行一个整数h,h不超过10。
输出
对应于m个case输出要求的等腰梯形。
样例输入
1
4
样例输出
******************
**********
代码:
#include <iostream>using namespace std;int main()
{int T;int h;while(cin>>T){while(T--){cin>>h;for(int i=0;i<h;i++){for(int j=0;j<h-i-1;j++)cout<<" ";for(int j=0;j<h+2*i;j++)cout<<"*";cout<<endl;}}}return 0;
}
问题 D: 沙漏图形 tri2str [1*+]
时间限制: 1 Sec 内存限制: 128 MB
题目描述
问题:输入n,输出正倒n层星号三角形。首行顶格,星号间有一空格,效果见样例
输入样例:
3
输出样例:
* * ** * ** *
* * *
数据规模 1<= n <=50
代码:
#include <iostream>using namespace std;int main()
{int h;while(cin>>h){for(int i=0;i<h;i++){for(int j=0;j<i;j++){cout<<" ";}for(int j=0;j<h-i;j++){cout<<"* ";}cout<<endl;}for(int i=2;i<=h;i++){for(int j=0;j<h-i;j++){cout<<" ";}for(int j=0;j<i;j++){cout<<"* ";}cout<<endl;}}return 0;
}