zoukankan      html  css  js  c++  java
  • MS 实习生热身赛

    题目1 : Arithmetic Expression

    描述

    Given N arithmetic expressions, can you tell whose result is closest to 9?

    输入

    Line 1: N (1 <= N <= 50000).
    Line 2..N+1: Each line contains an expression in the format of "a op b" where a, b are integers (-10000 <= a, b <= 10000) and op is one of addition (+), subtraction (-), multiplication (*) and division (/). There is no "divided by zero" expression.

    输出

    The index of expression whose result is closest to 9. If there are more than one such expressions, output the smallest index.

    样例输入

    4

    901 / 100

    3 * 3

    2 + 6

    8 - -1

    样例输出

    2

     

    解法:

    1. 直接上 double 比较, 只过了部分案例(不排除代码本身的 bug)

    2. 这道题考察的应该是分数的表示, 考试的时候我大致比划了一下, 但没有实现, 考试之后提交系统已经关闭, 因此也不知后面写出的代码是否有问题.

    唯有 / 会带来小数的问题. 我们用到分数的一个性质 a/b > c/d <-> a*d > b*c.

    因此我们将所有的数都表示成 abs(divided/divisor -9) 的形式.

    比如 901 / 100 -> 1/100; 3*3 -> 0/1; 2+6 -> 1/1; 8 - -1 = 0/1;

    这样就不会有小数的问题了.

    #include <iostream>

    #include <stdio.h>

    #include <memory.h>

    #include <algorithm>

    #include <vector>

    #include <map>

    #include <set>

    #include <string>

    #include <deque>

    #define MIN(x,y) (x)<(y)?(x):(y)

    using namespace std;

     

    int main() {

        freopen("C:\Users\vincent\Dropbox\workplacce\joj\test.txt", "r", stdin);

     

        int T, iCase = 0;

        scanf("%d", &T);

        

        char op1[1000], op2[1000];

        char ops[10];

        

        int divisor = 1, divided = 9999999;

        int index = 0;

     

        while(T --) {

            iCase ++;

            scanf("%s%s%s", op1,ops, op2);

            int lhs = atoi(op1);

            int rhs = atoi(op2);

            char op = ops[0];

            int fenzi, fenmu;

     

            if(op == '+') {

                fenzi = lhs + rhs;

                fenmu = 1;

            }else if(op == '-') {

                fenzi = lhs - rhs;

                fenmu = 1;

            }else if(op == '*') {

                fenzi = lhs * rhs;

                fenmu = 1;

            }else {

                fenzi = lhs;

                fenmu = rhs;

            }

     

            fenzi = fenzi - fenmu * 9;

     

            if(fenzi < 0) {

                fenzi = -1*fenzi;

            }

            if(fenmu < 0) {

                fenmu = -1 * fenmu;

            }

     

            if(divided*fenmu > divisor*fenzi) {

                index = iCase;

                divided = fenzi;

                divisor = fenmu;

            }

        }

        printf("%d ", index);

        return 0;

    }

     

    题目2 : Longest Repeated Sequence

    描述

    You are given a sequence of integers, A = a1, a2, ... an. A consecutive subsequence of A (say ai, ai+1 ... aj) is called a "repeated sequence" if it appears more than once in A (there exists some positive k that ai+k = ai, ai+k+1 = ai+1, ... aj+k = aj) and its appearances are not intersected (i + k > j).

    Can you find the longest repeated sequence in A?

    输入

    Line 1: n (1 <= n <= 300), the length of A.
    Line 2: the sequence, a1 a2 ... an (0 <= ai <= 100).

    输出

    The length of the longest repeated sequence.

    样例输入

    5

    2 3 2 3 2

    样例输出

    2

     

    思路:

    DP 题目. dp[i][j] 表示以 i 为结尾和以 j 为结尾相同子序列的长度

    dp[i][j] = dp[i-1][j-1] + 1 if(arr[i] == arr[j], 且两段子序列不相交)

     

    #include <iostream>

    #include <stdio.h>

    #include <memory.h>

    #include <algorithm>

    #include <vector>

    #include <map>

    #include <set>

    #include <string>

    #include <deque>

    #define MIN(x,y) (x)<(y)?(x):(y)

    using namespace std;

     

    int arr[500];

    int dp[500][500];

     

    int solve_dp(int n) {

        memset(dp, 0, sizeof(dp));

     

        int maxval = 0;

     

        for(int i = 1; i <= n; i ++) {

            for(int j = i+1; j <= n; j ++) {

     

                if(arr[i] == arr[j]) {

                    if(i + dp[i-1][j-1] < j) {

                        dp[i][j] = dp[i-1][j-1] + 1;

                    }else {

                        dp[i][j] = dp[i-1][j-1];

                    }

                }

                maxval = max(maxval, dp[i][j]);

     

            }

        }

        return maxval;

    }

     

    int main() {

        freopen("C:\Users\vincent\Dropbox\workplacce\joj\test.txt", "r", stdin);

     

        int n;

        scanf("%d", &n);

     

        for(int i = 1; i <= n; i ++) {

            scanf("%d", arr+i);

        }

        

        cout << solve_dp(n) << endl;

     

        return 0;

    }

  • 相关阅读:
    别再为了this发愁了------JS中的this机制
    offsetLeft,Left,clientLeft的区别
    下拉菜单的实现classList.add() classList.remove() class属性的添加和删除
    for循环
    html的基本数据类型(数字,字符串, 列表, 字典)
    定时器setInterval, innerText获取文本, charAt()获取单个字符串, substring(1, content.length)获取范围内的字符串, 实现字符串的滚动效果
    定时器 setInterval(‘function()’, 2000)
    parseInt 和 parseFloat 实现字符串转换为数字
    javarscript在HTML中的调用方式 (直接调用 和文件调用)
    input文本框 放上图片img 通过padding relative和absolute 的实现
  • 原文地址:https://www.cnblogs.com/zhouzhuo/p/3660055.html
Copyright © 2011-2022 走看看