SWEA 16800 구구단 걷기

1 개요[ | ]

SWEA 16800 구구단 걷기

2 C++[ | ]

#include <iostream>
#include <cmath>
using namespace std;

int main() {
	int TC;
	cin >> TC;
    long N, move, i;
	for(int testcase = 1; testcase <= TC; ++testcase) {
        cin >> N;
        move = N-1;
        for(i=2; i<=sqrt(N); i++) {
            if(N%i==0 && move > i+N/i-2) {
                move = i+N/i-2;
            }
        }
        cout << '#' << testcase << ' ' << move << '\n';
	}
}

3 Java[ | ]

import java.util.Scanner;
import java.io.FileInputStream;

class Solution {
    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        int TC = sc.nextInt();
        long N, move, i;
        for (int testcase = 1; testcase <= TC; testcase++) {
            N = sc.nextLong();
            move = N - 1;
            for(i=2; i <= Math.sqrt(N); i++) {
                if (N%i == 0 && move > i+N/i-2) {
                    move = i+N/i-2;
                }
            }
            System.out.format("#%d %d\n", testcase, move);
        }
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}