循环结构(1)

版权声明:https://blog.csdn.net/huashuimu2003 https://blog.csdn.net/huashuimu2003/article/details/84502389

图形输出

1

描述 Description
给定m和n两个整数的值。打印m行n列的图形。
样例输入 Sample Input

4 10
样例输出 Sample Output

描述 Description
给定m和n两个整数的值。打印m行n列的图形。
具体看样例。
输入格式 Input Format
两个用空格隔开的整数 m和n。
输出格式 Output Format
m行n列的"*" 组成的图形
样例输入 Sample Input

4 10
样例输出 Sample Output

   * * * * * * * * * *
  * * * * * * * * * *
 * * * * * * * * * *
* * * * * * * * * *

{每两个 * 号之间有空格,每行最后一个" * "后有空格}
代码

#include<iostream>
using namespace std;
int main()
{
	int m,n,i,j;
	cin>>m>>n;
	for(i=1;i<=m;i++)
	{
		for(j=1;j<=m-i;j++) cout<<' ';
		for(j=1;j<n;j++) cout<<"* ";
		cout<<'*'<<endl;
	}
	return 0;
}

2

描述 Description
给定n这个个整数的值。打印如下图形图形。

样例输入 Sample Input

4
样例输出 Sample Output

   *
  ***
 *****
*******
* 号之间没有空格。

时间限制 Time Limitation
1s

#include <iostream>
using namespace std; 
int main()
{  
    int i,j,k;    
    int N ;   
    cin>>N;        
    for(i=1;i<=N;i++)    
	{       
        for(j=1;j<=N-i;j++)          
        cout<<" ";        
        for(k=1;k<=2*i-1;k++)           
        cout<<"*";        
        cout<<endl;    
    }
    return 0;       
}

3

描述 Description
给定一个整数n。打印如下图形图形。
输入格式 Input Format
一个整数n
输出格式 Output Format
一个菱形。具体看样例。
样例输入 Sample Input

3
样例输出 Sample Output

  1
 222
33333
 222
  1 

时间限制 Time Limitation
1s

#include<bits/stdc++.h>
using namespace std;
int i,j,n,k;
int main()
{
	cin>>n;
	for(i=1;i<=n;i++)
	{
	    for(j=1;j<=n-i;j++)  cout<<' ';
    	for(k=1;k<=2*i-1;k++)    cout <<i;
	    cout <<endl;
	}
	for(i=1;i<=n-1;i++)
	{
		for(j=1;j<=i;j++) cout <<' ';
		for(k=1;k<=2*(n-i)-1;k++)  cout<<n-i;
		cout <<endl;
	}
	return 0; 
}

猜你喜欢

转载自blog.csdn.net/huashuimu2003/article/details/84502389
今日推荐