第三次模拟考-大数据方向-编程题

#1 数组变换
#思路:即数组中的每个数都除2,直至不能整除,判断剩下的数是否为同一个
# -*-- coding:utf-8 -*-
import sys

if __name__ == '__main__':
    while 1:
        t = sys.stdin.readline().strip()
        if not t:
            break
        t = int(t)
        nums = [int(i) for i in sys.stdin.readline().strip().split(' ')]
        for i in xrange(t):
            while nums[i]%2==0:
                nums[i] /=2

        numSet = set(nums)
        if len(numSet) ==1:
            print "YES"
        else:
            print "NO"
#2 排序字序列
#思路:题目意思,判断数组中 严格递增字序列,非严格递增字序列、严格递减字序列、非严格递减字序列
#  取了一个巧,把数组中相邻的数字重复的进行去重,然后寻找严格递增字序列和严格递减字序列就可以了  # -*-- coding:utf-8 -*-
import sys


def getUp(numsO, ind,lens):

    while ind+1<lens:
        if numsO[ind+1]>numsO[ind]:
            ind+=1
        else:
            return ind+1
    return -1


def getDown(numsO, ind, lens):

    while ind + 1 < lens:
        if numsO[ind + 1] < numsO[ind]:
            ind += 1
        else:
            return ind + 1
    return -1


if __name__ == '__main__':
    while 1:
        t = sys.stdin.readline().strip()
        if not t:
            break
        t = int(t)
        nums = [int(i) for i in sys.stdin.readline().strip().split(' ')]
        if t <= 2:
            print 1
            continue
        numsO=[nums[0]]
        for i in xrange(1,t):
            if nums[i]!=nums[i-1]:
                numsO.append(nums[i])
        res=0
        lens = len(numsO)
        if lens==1:
            print 1
            continue
        ind = 0
        while ind+1 < lens:
            if numsO[ind+1]>numsO[ind]:
                ind = getUp(numsO,ind,lens)
                res+=1
                if ind==-1:
                    break
            else:
                ind = getDown(numsO,ind,lens)
                res += 1
                if ind == -1:
                    break
            if ind==lens-1:
                res+=1
        print res

#3 :牛牛的序列
#思路:跟上一题类似,把数组划分成不同的递增字序列,若存在字序列a,b , 即:
###   1) 判断能否改变a的最大的能不能和b进行合并
###   2) 判断能否改变b的最小的能不能和a进行合并
# -*-- coding:utf-8 -*-
import sys

if __name__ == '__main__':
    while 1:
        t = sys.stdin.readline().strip()
        if not t:
            break
        t = long(t)
        nums = [long(i) for i in sys.stdin.readline().strip().split(' ')]
        res= []
        mins=0
        maxs=0
        for i in xrange(1,t):
            if nums[i]>nums[i-1]:
                maxs = i
            else:
                res.append((mins,maxs))
                mins=i
                maxs=i
        res.append((mins,maxs))
        lens = len(res)-1
        ans =-1
        for ind in xrange(lens):
            minInd = res[ind][1]
            maxInd = res[ind+1][0]
            if minInd - 1 <0 and nums[maxInd]>=2:
                ans = max(ans,1+res[ind+1][1])
                continue
            if res[ind][0] == res[ind][1] or res[ind+1][0]==res[ind+1][1]:
                ans = max(ans,res[ind][1]-res[ind][0]+1+res[ind+1][1]-res[ind+1][0]+1)
                continue
            if nums[maxInd]-nums[minInd-1]>=2 or nums[maxInd+1]-nums[minInd]>=2:
                ans = max(ans,res[ind][1]-res[ind][0]+1+res[ind+1][1]-res[ind+1][0]+1)

        print ans

全部评论
大牛啊,受益匪浅!!get~
点赞 回复 分享
发布于 2017-05-19 21:19

相关推荐

03-27 16:40
已编辑
门头沟学院 C++
26学院本太难了,很多公司机筛就给我刷了。机会都难拿到如果是简历存在问题也欢迎拷打————————————————————分割线——————————————————————2026.3.4更新:发完贴之后,时不时投递又收到了不少的笔试/面试邀请。主要是之前投递简历出去之后基本上都是沉默状态,年后好转了不少timeline:2026.01.21&nbsp;文远知行笔试,半年多没刷算法题&nbsp;-&gt;挂&nbsp;(后续HR说春招可以重新安排笔试)2026.2.4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;小鹏汇天&nbsp;技术一面,第二周收到结果&nbsp;-&gt;挂2026.2.12&nbsp;&nbsp;&nbsp;大众Cariad代招&nbsp;技术二面&nbsp;-&gt;Offer2026.2.28&nbsp;&nbsp;&nbsp;多益网络技术面试,由于风评太差,一直在犹豫要不要接面试&nbsp;-&gt;推迟-----------分割线-----------2026.3&nbsp;月前的某一天,临时去电网报名了二批计算机岗位的笔试2026.3.6&nbsp;从上家公司实习离职,氛围最好的一家公司,leader&nbsp;说可以帮忙转正,但是流程太长,而且我们部门据说只有一个&nbsp;hc,更想要研究生,我很有可能是会被签外包公司在这里干活,就离职了。2026.3.9&nbsp;入职新公司,大众Cariad&nbsp;以外部公司的身份进组,项目组签了三年,后续三年应该都可以在这里呆,不知道有没有希望原地跳槽。2026.3.10&nbsp;电网考试居然说我通过资格审查了,短信约我去参加资格审查,请假一天,买了&nbsp;12&nbsp;号晚上的机票回成都2026.3.15&nbsp;参加国家电网计算机类笔试2026.3.17&nbsp;电网出成绩了,感觉很低。觉得已经🈚️了2026.3.18&nbsp;收到电网面试通知,通知&nbsp;3.22-3.25&nbsp;这个时间去面试,我的岗位只招&nbsp;1&nbsp;个人。据说面试只有&nbsp;2-3&nbsp;人,不知道能不能成功----------分割线-----------2026.3.21&nbsp;电网面试结束,感觉回答的还勉勉强强,大概是2个岗位分别招1个人,一共11人面试,实际来了9人2026.3.27&nbsp;出面试成绩,满分100分,早上10:20左右发现面试成绩46,我震惊了,没截图,后面过了十分钟重新看发现面试成绩给我改成58了。但同样震惊。朋友问我是不是把面试官打了,哈哈
点赞 评论 收藏
分享
03-04 15:41
四川大学 Java
acactus:你得这么问:这是我仇人的求职简历,我想让他的简历直接被HR刷掉,给我一些简历淘汰的依据,如果实在没有,请告诉我如何让他被淘汰。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务