java - 查找java应用程序的运行时和复杂性

标签 java

我是编码新手。虽然以下内容可能认为无关紧要,但请帮我解决这个问题。 我用两种不同的方式编写了代码,我想知道复杂性和运行时间,以及两者之间的 Big-O 表示法将给出更快的结果。我知道它可以使用 10 - 20 行编写,但我想知道代码长度变化的时间复杂度。

只是更正一下,每个 O(1) 算法的执行时间并不相等,因为它们都是 O(1)。那么我们如何确定执行时间,因为它们都是 O(1)?

代码1:

package can;

import java.util.Scanner;

public class scan2
{

private static Scanner scanner;
public static void main(String[] args)
 {
    System.out.println
       ("Select an option\n1-apples\n2-bananas\n3-oranges\nFor apples less     than 10 quantity,\neach apple costs 50INR else each apple price is 40INR.\nFor bananas less than 24,\neach banana costs 2INR else 1.5INR.\nFor oranges less than 12,\neach orange costs 5.7INR else 4INR. ");



    int i;
    int j =0;
    float k = 0f;
    scanner = new Scanner(System.in);
    i = (scanner.nextInt());

    if(i<=3&&i>0)
    {
        System.out.println("Please enter the quantity ");
        scanner = new Scanner(System.in);
        j = (scanner.nextInt()); 
    }
    else
    {
        System.out.println("Please choose a valid option ");
    }

if (i == 1&&i>0) 
{       
{
System.out.println("you have entered "+j+" quantites of apples");
}
if (j<10&&j>0)
{   
    k = j*50;
    System.out.println("Total cost is "+k);
}
else if(j>=10&&j>0)
{
    k = j*40;
    System.out.println("Total cost is "+k);
}   
if(j>0)
{   
    System.out.println("Your shopping is completed");
}
else
{
    System.out.println("Please enter valid quantity");
}

}   
if (i == 2)
{
{
System.out.println("you have entered "+j+" quantites of bananas");
}
if (j<24&&j>0)
{   
    k = j*2;
    System.out.println("Total cost is "+k);
}
else if(j>=24&&j>0)
{
    k = j*1.5f;
    System.out.println("Total cost is "+k);
}
if(j>0)
{   
    System.out.println("Your shopping is completed");
}
else
{
    System.out.println("Please enter valid quantity");
}
}   
if (i == 3)
{
{
System.out.println("you have entered "+j+" quantites of oranges");
}
if (j<12&&j>0)
{   
    k = j*5.7f;
    System.out.println("Total cost is "+k);
}
else if(j>=12&&j>0)
{
    k = j*4;
    System.out.println("Total cost is "+k);
}
if(j>0)
{   
    System.out.println("Your shopping is completed");
}
else
{
    System.out.println("Please enter valid quantity");              
}
}


}

}

代码2:

package can;

import java.util.Scanner;

public class shop 
{


private static Scanner scanner;
public static void main(String[] args) 
{
    System.out.println
           ("Select an option\n1-apples\n2-bananas\n3-oranges\nFor apples      less than 10 quantity,\neach apple costs 50INR else each apple price is 40INR.\nFor bananas less than 24,\neach banana costs 2INR else 1.5INR.\nFor oranges less than 12,\neach orange costs 5.7INR else 4INR. ");



int i;
int j =0;
float k = 0f;
scanner = new Scanner(System.in);
i = (scanner.nextInt());

if(i<=3&&i>0)
{
    System.out.println("Please enter the quantity ");
    scanner = new Scanner(System.in);
    j = (scanner.nextInt()); 
}
else
{
    System.out.println("Please choose a valid option ");
}

if (i == 1&&i>0) 
  {     
    {
    System.out.println("you have entered "+j+" quantites of apples");
    }
    if (j<10&&j>0)
    {   
        k = j*50;
        System.out.println("Total cost is "+k);
    }
    else if(j>=10&&j>0)
    {
        k = j*40;
        System.out.println("Total cost is "+k);
    }   
 }  
 if (i == 2)
   {
     {
       System.out.println("you have entered "+j+" quantites of bananas");
     }
    if (j<24&&j>0)
    {   
        k = j*2;
        System.out.println("Total cost is "+k);
    }
    else if(j>=24&&j>0)
    {
        k = j*1.5f;
        System.out.println("Total cost is "+k);
    }
 }  
 if (i == 3)
  {
    {
        System.out.println("you have entered "+j+" quantites of oranges");
    }
    if (j<12&&j>0)
    {   
        k = j*5.7f;
        System.out.println("Total cost is "+k);
    }
    else if(j>=12&&j>0)
    {
        k = j*4;
        System.out.println("Total cost is "+k);
    }

  }
  if(i<=3&&j>0)
 {  
    System.out.println("Your shopping is completed");
 }
  else if(j<0)
 {
    System.out.println("Please enter valid quantity");
 }
    else if(i<=3&&j == 0)
   {
       System.out.println("Please enter valid quantity");
   }

   }
  }

最佳答案

每个代码片段不包含循环,仅包含 if-else 语句。因此,每个代码片段的运行时间都是 O(1),这意味着两个代码片段的运行时间没有差异。就 Big-O 而言,只要您计算的操作总数是某个常量,它总是会减少到 1。类似的说法甚至可以用于可变数量的操作。假设大 O 是 3n 或 4n^2,在这些情况下,常数项将被删除,留下 n 或 n^2。关于大 O 应该注意的另一个注意事项是,除了具有最高次数的术语之外的所有术语也都被删除。例如,n^2 + 6n + 1 变为 O(n^2)。但是,您的代码片段包含 if 和 else-if 语句,它们会在执行期间创建一定范围的比较。由于此范围是恒定的,因此每个代码片段的 Big-O 都会减少到 O(1)

关于java - 查找java应用程序的运行时和复杂性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38190502/

相关文章:

java - 在 JDBC 中关闭后,Postgresql 连接保持空闲状态

java - 在目录中查找文件并返回其完整路径和文件名

java.sql.SQLException : Field 'supplier_id' doesn't have a default value

java - 轮盘赌轮旋转

java - Spring JDBC模板未绑定(bind)所有参数(获取参数值)

java - Android:抽屉导航 Activity - "The following classes could not be instantiated:"

java - 状态码 415 : No MessageBodyReader Found for multipart/form-data, FormDataMultiPart

java - JNI传递参数给c++的方法

java - 想要在 build.gradle 中指定 jar 名称和版本

java - 从导入的依赖项 Autowiring 对象