This question already has answers here:
What is a plain English explanation of “Big O” notation?
(40个答案)
3年前关闭。
我是编码新手。尽管以下内容可能与您无关,请帮助我解决此问题。
我已经用两种不同的方式编写了代码,我想知道复杂度和运行时,并且使用Big-O表示法可以更快地得到结果,我知道可以使用10-20行编写,但是我想知道代码长度变化的时间复杂度。
只是一个校正,每个O(1)算法的执行时间并不相等,因为它们都是O(1)。那么我们如何确定它们都是O(1)的执行时间呢?
CODE1:
CODE2:
(40个答案)
3年前关闭。
我是编码新手。尽管以下内容可能与您无关,请帮助我解决此问题。
我已经用两种不同的方式编写了代码,我想知道复杂度和运行时,并且使用Big-O表示法可以更快地得到结果,我知道可以使用10-20行编写,但是我想知道代码长度变化的时间复杂度。
只是一个校正,每个O(1)算法的执行时间并不相等,因为它们都是O(1)。那么我们如何确定它们都是O(1)的执行时间呢?
CODE1:
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");
}
}
}
}
CODE2:
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。甚至可以说类似的声明涉及可变数量的操作。假设big-O为3n或4n ^ 2,在这种情况下,常数项将被舍弃,剩下n或n ^ 2。关于big-O的另一个注意事项是,除具有最高程度的术语外,所有其他术语也将被删除。例如,n ^ 2 + 6n + 1变为O(n ^ 2)。但是,您的代码段包含if和else-if语句,它们创建在执行期间可能发生的一定范围的比较。由于此范围是恒定的,因此每个代码段的Big-O都减小到O(1)
关于java - 查找Java应用程序的运行时和复杂性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38190502/
10-10 08:55