iT邦幫忙

0

C++求解

nama 2021-06-16 15:17:031402 瀏覽

會考完想趁升高中這段時間多少學一下
想請問各位要如何利用迴圈得到下列結果
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
phantom_0
iT邦新手 5 級 ‧ 2021-06-16 16:03:41

我想大概
1.第一層 迴圈有for (i=1. i<=10 , i++) i=10次,
2.再創一個內層迴圈,內容 for (x=1; x<=i, x++) 並打印 x
3.第i=10次時,創一個變數去接收i 例如h=i
然後一樣外層10-1次圈(因為總共19次也就是i-1次)
for(x=1,x<=i-1,x++)

4.在創一個內層迴圈 for(y=1, y<=h, y++) 並打印 y。

當然這我隨便想的,一定有更好的解答,裡面可能會有錯,不過其他解法應該也是同樣概念。 (其實可以用少一點迴圈 我這樣有點像作弊)

1
海綿寶寶
iT邦大神 1 級 ‧ 2021-06-16 16:32:19

這個去改

0
不明

這讓我想起當初大一看到這題目時 心裡OS這到底什麼鬼
教授還規定只能用一個雙迴圈來解
不過回來想想 思考如何求解 對之後來說很重要
我只能幫到這 Fight!

#include <iostream>

using namespace std;

int main()
{
    for (int i = 1; i < 20; i++) { //總共19列
      int rows = i;
      /* if (rows > 10) rows = 這裡讓你想想該放什麼才能讓後面倒過來 */;
      for (int j = 1; j <= rows; j++) {
        cout << j << " "  ;
      }
      cout << endl  ;
    }

    return 0;
}

1
一級屠豬士
iT邦大師 1 級 ‧ 2021-06-16 18:13:58

藉由這個問題,來介紹 flip flop operator , 使用 Python 來介紹.

pip3 install flip-flop-operator 安裝之後,就可以使用.

#!/usr/local/bin/python3

from flip_flop_operator import flip_flop

for i in range(20):
  if (i==0) | flip_flop | (i == 11):
    print ('-' * i)
  if (i==10) | flip_flop | (i == 20):
    print ('-' * (20 - i))

我就不列印一串數字了.
https://ithelp.ithome.com.tw/upload/images/20210616/20050647q0WCm5SGlK.png

這是變什麼魔術
/images/emoticon/emoticon04.gif

flip flop operator, 大大可以看 Perl.

Ruby 要看版本.
https://blog.saeloun.com/2020/01/06/ruby-2-7-revert-flip-flop-operator-deprecation.html

Python 要安裝套件.

我要發表回答

立即登入回答