回 帖 发 新 帖 刷新版面

主题:第66次编程比赛

[size=3]问题题目:最大的异或

[color=0000FF]题目描述:[/color]
给你n个正整数,你要找出哪两个数按位异或运算后的结果是最大的

[color=0000FF]输入:[/color]
输入一个整数n(2<=n<=100000),然后就是n个1e9以内的正整数

[color=0000FF]输出:[/color]
输出最大的按位异或运算结果

[color=0000FF]样例输入:[/color]
4
1 3 4 7

[color=0000FF]样例输出:[/color]
7

[color=0000FF]提示:[/color]
3和4的异或的结果是7,已经最大

参加比赛者直接回复本帖子即可,回复帖子不可见
结束时间:6月2日[/size]

回复列表 (共27个回复)

沙发

#include <stdio.h>


void main()
{
    int n,*m;
    int i,M,max;
    m=&M;
    while(1)
    {
        fflush(stdin);
        printf("input n ( 2 <= n <= 100000 ):");
        scanf("%d",&n);
        if(n>=2&&n<=100000)
            break;
        printf("wrong!input again pleas.\n");
    }
    scanf("%d",&max);
    for(i=1;i<n;i++)
    {
        scanf("%d",m);
        max=(max^n)>((*m)^n)?max:(*m);
    }
    printf("max is %d\n",max^n);
    
}

板凳

#include <iostream>
#include <vector>
using namespace std; 

long max(vector<long> &number,int n);
int main()
{
    int n = 0;
    long j = 0;
    vector<long> number;

    cout<<"数字个数:"<<endl;
    cin>>n;
    cout<<"Please input number:"<<endl;
    for(int i = 0;i < n;++i)
    {
        cin>>j;
        number.push_back(j);
    }

    int maxNum;
    maxNum = max(number,n);
    cout<<"The max | number is:"<<maxNum<<endl;

    return 0;
}

long max(vector<long> &number,int n)
{
    int maxNum = 0;
    int k = 0;
    for(int i = 0;i < n;++i)
    {
        for(int j = 0;j < n;++j)
        {
            k = number[i] | number[j];
            if(k > maxNum)
                maxNum = k;
        }
    }

    return maxNum;
}

3 楼

#include "iostream.h"
#define max 100000 
main(){
    long ln;
    long larray[max];
    long lresault[max];
    long lmax;

    cout<<"Please input integer number:";
    cin>>ln;
    cout<<"Please input "<<ln<<" numbers:";
    for(int i=0;i<ln;i++)    
        cin>>larray[i];
    for(i=0;i<ln;i++)
    lresault[i]=larray[i]^ln;
    
    lmax=lresault[0];
    for(i=0;i<ln;i++)
        if(lmax<lresault[i])
            lmax=lresault[i];

        


    //cout<<"The number your input is:";
    //for(i=0;i<ln;i++)
        //cout<<larray[i]<<" ";

    //cout<<"The resault number is:";
    //for(i=0;i<ln;i++)
        //cout<<lresault[i]<<" ";

    cout<<"The max different is "<<lmax;

    return;
}
现在只是将最笨的思路展现出来,没有考虑任何的简化、优化;

4 楼

//Part_One

// DOS1.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include "windows.h"
#include <time.h>

using namespace std;

//#define TEST                 //TEST 表示的是测试。没有定义为手动输入
//#define ONLYONE            //ONLYONE 表示的是单线程. 没有定义表示为多线程

typedef struct tag_Num_Param
{
    unsigned int *pData;            //数据
    int nCount;                        //元素个数
    int nStart;                        //计算的开始位置
    int nEnd;                        //计算的结束位置
    unsigned int nIndex1;
    unsigned int nIndex2;
    unsigned int nResult;

    HANDLE hEvent;                //互斥对象
}NUM_PARAM;

DWORD WINAPI Thread_Calculate_Number(LPVOID lpParam);

5 楼

