开发者

How to show a number pattern in C++ [closed]

开发者 https://www.devze.com 2023-01-11 16:44 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_如何学运维 Closed 12 years ago.

How can I show the following pattern?

1 2 3 4 5
  1 2 3 4
    1 2 3
      1 2
        1


This should do the trick:

cout << "1 2 3 4 5" << "\n";
cout << "  1 2 3 4" << "\n";
cout << "    1 2 3" << "\n";
cout << "      1 2" << "\n";
cout << "        1" << endl;


printf("1 2 3 4 5\n  1 2 3 4\n    1 2 3\n      1 2\n        1\n");

That'll be four peanuts please.


#include <iostream>

void pattern(const int N) {
for (int i=N;i>0;i--) {
for (int j=i;j std::cout << " ";
for (int j=0;j std::cout << j+1 << " ";
std::cout << std::endl;
}
}

int main(void){
pattern(5);
return 0;
}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号