Astrisks按降序排列 [英] Astrisks in descending order

查看:142
本文介绍了Astrisks按降序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输出结果不像我预期的那样?!

建议请。!!!

  #include     stdafx.h 
#include < iostream >

使用 命名空间标准;

// [问题]
// 按降序打印Astrisks
// 设n为从列的第n个位置开始打印*的行数。例如,n = 5表示我们要打印5行星号和星号。第一颗星将打印在列的第5个位置。

int main()
{
int n;
cout<< 输入值:;
cin>> N;
int i = 0 ,j = n; // 让我=行& j = colum
如果(j> = 0
{
while (i< n)
{
while (j< = n)
{
cout<< *;

j--;
}
cout<< ' \ n';
i ++;
}

}
返回 0 ;
}

解决方案

你运行它并调试它吗?只需在笔和笔上做到这一点纸上看到的问题。



以下是询问者的预期:

1. 首先尝试你想做什么!你可能会发现它并不那么难。

2.制定你所做的事情看似问题/不工作。



尝试一下,告诉你是否面临问题。

各位会员非常乐意为此提供帮助。




首先:

''j'的价值&打印开始时''n''相同...导致第一行出现单星(不需要降序设计。)


'if''语句应该检查n的值(即不是j),然后你需要移动定义j并在外部while循环中将其设置为n。


假设你需要生成这个模式(一个圆点代表一个空格)

 .... * 
... *。
.. * ..
。* ...
* ....



以下代码可以:

  int  n =  5 ; 
for int row = 0 ; row< n; ++ row)
{
for int col = 0 ; col< n; ++ col)
{
cout<< ((n-col == row + 1)?' *'' 。');
}
cout<< ENDL;
}







 * .... 
。* ...
.. * ..
... *。
.... *



使用

  int  n =  5 ; 
for int row = 0 ; row< n; ++ row)
{
for int col = 0 ; col< n; ++ col)
{
cout<< ((col == row)?' *' ' 。');
}
cout<< ENDL;
}







 .... * 
.... *
.... *
.... *
.... *



使用

  int  n =  5 ; 
for int row = 0 ; row< n; ++ row)
{
for int col = 0 ; col< n; ++ col)
{
cout<< ((col == n- 1 )?' * '' 。');
}
cout<< ENDL;
}







干杯

Andi


The output is not like that as I expected ?!
Suggestion Please .!!!

#include "stdafx.h"
#include <iostream>

using namespace std;

//	[P R O B L E M]
//	Print Astrisks in descending order
//      Let n as the number of lines to print "*" starting from nth position of          column. For instance n = 5 that means we've to print 5 rows of asterisks & first star will be printed on 5th position of column.

int main ()
{
	 int n;
	 cout << "Enter Value: ";
	 cin >> n;
	 int i = 0, j = n;	//	Let i = row & j = colum
	 if (j >= 0)
	 {
		while (i < n)
	 {
		 while (j <= n)
		 {
			 cout << "*";
			 
			 j--;
		 }		 
		 cout << '\n';
		 i++;
	 }
	 
	 }
	 return 0;
}

解决方案

Did you run it and DEBUGGED it? Just do it on pen & paper to see the issues.

Here is what is expected of enquirers:
1. TRY first what you want to do! You may find that it''s not that hard.
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.


To start with:
value of ''j'' & ''n'' are same when the print starts... leading to single star on first line (not needed as descending design was needed.)


The ''if'' statement should check for the value of n (ie. not j) and then you need to move the definition j and set it to n inside the outer while loop.


Assuming you need produce this pattern (a dot stands for a space)

....*
...*.
..*..
.*...
*....


the following code would do:

int n = 5;
for (int row = 0; row < n; ++row)
{
   for (int col = 0; col < n; ++col)
   {
      cout << ((n-col == row+1) ? '*' : '.');
   }
   cout << endl;
}



For

*....
.*...
..*..
...*.
....*


use

int n = 5;
for (int row = 0; row < n; ++row)
{
   for (int col = 0; col < n; ++col)
   {
      cout << ((col == row) ? '*' : '.');
   }
   cout << endl;
}



For

....*
....*
....*
....*
....*


use

int n = 5;
for (int row = 0; row < n; ++row)
{
   for (int col = 0; col < n; ++col)
   {
      cout << ((col == n-1) ? '*' : '.');
   }
   cout << endl;
}


etc.

Cheers
Andi


这篇关于Astrisks按降序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