如何打印此输出? [英] How do I print this output?
本文介绍了如何打印此输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
编写一个C ++程序来打印以下输出 -
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
我尝试过:
#include< iostream>使用namespace std
;
int main()
{
int i,j;
for(i = 1; i< = 5; i ++){
for(j = 5; j< = i; j - ){
cout<< j;
}
cout<<'\ n';
}
返回0;
}
我不知道这段代码有什么问题,但它没有给我所需的输出。
解决方案
尝试
( j = 5 ; j > = i; j - ){
>
您应该测试j
更大大于或等于i
。
for (j = 5 ; j> = i; j - ){
看一下带下划线的文字:
for(j = 5; j< = i ; j - )
j
永远不会小于或等于i
,因为在开头j = 5
和I = 1
Write a C++ program to print the following output-
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
What I have tried:
#include <iostream> using namespace std; int main() { int i,j; for(i=1; i<=5; i++){ for(j=5; j<=i; j--){ cout<<j; } cout<<'\n'; } return 0; }
I don't know what is wrong with this code but it doesn't gives me the desired output.
解决方案
Try
for(j=5; j>=i; j--){
You should be testingj
to be greater than or equal toi
.
for(j=5; j >= i; j--){
Take a look at underlined text:
for(j=5; j<=i; j--)
j
will never be less or equali
, because on the beginningj=5
andi=1
.
这篇关于如何打印此输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文