腾讯2017秋招编程笔试题

来源:互联网 发布:淘宝联盟手机版官网 编辑:程序博客网 时间:2024/06/05 11:41

假定一种编码的编码范围是a ~ y的25个字母,从1位到4位的编码,如果我们把该编码按字典序排序,形成一个数组如下: a, aa, aaa, aaaa, aaab, aaac, … …, b, ba, baa, baaa, baab, baac … …, yyyw, yyyx, yyyy 其中a的Index为0,aa的Index为1,aaa的Index为2,以此类推。 编写一个函数,输入是任意一个编码,输出这个编码对应的Index.

aaaa~aaab之间相差1,base[4]=1;

aaa~aab之间相差26,base[3]=base[4]*25+1;

aa~ab之间相差,base[2]=base[3]*25+1;

a~b之间相差,base[1]=base[2]*25+1;

例如baca:

第一步:找出b的位置,即与a之间的距离d1=(‘b’-‘a’)*base[1] + 1

第二步:找出ba的位置,即与ba之间的距离d2 = (‘a’-‘a’)*base[2] + 1

第三步:找出bac的位置,即与baa之间的距离d3 = (‘c’-‘a’)*base[3] + 1

第四步:找出baca的位置,即与baca之间的距离d4 = (‘a’-‘a’)*base[4] + 1

即baca的Index = d1+d2+d3+d4-1

#include <iostream>#include <cstdio>#include <cstring>#include <string> using namespace std;int main(){    int base[5];    base[4]=1;    base[3]=base[4]*25+1;    base[2]=base[3]*25+1;    base[1]=base[2]*25+1;    string str;    cin>>str;    int len=str.length();    int index=0;    for(int i=0;i<len;i++)        index+=(str[i]-'a')*base[i+1]+1;    cout<<index-1<<endl;}


游戏里面有很多各式各样的任务,其中有一种任务玩家只能做一次,这类任务一共有1024个,任务ID范围[1,1024]。请用32个unsigned int类型来记录着1024个任务是否已经完成。初始状态都是未完成。 输入两个参数,都是任务ID,需要设置第一个ID的任务为已经完成;并检查第二个ID的任务是否已经完成。 输出一个参数,如果第二个ID的任务已经完成输出1,如果未完成输出0。如果第一或第二个ID不在[1,1024]范围,则输出-1

水题

#include <iostream>#include <cstdio>#include <cstring>#include <string> using namespace std;int main(){    int id1,id2;    cin>>id1>>id2;    if(id1<1||id1>1024||id2<1||id2>1024)        cout<<"-1"<<endl;    else if(id1==id2)        cout<<"1"<<endl;    else        cout<<"0"<<endl;}
给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。输入值小于1000。
如,输入为10, 程序应该输出结果为2。(共有两对质数的和为10,分别为(5,5),(3,7))


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>using namespace std;bool isPrime[1010];void init(){    isPrime[0]=isPrime[1]=true;    for(int i=2;i<=sqrt(1000)+1;i++)    {        for(int j=i*i;j<=1000;j+=i)            isPrime[j]=true;    }}int main(){    int n,cnt=0;    scanf("%d",&n);    init();    for(int i=2;i<=n&&i<=(n-i);i++)    {        if(!isPrime[i]&&!isPrime[n-i])            cnt++;    }    printf("%d\n",cnt);}
geohash编码:geohash常用于将二维的经纬度转换为字符串,分为两步:第一步是经纬度的二进制编码,第二步是base32转码。
此题考察纬度的二进制编码:算法对纬度[-90, 90]通过二分法进行无限逼近(取决于所需精度,本题精度为6)。注意,本题进行二分法逼近过程中只采用向下取整来进行二分,针对二分中间值属于右区间。算法举例如下: 针对纬度为80进行二进制编码过程:
1) 区间[-90, 90]进行二分为[-90, 0),[0, 90],成为左右区间,可以确定80为右区间,标记为1;
2) 针对上一步的右区间[0, 90]进行二分为[0, 45),[45, 90],可以确定80是右区间,标记为1;
3) 针对[45, 90]进行二分为[45, 67),[67,90],可以确定80为右区间,标记为1;
4) 针对[67,90]进行二分为[67, 78),[78,90],可以确定80为右区间,标记为1;
5) 针对[78, 90]进行二分为[78, 84),[84, 90],可以确定80为左区间,标记为0;
6) 针对[78, 84)进行二分为[78, 81), [81, 84),可以确定80为左区间,标记为0;

#include <iostream>#include <cstdio>using namespace std;int main(){    int n;    int low=-90,high=90;    scanf("%d",&n);    for(int i=0;i<6&&low<=high;i++)    {        int mid=(low+high)/2;        if(mid<=n)        {            printf("1");            low=mid+1;        }        else        {            printf("0");            high=mid-1;        }    }    printf("\n");    return 0;}



原创粉丝点击