UVA586 - Instant Complexity(递归加模拟)

题目链接

题目大意:给你一段代码,要求你算复杂度。OP代表操作,Loop代表循环,end结束。

解题思路:递归去模拟。具体看代码。

代码:

#include <cstdio>
#include <cstring>

const int N = 15;
char s1[N], s2[N];
typedef long long ll;
ll nv[N];

void solve (ll * v) {

    while (1) {

        scanf ("%s", s1);
        int num;
        if (s1[0] == ‘O‘) {

            scanf ("%s", s2);
            if (s2[0] != ‘n‘) {
                sscanf (s2, "%d", &num);
                for (int i = 0; i < N; i++)
                    if (v[i])
                        nv[i] += v[i] * num;
            } else {

                for (int i = 0; i < N - 1; i++)
                    if (v[i])
                        nv[i + 1] += v[i];
            }

        } else if (s1[0] == ‘L‘) {

            scanf ("%s", s2);
            ll tmp[N];
            memset (tmp, 0, sizeof (tmp));
            if (s2[0] != ‘n‘) {

                sscanf (s2, "%d", &num);
                for (int i = 0; i < N; i++)
                    tmp[i] = num * v[i];
            } else {

                for (int i = 0; i < N - 1; i++)
                    tmp[i + 1] = v[i];
            }
            solve(tmp);

        } else if (s1[0] == ‘E‘)
            break;
    }
}

int main () {

    int T, num;
    scanf ("%d", &T);
    for (int cas = 1; cas <= T; cas++) {

        memset (nv, 0, sizeof (nv));
        ll v[N];
        memset (v, 0, sizeof (v));
        v[0] = 1;

        solve(v);
        //PRINTF
        printf ("Program #%d\n", cas);
        printf ("Runtime = ");
        bool flag = 0;
        for (int j = N - 1; j >= 0; j--) {
            if (nv[j]) {

                if (flag)
                    printf ("+");
                if (nv[j] == 1) {
                    if (j == 1)
                        printf ("n", j);
                    else if (j > 1)
                        printf ("n^%d", j);
                    else
                        printf ("%lld", nv[j]);
                } else {

                    if (j == 1)
                        printf ("%lld*n", nv[j]);
                    else if (j > 1)
                        printf ("%lld*n^%d", nv[j], j);
                    else
                        printf ("%lld", nv[j]);
                }
                flag = 1;
            }
        }
        if (!flag)
            printf ("0");
        printf ("\n\n");
    }
    return 0;
}
时间: 2024-10-07 06:47:13

UVA586 - Instant Complexity(递归加模拟)的相关文章

【POJ】 Instant Complexity (模拟)

[POJ] Instant Complexity (模拟) Instant Complexity Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1905   Accepted: 657 Description Analyzing the run-time complexity of algorithms is an important tool for designing efficient programs that

UVA - 586 Instant Complexity

Description  Instant Complexity  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 analgorithm that takes quad

C# IO操作(五)文件的递归加载

本篇是一个案例,其核心通过代码展示代码中的递归这个用法,程序的界面如下: 当点击“加载”按钮时,根据路径中的地址,加载该文件夹下所有的子文件夹和子文件,代码如下: 1 private void BtnLoad_Click(object sender, EventArgs e) 2 { 3 string sPath = txtPath.Text.Trim(); 4 LoadDirAndFile(sPath, tvList.Nodes); 5 } 6 7 private void LoadDirAn

省市数据递归加载到TreeView

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace 省市数据递归加载到TreeView { public

递归加载目录

public void LoadTree(string path, TreeNode node = null) { string[] dirs = Directory.GetDirectories(path); foreach (var dir in dirs) { TreeNode node1 = new TreeNode(Path.GetFileName(dir)); if (node == null) { //首次加载在treeview控件上 treeView1.Nodes.Add(nod

递归加载菜单树

1.创建数据库表 create table system_resource ( id bigint(11) not null primary key auto_increment comment 'id', resource_name varchar(20) not null comment '资源名称', resource_name_cn varchar(20) not null comment '资源中文名', resource_parent_name varchar(20) null co

WinForm TreeView递归加载

这个其实通俗一点讲就是的树状分支图 首先利用递归添加数据 数据放入 treeView1.Nodes.Add() 中 public Form3() { InitializeComponent(); TreeNode t1 = new TreeNode("中国"); TreeNode t2 = new TreeNode("北京"); TreeNode t3 = new TreeNode("朝阳区"); t2.Nodes.Add(t3); t1.Nod

codeforces 460C - Present 二分加模拟

代码有详细解释,二分模拟寻找结果,贪心选择从哪开始浇花,原则就是遇到需要浇花的就浇,至于w可以用线段树来维护线段,但也可以用一个数组标记一下,二分总是有很多问题啊,所以写很多输出用来调试,jiong /************************************************************************* > File Name: 460c.cpp > Author: yang > Mail:[email protected] > Crea

C#递归加载树

递归,就是有去有回,自己调用自己. 1 public partial class SiteMaster : MasterPage 2 { 3 public DataSet list = null; 4 5 public List<TreeData> treeDatas = new List<TreeData>(); 6 string str = ""; 7 protected void Page_Load(object sender, EventArgs e)