网易互娱 笔试题分享 3AC+一道40%(其余超时)

第一题
#include<set>
#include<stdio.h> 
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int trans(int n) //10转2 ,返回1的个数 
{
	int result=0,temp=n,i=1,count=0;
	while(temp)
	{
		if(temp%2==1)count++;
		result+=i*(temp%2);
		temp=temp/2;
		i=i*10;
	}
	return count;
}

void tongJi(int N)
{
	int a[102],results,temp;
	set<int> lable;
	for(int i=0;i<N;i++)
	{
		cin>>a[i];
		temp=trans(a[i]);
		lable.insert(temp);
	}
	results=lable.size();
	cout<<results<<endl;
}

int main()
{
	int T,N;
	cin>>T;//输入样例个数
	for(int i=0;i<T;i++)
	{
		cin>>N;//输入每个样例中的个数
		tongJi(N);
	}
	return 0;
}
第二题
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <math.h>
#include <string>
#include <queue>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int min(int a, int b, int c)
{
 int r;
 if(a >= b) r = b;
 else r = a;
 if(r >= c) r = c;
 return r; 
}
int main() {
    int T, m, t, m1, t1, m2, t2;
    cin >> T;
    while(T--)
    {
     int sum = 0;
     cin >> m >> t >> m1 >> t1 >> m2 >> t2;
     int x = t1, y = t2;
     int flag = 0;
     while(t!=0)
     {
      if(flag == 0)
      {
       int a = min(t1, t2, t);
       sum += a * (m1-m2);
       if(sum < 0) sum = 0;
       else if(sum > m) sum = m;
       t1 -= a;
       t2 -= a;
       t -= a;
       if(t1 == 0 && t2!=0) 
       {
        flag = 2;
        t1 += x; 
       }
       else if(t2 == 0 && t1 != 0)
       {
        flag = 1;
        t2 += y;
       }
       else if(t1==0&&t2==0)
       {
        flag = 3;
        t1 += x;
        t2 += y;
       }
      }
      else if(flag == 1)
      {
       int a = min(t1, t2, t);
       sum += a * (m1);
       if(sum < 0) sum = 0;
       else if(sum > m) sum = m;
       t1 -= a;
       t2 -= a;
       t -= a;
       if(t1 == 0 && t2!=0) 
       {
        flag = 3;
        t1 += x; 
       }
       else if(t2 == 0 && t1 != 0)
       {
        flag = 0;
        t2 += y;
       }
       else if(t1==0&&t2==0)
       {
        flag = 2;
        t1 += x;
        t2 += y;
       }
      }
      else if(flag == 2)
      {
       int a = min(t1, t2, t);
       sum += a * (-m2);
       if(sum < 0) sum = 0;
       else if(sum > m) sum = m;
       t1 -= a;
       t2 -= a;
       t -= a;
       if(t1 == 0 && t2!=0) 
       {
        flag = 0;
        t1 += x; 
       }
       else if(t2 == 0 && t1 != 0)
       {
        flag = 3;
        t2 += y;
       }
       else if(t1==0&&t2==0)
       {
        flag = 1;
        t1 += x;
        t2 += y;
       }
      }
      else if(flag == 3)
      {
       int a = min(t1, t2, t);
       t1 -= a;
       t2 -= a;
       t -= a;
       if(t1 == 0 && t2!=0) 
       {
        flag = 1;
        t1 += x; 
       }
       else if(t2 == 0 && t1 != 0)
       {
        flag = 2;
        t2 += y;
       }
       else if(t1==0&&t2==0)
       {
        flag = 0;
        t1 += x;
        t2 += y;
       }
      }
     }
     cout << sum << endl;
    } 
    return 0;
}


第三题
#include<bits/stdc++.h>

using namespace std;

int count(string str) 
{
    int left = 0, right = 0, change = 0, res = 1;
    for (int i = 0; i < str.length(); i++)
    {
        if (str[i] != 'N')
        {
            if (change < 2)
            {
                change++;
                right++;
            }
            else
            {
                while (left <= right && str[left] == 'N') 
     left++;
                left++;
                right++;
            }
        }
        else right++;
        res = max(res, right - left );
    }
    return res;
}

