小米安卓三道编程题AC代码分享

//1、排座位,暴力+贪心,遇到0就判断它的左右是否为0,然后放进去。输出是0/1,题目错了,坑  -------AC 100%
#include<bits/stdc++.h>

using namespace std;
#define maxn 10005

char s[maxn];

int main()
{
    while(~scanf("%s",s+1))
    {
        int len=strlen(s+1);
        s[0]='0';
        s[len+1]='0';
        int k;
        scanf("%d",&k);
        int ans=0;
        for(int i=1; i<=len; i++)
        {
            if(s[i]=='1')
            {

            }
            else
            {
                if(s[i-1]=='0'&&s[i+1]=='0')
                {
                    ans++;
                    s[i]='1';
                }
            }
        }
        if(ans>=k)
        {
            printf("1\n");
        }
        else
        {
            printf("0\n");
        }
    }
    return 0;
}

2、杨辉三角。不会做,以为是找规律,但是放到oeis也没有找出来。于是水了一发java大数,居然过了,应该是数据水吧,这不是正解。
我预处理把200*200的三角打出来,超过10^18就停,然后去判断。不是正解不是正解不是正解,虽然过了。-----AC 100%

import java.math.BigInteger;
import java.util.Scanner;

public class Main
{

    static BigInteger[][] a = new BigInteger[250][250];

    public static void init()
    {
        int n=200;

        BigInteger bi = new BigInteger("10000000000000000000");
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                a[i][j] = new BigInteger("0");
            }
            //System.out.println();
        }

        for(int i=1; i<=n; i++)
            a[i][1] = a[i][i] = new BigInteger("1");

        int limit = 10000000;
        for(int i=3; i<=n; i++)
            for(int j=2; j<=min(i-1,limit); j++)
            {
                a[i][j]=a[i-1][j-1].add(a[i-1][j]);
                if(a[i][j].compareTo(bi)>0)
                {
                    limit = j;
                }
            }



    }
    private static int min(int i, int limit)
    {
        // TODO Auto-generated method stub
        if(i<limit) return i;

        return limit;
    }
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        init();
        long x = cin.nextLong();
        BigInteger y = BigInteger.valueOf(x);
        BigInteger bi = new BigInteger("10000000000000000000");
        int n=200;
        int f=0;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=i; j++)
            {
                if(a[i][j].compareTo(bi)>0)
                {
                    break;
                }
                if(a[i][j].equals(y))
                {
                    f=1;
                    System.out.println(i);
                    break;
                }
            }
            if(f==1)
            {
                break;
            }
        }
        if(f==0)
        {
            System.out.println(x+1);
        }
    }
}

3、喝饮料那题,小数据暴力,大数据我直接算。我的界定线的k>=10和k<=0,其实界定线为7就ok!
k<10的时候,就是暴力放进去,看最后一天是哪天。
k>=10的时候,那就是有规律了,比如AA 10 ,那么A在第1天和第11天,即i+(num[A]-1)*k。有多种饮料的时候同理。------AC 100%
#include<bits/stdc++.h>

using namespace std;
#define maxn 10000005
//bool f[maxn];
char s[10005];
int have[10];
bitset<maxn > f;
int cmp(int a,int b)
{
    return a>b;
}
int main()
{
    while(~scanf("%s",s))
    {
        f.reset();
        memset(have,0,sizeof(have));
        int k;
        scanf("%d",&k);
        int len=strlen(s);
        for(int i=0; i<len; i++)
        {
            int a=s[i]-'A';
            have[a]++;
        }
        sort(have,have+7,cmp);
        int ans=0;
        if(k<10)
        {
            for(int i=0; i<7; i++)
            {
                int num=have[i];
                for(int j=1;; j++)
                {
                    if(num==0)
                    {
                        break;
                    }
                    if(f[j]==false)
                    {
                        num--;
                        f[j]=true;
                        ans=max(ans,j);
                        j=j+k;
                    }
                }
            }
        }
        else
        {
            for(int i=0; i<7; i++)
            {
                int num=have[i];
                ans=max(ans,i+1+(num-1)*k);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

#小米#
全部评论
第一题真坑。。输出半天都不对,要哭了
点赞 回复 分享
发布于 2017-09-18 21:10
** 第一题坑爹啊
点赞 回复 分享
发布于 2017-09-18 21:04
import java.util.Scanner; ////思路:找到1和1之间的0的个数n,判断(n-2)/2是奇数还是偶数[前提是n-2>0] //如果是奇数则(n-2)/2+1,偶数则(n-2)/2 public class xiaomi1 {           static boolean fun(String table, int n) {         int i=0;         boolean flag = true ; //true:1 false:0         int count = 0 ;         if(table.charAt(i)=='1'){             flag = true ;         }else if(table.charAt(i)=='0'){             count++;             flag=false;         }         i++;         int mm=0;         while(i<table.length())         {             if(flag){                 if(table.charAt(i)=='0'){                     if(count-2>0 && (count-2)%2 == 0){                         mm=mm+(count-2)/2 ;                     }else if(count-2>0 && (count-2)%2 != 0){                         mm=mm+(count-2)/2 +1;                     }                     count=1;                     flag=false;                 }else{                     count=0;                     flag=true;                 }             }else{                 if(table.charAt(i)=='0'){                     count++;                     flag=false;                 }else{                     if(count-2>0 && (count-2)%2 == 0){                         mm=mm+(count-2)/2 ;                     }else if(count-2>0 && (count-2)%2 != 0){                         mm=mm+(count-2)/2 +1;                     }                     count=0;                     flag=true;                 }             }             i++;                      }         //System.out.println(mm);         //System.out.println(n);         return mm>=n?true:false ;     }          public static void main(String[] args) {         Scanner in = new Scanner(System.in);         boolean res;                      String _table;         try {             _table = in.nextLine();         } catch (Exception e) {             _table = null;         }                  int _n;         _n = Integer.parseInt(in.nextLine().trim());            //res = fun(_table, _n);         System.out.println(fun(_table, _n));                } }
点赞 回复 分享
发布于 2017-09-18 21:45
大佬 腻害啊 小米服务端岗位的题 做得崩溃了
点赞 回复 分享
发布于 2017-09-18 21:04
我第二题 11%  不知道为啥。。。
点赞 回复 分享
发布于 2017-09-18 21:04

相关推荐

评论
点赞
收藏
分享

创作者周榜

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