//Part two
int _tmain(int argc, _TCHAR* argv[])
{
    int nCount;        //数据个数
    unsigned int *pnNumber;            //数据
    unsigned long  tm1, tm2, dwUseTime;        //所用时间
    unsigned int nValue1, nValue2, nResult;        // 1 ^ 2 = nResult
    int nret;

    HANDLE *pHandle;        //线程句柄point
    int nHandle;            //线程个数
    NUM_PARAM *pThread_Param;    //线程参数

    //初始化
    nCount = 0;
    pnNumber = 0;
    dwUseTime = 0;
    nValue1 = nValue2 = nResult = 0;
    pThread_Param = 0;

    //个数的输入
#ifndef TEST    
    cout<<"请输入数据个数(2<=n<=100000)"<<endl;
    cout<<"输入个数:";
    while(1)
    {
        cin >> nCount;
        if( 2<= nCount && nCount <= 100000)
        {
            break;
        }
        else
        {
            cout<<"输入错误.请重新输入(2<=n<=100000)"<<endl;
            cout<<"请输入个数:";
        }
    }
#else
    cout<<"请输入数据个数(2<=n<=100000)"<<endl;
    cout<<"输入个数: 100000"<<endl;
    nCount = 100000;
#endif
    
    //空间的申请
    pnNumber = new unsigned int[nCount];
    if(!pnNumber)
    {
        cout<<"空间申请失败,程序退出"<<endl;
        system("pause");
        return 0;
    }
    memset(pnNumber, 0, sizeof(int) * nCount);

    //数据的录入
    srand( (unsigned)time( NULL ));
    for(int i=0; i<nCount; i++)
    {
#ifndef TEST
        cin>>pnNumber[i];
#else
        pnNumber[i] = rand()%100000000;
#endif
    }
    
    tm1 = ::GetTickCount();
    //计算
#ifdef ONLYONE
    unsigned int nRe_Now = 0;
    for(int i=0; i<nCount; i++)
    {
        for(int j=i+1; j<nCount; j++)
        {
            nRe_Now = pnNumber[i] ^ pnNumber[j];
            if(nRe_Now > nResult)
            {
                nResult = nRe_Now;
                nValue1 = i;
                nValue2 = j;
            }
        }
    }
#else
    //多线程方式
    //nHandle = nCount/10000;
    nHandle = (nCount+1)/10000;

    //线程参数空间的申请
    pThread_Param = new NUM_PARAM[nHandle];
    if(!pThread_Param)
    {
        cout<<"线程参数空间分配失败"<<endl;
        delete []pnNumber;
        return 0;
    }

    //线程句柄的空间的申请
    pHandle = new HANDLE[nHandle];
    if(!pHandle)
    {
        cout<<"线程句柄空间分配失败"<<endl;
        delete []pnNumber;
        delete []pThread_Param;
        return 0;
    }

    for(int i=0; i<nHandle; i++)
    {
        //参数初始化
        pThread_Param[i].pData = pnNumber;
        pThread_Param[i].nCount = nCount;
        pThread_Param[i].nStart = i*10000;
        pThread_Param[i].nEnd = ((i+1)>=nHandle)?(nCount - 1): ((i+1)*10000-1);
        pThread_Param[i].nIndex1 = 0;
        pThread_Param[i].nIndex2 = 0;
        pThread_Param[i].nResult = 0;
        pThread_Param[i].hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

        pHandle[i] = CreateThread(NULL, 0, Thread_Calculate_Number, (LPVOID)&pThread_Param[i], 0, NULL);
        CloseHandle(pHandle[i]);
    }

    for(int i=0; i<nHandle; i++)
    {
        WaitForSingleObject(pThread_Param[i].hEvent, INFINITE);
        CloseHandle(pThread_Param[i].hEvent);
    }

    //结果 
    for(int i=0; i<nHandle; i++)
    {
        if(pThread_Param[i].nResult  > nResult)
        {
            nResult = pThread_Param[i].nResult;
            nValue1 = pThread_Param[i].nIndex1;
            nValue2 = pThread_Param[i].nIndex2;
        }
    }
#endif
    tm2 = ::GetTickCount();
    dwUseTime = tm2 - tm1;
    printf("%d-[%d] ^ [%d]-[%d] = [%d]----[time:%ds]\n", nValue1, pnNumber[nValue1], nValue2, pnNumber[nValue2], nResult, dwUseTime/1000);
    delete []pnNumber;
#ifdef ONLYONE
    ;
#else
    delete []pThread_Param;
    delete []pHandle;
#endif
    system("pause");
    return 1;
}

6 楼

//Part three
DWORD WINAPI Thread_Calculate_Number(LPVOID lpParam)
{
    NUM_PARAM *pTemp = (NUM_PARAM*)lpParam;
    unsigned int nResult_Now=0;

    for(int i = pTemp->nStart; i<=pTemp->nEnd; i++)
    {
        for(int j=i+1; j<pTemp->nCount; j++)
        {
            nResult_Now = pTemp->pData[i] ^ pTemp->pData[j];
            if(nResult_Now > pTemp->nResult)
            {
                pTemp->nResult = nResult_Now;
                pTemp->nIndex1 = i;
                pTemp->nIndex2 = j;
            }
        }
    }
    SetEvent(pTemp->hEvent);
    return 0;
}
//////////////VS2003, Windows XP

7 楼

#include<iostream.h>

typedef struct node
{
    long int data;
    node * next;
}node;
int main()
{
    long int n,x,temp,result=0;
    node *p=new node,*head=p;
    cout<<"please enter a number between 2 to 100000!"<<endl;
    cin>>n;
    cout<<"please enter "<<n<<" numbers between 0 to 1e9,box of blank!"<<endl;
    for(int i=1;i<=n;i++){
        cin>>x;
        p->next=new node;
        p=p->next;
        p->data=x;
        p->next=NULL;
    }
    p=head->next;
    while(p){
        temp=n^(p->data);
        if(result<temp)
            result=temp;
        p=p->next;
    }
    cout<<endl;
    cout<<n<<endl;
    p=head->next;
    while(p){
        cout<<p->data<<"  ";
        p=p->next;
    }
    cout<<endl;
    cout<<result<<endl;
    return 0;
}