int main()
{
 int T;
 cin >> T;
 while(T--)
 {
  string str;
  cin >> str;
  for(int i = 0; i < str.length(); ++i)
  {
   if(str[i] < 'A' || str[i] > 'Z')
    return 0;
  }
  cout << count(str) << endl;
 }return 0; 
}

第四题  只通过了40%,其余超时,求大神分享代码呀~~~
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int  a[200001];

void ansQun(int h,int n)//h是这次洪水的高度,n是发电站的个数 
{
	int temp,flag=0,count=0;
	for(int i=0;i<n;i++)
	{
		if(h>=a[i])
		{
			flag=0;
		}
		else //没被淹没的 
		{
			if(flag==0)//还没有记录,要计算之 
			{
				flag=1;count++;
			}
		}
	} 
	cout<<count<<endl; 
}

int main() {
    int n, q, h;
    cin >> n;//输入发电基站的个数 
    for(int i=0;i<n;i++) cin >> a[i];//每个电站的海拔 
    cin >> q;//洪水个数 
    for(int i=0;i<q;i++)
	{
		cin>>h;
		ansQun(h,n);
	} 
    return 0;
}



#网易互娱##笔试题目#
全部评论
妹子这么强吗 我第四题看都没看就交了
点赞 回复 分享
发布于 2019-08-11 18:22
这才是真正的大佬~
点赞 回复 分享
发布于 2019-08-11 17:30
楼主你第一题没超时吗
点赞 回复 分享
发布于 2019-08-11 17:37
第一题可以直接temp&(temp-1)
点赞 回复 分享
发布于 2019-08-11 17:39
楼主,如果第一题二进制函数的while 条件改变为temp/2对结果会有影响吗?
点赞 回复 分享
发布于 2019-08-11 17:39
第四题有人AC的吗?
点赞 回复 分享
发布于 2019-08-11 17:40
第四题写了两小时,看不到出错的用例也太难找瓶颈了…
点赞 回复 分享
发布于 2019-08-11 17:44
第一题有没有自己测试的用例都能行,一提交就是%0的同学啊,求问这是哪里出了问题
点赞 回复 分享
发布于 2019-08-11 17:55
第四题的改进考虑了好多角度: 1. 对低于最小高度和高于最大高度的情况直接输出结果 2. 保存每个发电站被淹和不被淹两种情况对应可以跳至的下一个检查点,比如对高度为[1,2,3]的发电站,如果1都不会被淹,那2、3都可以跳过检查,用两个数组分别记录被淹和不被淹的情况 3. 记录每次洪水高度到hash表中,如果有重复洪水高度直接查表 4. 对发电站的高度进行排序,然后对每次洪水的高度,通过二分查找找到刚好可以淹没的发电站高度,计算洪水为改高度的结果并保存至hash表,如果hash表中已有结果可以直接返回 (最后才想到,没写完) 前三种方案都试了还是40%,第四种没时间写了,不知道能不能解决,有没有哪位大佬全ac的说一下第四题到底是怎么个思路……
点赞 回复 分享
发布于 2019-08-11 18:06
第二题可以不用这么长,有些条件重复了应该。
点赞 回复 分享
发布于 2019-08-11 18:31
请问第一道题中,第二行的输入不是各个用例分别有多少个数组成吗,这样的话不是应该先把所有用例的个数存储完毕后,才能够开始逐一对每个用例进行转换二进制的方法调用吗?我看楼主你写的好像是先根据第一个用例中的数字个数进行一次循环,完毕后再处理第二个用例。
点赞 回复 分享
发布于 2019-08-11 21:26
……为啥我只有三道题……
点赞 回复 分享
发布于 2019-08-14 16:41
收到后续的面试通知了嘛
点赞 回复 分享
发布于 2019-08-14 17:21
楼楼你收到面试通知了吗
点赞 回复 分享
发布于 2019-08-17 09:47

相关推荐

评论
4
13
分享

创作者周榜

更多
牛客网
牛客企业服务