C++笔试题第一波

来源:互联网 发布:vr下载app软件 编辑:程序博客网 时间:2024/04/28 23:35
#include <iostream>using namespace std;//把指定的位置为0或者1。int Grial(int x, int n, int flags){    if (flags == 0)    {        x &= (~(0x1 << (n - 1)));    }    else    {        x |= (0x1 << (n - 1));    }    return x;}int main(){    cout << Grial(15,5,1);    return 0;}
#include <iostream>using namespace std;//成对出现的数组中找出唯一一个只出现一次的那个数。//按位异或相异为1,相同为0。int Grial(int a[],int n){    int i;    int count = 0;    for (i=0;i<n;i++)    {        count ^= a[i];    }    return count;}int main(){    int a[] = { 1, 1, 6, 3, 3, 8, 8, 6, 10 };    cout << Grial(a,9)<<endl;    return 0;}
#include <iostream>using namespace std;//求成对出现的数组中只出现一次的两个不重复的数字。int index(int x)//求第一次出现1的下标位置。{    int count = 0;    while (x)    {        count++;        if (x & 0x1 == 1)        {            return count;        }        x >>= 1;    }}bool test(int x,int y)//判断指定的y位是不是1。{    return ((x & (0x1<<(y-1)))) != 0;}void Grial(int a[],int n){    int i = 0;    int count = 0;    int number1=0;//存储第一个数。    int number2=0;//存储第二个数。    int *Adata = new int[n];    int *Bdata = new int[n];    int k1 = 0;    int k2 = 0;    for (; i < n; i++)    {        count ^= a[i];    }    i = index(count);    for (int j = 0; j < n; j++)//此处将数组分成两个部分。    {        if (test(a[j], i) == true)        {            Adata[k1++] = a[j];        }        else        {            Bdata[k2++] = a[j];        }    }    for (i = 0; i < k1; i++)    {        number1 ^= Adata[i];    }    for (i = 0; i < k2; i++)    {        number2 ^= Bdata[i];    }    cout << number1 << endl;    cout <<  number2 << endl;}int main(){    int a[] = {4,4,6,82,8,3,8,3,6,1070};    Grial(a, 10);    return 0;}
1 0
原创粉丝点击