8 楼

#include<stdio.h>
main()
{
    int n,i,j;
    int *pointer,max;
    printf("Please input number:\n");
    scanf("%d",&n);
    printf("Please input data:\n");
    pointer=(int *)calloc(sizeof(int),n);
    for(i=0;i<n;i++)
        scanf("%d",pointer+i);
    printf("Input numbers:\n");
    for(i=0;i<n;i++)
        printf("%d%c",*(pointer+i),'\t');
    printf("\n");
    max=*(pointer)^*(pointer+1);
    for(i=0;i<n;i++)
        for(j=i+1;j<n;j++)
    {
        if((*(pointer+i)^*(pointer+j))>max)
            max=*(pointer+i)^*(pointer+j);
    
    }
    printf("Max=%d\n",max);
    free(pointer);
}

9 楼

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h> 
#include <limits.h>
#include <assert.h>
#include <conio.h>

int        nMaxDigits = 1;

int GetMaxDigits(unsigned int nums[], int n)
{
    unsigned int    nMax = nums[0];
    
    for(int i=1; i<n; ++i)
    {
        if(nMax < nums[i])
            nMax = nums[i];
    }
    
    int        nDigits = 0;
    while(nMax > 0)
    {
        nMax >>= 1;
        ++nDigits;
    }
    
    // 如果最大数是0,也应该是1位
    if(nDigits == 0)
        nDigits = 1;
    
    return nDigits;
}

// 根据整数的第m位(从高位起计数)把[x1,x2]范围内的整数划分成高位是0
// 和高位是1的两部分,返回中间的位置,即最后一个0的位置
int Partition(unsigned int nums[], int x1, int x2, int m)
{
    int                i, j;
    unsigned int    temp;
    
    i = x1, j = x2;
    temp = nums[i];

    while(i < j)
    {
        while((nums[j] & (1<<m))  > 0 && i < j )
            --j;
        if(i < j)
            nums[i++] = nums[j];
        while((nums[i] & (1<<m)) == 0 && i < j )
            ++i;
        if(i < j)
            nums[j--] = nums[i];
    }

    nums[i] = temp;

    if((temp & (1<<m)) > 0)
        return i-1;
    else
        return i;
}


unsigned int Divide(unsigned int nums[], int x1, int x2, int y1, int y2, int m, unsigned int maxxor)
{
    if(x1 > x2 || y1 > y2)    // 当m<0和本条件同时满足时,以本条件优先
        return 0;

    if(m < 0)
        return maxxor;
    
    if(x1 == x2 && y1 == y2)
        return nums[x1] ^ nums[y1];


    int        x0, y0;
    
    x0 = Partition(nums, x1, x2, m);
    y0 = Partition(nums, y1, y2, m);

    maxxor <<= 1;

    if(x1 > x0 && y1 > y0 || x0 == x2 && y0 == y2)
        return Divide(nums, x1, x2, y1, y2, m-1, maxxor);
    else
    {
        unsigned int        maxxor1, maxxor2;
        maxxor1 = Divide(nums, x1, x0, y0+1, y2, m-1, maxxor+1);
        maxxor2 = Divide(nums, y1, y0, x0+1, x2, m-1, maxxor+1);
        return maxxor1>maxxor2?maxxor1:maxxor2;
    }
}

unsigned int MaxXOR(unsigned int nums[], int n)
{
    assert(n > 0);

    if(n == 1)
        return nums[0];
    else if(n == 2)
        return nums[0] ^ nums[1];

    nMaxDigits = GetMaxDigits(nums, n);

    int        m = nMaxDigits - 1;
    int        x1 = 0, x2 = n-1;
    int        x0 = Partition(nums, x1, x2, m);

    while((x1 > x0 || x0 == x2) && (m>=0))
    {// 如果划分后只有一个集合,则选择次高位重新划分
        x0 = Partition(nums, x1, x2, --m);
    }

    if(m < 0)    // 所有数都相等
        return 0;

    return Divide(nums, x1, x0, x0+1, x2, m-1, 1);
}



int main()
{
    int                n;
    unsigned int    nMax = 0;
    unsigned int    *nums;

    FILE            *fp;
    int                i;
    
    if((fp = fopen("test.txt", "rt")) == NULL)
    {
        printf("Error open file!\n");
        exit(1);
    }

    fscanf(fp, "%d", &n);

    nums = new unsigned int[n];
    assert(nums != NULL);

    for(i=0; i<n; ++i)
        fscanf(fp, "%d", nums+i);

    fclose(fp);

    nMax = MaxXOR(nums, n);

    printf("%u\n", nMax);

    delete []nums;

    return 0;
}

10 楼

内容隐藏,本帖结帖后才能浏览。

我来回复

您尚未登录,请登录后再回复。点此登录或注册