zoukankan      html  css  js  c++  java
  • Instant Complexity(模拟,递归)

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 1535   Accepted: 529

    Description

    Analyzing the run-time complexity of algorithms is an important tool for designing efficient programs that solve a problem. An algorithm that runs in linear time is usually much faster than an algorithm that takes quadratic time for the same task, and thus should be preferred. 

    Generally, one determines the run-time of an algorithm in relation to the `size' n of the input, which could be the number of objects to be sorted, the number of points in a given polygon, and so on. Since determining a formula dependent on n for the run-time of an algorithm is no easy task, it would be great if this could be automated. Unfortunately, this is not possible in general, but in this problem we will consider programs of a very simple nature, for which it is possible. Our programs are built according to the following rules (given in BNF), where < number > can be any non-negative integer: 

    < Program > ::= "BEGIN" < Statementlist > "END"
    < Statementlist > ::= < Statement > | < Statement > < Statementlist >
    < Statement > ::= < LOOP-Statement > | < OP-Statement >
    < LOOP-Statement > ::= < LOOP-Header > < Statementlist > "END"
    < LOOP-Header > ::= "LOOP" < number > | "LOOP n"
    < OP-Statement > ::= "OP" < number >

    The run-time of such a program can be computed as follows: the execution of an OP-statement costs as many time-units as its parameter specifies. The statement list enclosed by a LOOP-statement is executed as many times as the parameter of the statement indicates, i.e., the given constant number of times, if a number is given, and n times, if n is given. The run-time of a statement list is the sum of the times of its constituent parts. The total run-time therefore generally depends on n. 

    Input

    The input starts with a line containing the number k of programs in the input. Following this are k programs which are constructed according to the grammar given above. Whitespace and newlines can appear anywhere in a program, but not within the keywords BEGIN, END, LOOP and OP or in an integer value. The nesting depth of the LOOP-operators will be at most 10.

    Output

    For each program in the input, first output the number of the program, as shown in the sample output. Then output the run-time of the program in terms of n; this will be a polynomial of degree Y <= 10. Print the polynomial in the usual way, i.e., collect all terms, and print it in the form "Runtime = a*n^10+b*n^9+ . . . +i*n^2+ j*n+k", where terms with zero coefficients are left out, and factors of 1 are not written. If the runtime is zero, just print "Runtime = 0". 
    Output a blank line after each test case.

    Sample Input

    2
    BEGIN
      LOOP n
        OP 4
        LOOP 3
          LOOP n
            OP 1
          END
          OP 2
        END
        OP 1
      END
      OP 17
    END
    
    BEGIN
      OP 1997 LOOP n LOOP n OP 1 END END
    END

    Sample Output

    Program #1
    Runtime = 3*n^2+11*n+17
    
    Program #2
    Runtime = n^2+1997

    简直是想半天也无从下手的一道题,不过个人感觉通过这道题对递归了解的更深刻了点,不过自己写递归,还是不会写。。。sad.加油吧

    大致题意:

    给出一段Pascial程序,计算其时间复杂度(能计算的项则计算,不能计算则化到最简的关于n的表达式O(n),并把各项根据n的指数从高到低排列),输出时,系数为0的项不输出,系数为1的项不输出系数,指数为1的项不输出指数。

    一段程序只有唯一一个BEGIN,代表程序的开始。与其对应的为最后的END,代表程序的结束。

    一段程序最多只有10层循环嵌套,循环的入口为LOOP,一个LOOP对应一个END,代表该层循环的结束。

    一段程序中OP的个数不限。

    LOOP是循环的入口,其后面的数据可能是常量(非负整数),也可能是变量n,代表循环体执行的次数。

    OP是语句,其后面的数据只能为常量(非负整数),代表该语句执行的次数。

     还要注意输出,指数为1的不输出指数,系数为1的不输出系数,系数为0的不输出,指数为0的只输出常数

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 void Loop(int *ans, char *LpNum)//LpNum代表该循环的次数
     5 {
     6     char s[20],s1[20] = {0};
     7     while(scanf("%s",s) && s[0] != 'E')
     8     {
     9         if(s[0] == 'L')//相当于一个大循环嵌套的小循环,是一个子结构,所以,递归调用
    10         {
    11             int *tmp = new int [11];
    12             memset(tmp,0,11*sizeof(int));//暂存数组tmp
    13             scanf("%s",s1);
    14             Loop(tmp,s1);
    15 
    16             for(int i = 0; i <= 10; i++)
    17                 ans[i] += tmp[i];
    18         }
    19         else if(s[0] == 'O')
    20         {
    21             int x;
    22             scanf("%d",&x);
    23             ans[0] += x;
    24         }
    25     }
    26 
    27     if(LpNum[0] == 'n')
    28     {
    29         for(int i = 10; i > 0; i--)
    30             ans[i] = ans[i-1];
    31         ans[0] = 0;
    32     }
    33     else
    34     {
    35         int x = atoi(LpNum);
    36         for(int i = 0; i <= 10; i++)
    37             ans[i] *= x;
    38     }
    39 }
    40 
    41 int main()
    42 {
    43     int test;
    44     int ans[11];
    45     scanf("%d",&test);
    46     for(int item = 1; item <= test; item++)
    47     {
    48         char s[10];
    49         scanf("%s",s);
    50         memset(ans,0,sizeof(ans));
    51 
    52         Loop(ans,"1");//把最外面的大循环次数当做1
    53 
    54         printf("Program #%d
    ",item);
    55         printf("Runtime = ");
    56 
    57         bool tag = 1;
    58         for(int i = 10; i >= 0; i--)
    59         {
    60             if(ans[i] == 0)
    61                 continue;
    62             if(i == 0)
    63                 printf("%s%d",tag?"":"+",ans[0]);
    64             else if(ans[i] == 1)
    65                 printf("%sn",tag?"":"+");
    66             else if(ans[i] > 1)
    67                 printf("%s%d*n",tag?"":"+",ans[i]);
    68             if(i > 1)
    69                 printf("^%d",i);
    70             tag = 0;
    71         }
    72         if(tag)
    73             printf("0");
    74         printf("
    
    ");
    75 
    76     }
    77     return 0;
    78 }
    View Code


  • 相关阅读:
    DevOps Workshop 研发运维一体化(北京第二场) 2016.04.27
    DevOps Workshop 研发运维一体化(成都站) 2016.05.08
    TFS 2015 生成不输出任何结果
    挂起的更改中的“解析”是什么意思?原来是微软错误的翻译
    微软研发流程(ALM)管理培训会议(比亚迪汽车)
    TFS实战培训
    【实战TFS】【QQ群】了解别人是如何使用TFS的
    DevOps Workshop 研发运维一体化(广州站)
    制作Visual Studio 2017 (VS 2017) 离线安装包
    在每页(分页)报表中重复显示标题
  • 原文地址:https://www.cnblogs.com/LK1994/p/3395265.html
Copyright © 2011-2022 走看看