変態技術者になりたい何かのブログ

変態技術者になりたい何かのブログ

早朝出社・終電帰りの見習いSEが夢を叶えるために実装の問題を解いて解いて解きまくる、そんなブログ

Amebaでブログを始めよう!
引用:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_5_D
goto 文は、C/C++言語などで使うことのできる文で、実行されると無条件に指定されたラベルに処理が飛びます。例えば goto CHECK_NUM; という文が実行されると、プログラムの中で CHECK_NUM: と書かれた行に処理が移ります。この機能を使って、繰り返し処理なども実現することができます。
一方、goto 文は自由度が高すぎ、プログラムの可読性に影響するため、可能な限り使わないことが推奨されています。

次のC++言語のプログラムと同じ動作をするプログラムを作成してください。ただし、goto 文は使わないで実装してみましょう。

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;
}

Input
1つの整数 n が1行に与えられます。

Output
上記プログラムに入力の整数 n を与えた結果を出力してください。

Constraints
    3≤n≤10000

Sample Input
30

Sample Output
 3 6 9 12 13 15 18 21 23 24 27 30
※先頭に空白を出力することに注意してください。


ソース:
package ITP1;

import java.io.IOException;
import java.util.Scanner;

/*
 * goto 文は、C/C++言語などで使うことのできる文で、実行されると無条件に指定されたラベルに処理が飛びます。例えば goto CHECK_NUM; という文が実行されると、プログラムの中で CHECK_NUM: と書かれた行に処理が移ります。この機能を使って、繰り返し処理なども実現することができます。
 * 一方、goto 文は自由度が高すぎ、プログラムの可読性に影響するため、可能な限り使わないことが推奨されています。
 *
 * 次のC++言語のプログラムと同じ動作をするプログラムを作成してください。ただし、goto 文は使わないで実装してみましょう。
 *
 * 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;
 * }
 *
 * Input
 * 1つの整数 n が1行に与えられます。
 *
 * Output
 * 上記プログラムに入力の整数 n を与えた結果を出力してください。
 *
 * Constraints
 *     3≤n≤10000
 *
 * Sample Input
 * 30
 *
 * Sample Output
 *  3 6 9 12 13 15 18 21 23 24 27 30
 * ※先頭に空白を出力することに注意してください。
 */
public class SP1 {
    final private static int inputNumMin =     3;
    final private static int inputNumMax = 10000;

    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
        int inputInt  = scan.nextInt();
        if(!isFormatNum(inputInt)) {
            throw new IOException();
        }
        call(inputInt);
    }
    private static void call(int inputInt) throws IOException {
        final String space = " ";
        for(int i = inputNumMin; i <= inputInt; i++) {
            if (i % 3 == 0 || i % 10 == 3) {
                System.out.print(space + i);
            }
        }
        System.out.println();
    }
    private static boolean isFormatNum(int inputInt) {
        return (inputNumMin <= inputInt && inputInt <= inputNumMax);
    }
}

P.S.
とりあえず初見C++で何かいてあるのか解りませんでした。
もう忘れているのか、それとも単に汚いコードだからなのか。。。