【解決方法】このコードの複雑さはどのくらいですか?


what is the time complexity of this code?

私が試したこと:

import java.util.*;
public class Main
{
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    int n = sc.nextInt();
	    for(int i=0; i<n; i++){
	        for(int j=0; j<n; j++){
	    
		System.out.println(i + "->"+ j);
	        }
	    }
	}
}

解決策 1

解決策 3

実験的なアプローチをお勧めします。試してみてください

Java
public static void main(String[] args)  throws InterruptedException {
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      long start = System.currentTimeMillis();
      for(int i=0; i<n; i++){
          for(int j=0; j<n; j++){
              Thread.sleep(1000);
              System.out.println(i + "->"+ j);
          }
      }
      double elapsed_seconds = (System.currentTimeMillis() - start) / 1000;
      System.out.println("seconds " +  elapsed_seconds);
  }

期待される動作を見つけるために、さまざまな入力値を使用します。

解決策 2

私たちは立ち往生している人々を喜んで助けますが、それは私たちがあなたのためにすべてをするためにここにいるという意味ではありません! 私たちがすべての作業を行うことはできません。あなたはこれに対して報酬を受け取っているか、またはそれはあなたの成績の一部であり、私たちがあなたのためにすべてを行うことはまったく公平ではありません.

コメント

タイトルとURLをコピーしました