Asa's Website

D - Structured Programming

cpaojitp1

最終更新日

Table of Contents

Loading...

問題

https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/5/ITP1_5_D

問題文

goto を使わずに、以下のプログラムと同じ動作をさせてね。
call(n)call(n) の結果を出力してね。

void call(int n){ int i = 1; CHECK_NUM: int x = i; if ( x % 3 == 0 ){ cout << " " << i; goto END_CHECK_NUM; } INCLUDE3: if ( x % 10 == 3 ){ cout << " " << i; goto END_CHECK_NUM; } x /= 10; if ( x ) goto INCLUDE3; END_CHECK_NUM: if ( ++i <= n ) goto CHECK_NUM; cout << endl; }

制約

サンプル

I/O 1

30
3 6 9 12 13 15 18 21 23 24 27 30

先頭に空白があることに注意

考察

ラベル部分を関数に置き換える。プロトタイプ宣言も書いておく。

コード

https://onlinejudge.u-aizu.ac.jp/status/users/a01sa01to/submissions/1/ITP1_5_D/judge/6370601/C++17

int n; int i = 1; int x = i; void checknum(); void include3(); void endchecknum(); void checknum() { x = i; if (x % 3 == 0) { cout << " " << i; endchecknum(); return; } include3(); return; } void include3() { if (x % 10 == 3) { cout << " " << i; endchecknum(); return; } x /= 10; if (x) include3(); else endchecknum(); } void endchecknum() { if (++i <= n) checknum(); } int main() { cin >> n; checknum(); cout << endl; }