2017微软秋季校园招聘在线编程笔试1——1399 : Shortening Sequence

在线笔试的时候只AC了第一道,还做复杂了,第二题暴力仅在最小规模数据下成功,得到20分。做得非常不好...
这两天又重新看了一下这些题,本着练习一下C++的目的把这四道题都做了一遍。现在回头看感觉没有当时觉得的那么难!当时一定是太紧张和害怕了!希望自己以后做题能够放平心态,发挥出自己的水平就可以了。

1399 : Shortening Sequence

Description
There is an integer array A1, A2 ...AN. Each round you may choose two adjacent integers. If their sum is an odd number, the two adjacent integers can be deleted.
Can you work out the minimum length of the final array after elaborate deletions?
Input
The first line contains one integer N, indicating the length of the initial array.
The second line contains N integers, indicating A1, A2 ...AN.
For 30% of the data:1 ≤ N ≤ 10
For 60% of the data:1 ≤ N ≤ 1000
For 100% of the data:1 ≤ N ≤ 1000000, 0 ≤ Ai ≤ 1000000000
Output
One line with an integer indicating the minimum length of the final array.
Sample Hint
(1,2) (3,4) (4,5) are deleted.
Sample Input
7
1 1 2 3 4 4 5
Sample Output
1

思路分析
如果列表中同时存在奇数和偶数,那么一定存在可消除的数对。因此,在消除的最后,列表里要么只有奇数,要么只有偶数,而数对是成对消去的,因此最后列表中剩下的数字个数就是原列表中奇数项和偶数项的差值的绝对值。

AC代码

#encoding=utf-8
import sys
num = int(sys.stdin.readline())
s = [int(x) for x in sys.stdin.readline().strip().split()]
count = 0
for i in s:
    if i % 2 == 0:
        count += 1
    else:
        count -= 1
if count < 0:
    count = -count
print count
#include <iostream>

using namespace std;

int main() {
    int sum;
    cin >> sum;
    int count_odd = 0;
    int count_even = 0;
    int tmp;
    for (int i = 0; i < sum; i++) {
        cin >> tmp;
        if (tmp % 2 == 0)
            count_even++;
        else
            count_odd++;
    }
    int rst = count_odd - count_even > 0 ? count_odd - count_even : count_even - count_odd;
    cout << rst;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,791评论 0 33
  • 今天还是走到这一天, 要奔向各自的明天。 没人能代替分别的你, 还有那段青春岁月。 一路上我们曾携手并肩, 用汗和...
    雨_莲阅读 86评论 0 0
  • 说在前面的话绘本是最适合孩子阅读的书,好的绘本能给孩子艺术、文学、智慧等方面的综合滋养。越来越多的爸爸妈妈已经加入...
    发芽的梦阅读 5,578评论 4 4
  • 暑假你在家呆的很寂寞。 自己在沙发搭窝棚,钻进去,喊我来看很开心。 玩乐高,画画,每天一篇数学题,读一篇语文课文,...
    余渔与鱼阅读 216评论 0 0