2016-07-04 80 views
0

我是编程新手。尽管以下可能认为不相关请帮我解决这个问题。 我用两种不同的方式编写了代码,我想知道复杂性和运行时间,哪一个会在两者之间用Big-O符号给出更快的结果。我知道它可以用10-20行编写,但我想知道代码长度变化的时间复杂度。查找java应用程序的运行时间和复杂度

只是一个校正,每个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"); 
    } 

    } 
    } 
+1

这两者中的任何一个都没有循环。他们是O(1)。我认为你还没有真正理解Big-O的含义。 Big-O允许告诉您在N个元素的集合上执行算法时,如果时间将保持不变,或者将以线性增长,指数增长或对数增长(例如)为N的值。并没有收集你的代码。大O不相关。 –

+0

基本if-else语句是O(1)。 1 for-loop使它成为O(N)。 2个嵌套for循环使其成为O(N^2)。还有一些更复杂的东西可以让你看到O(logN)或O(NlogN)。 – btrballin

+0

代码长度与执行时间没有直接关系,因为源不是执行的内容。同样,每个“O(1)”算法在执行时间上都不相同,因为它们都是“O(1)”。 –

回答

1

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