笔试面试题目2

1. 面试宝典上的一个题目(转载):http://blog.csdn.net/thebestdavid/article/details/11599027

        ABCDE*4=EDCBA
        在面试宝典上面看到的一道题目,也是一道老掉牙的题目了,
        题目详情:一个五位数字ABCDE*4=EDCBA,这五个数字不重复,请编程求出来。

        网上流传的代码都是对5位数ABCDE的所有可能情况作遍历,即从10000 - 99999;我的想法是对EDCBA作遍历,从遍历的范围来说,为原来的1/4,因为EDCBA必须能被4整除才可以,然后遍历的初始位置也发生改变,本来是10000,现在直接变成10000 * 4,范围又减少了一半左右。对于这道程序来说,效果并不明显,我仅仅是提供另一种思路,用逆向的思维来解答问题。对于题目中的要求:每个数字必须不重复,在数学的角度来说,这应该是不会重复的,要加上这个不重复的条件也可以,只是程序会变得繁琐不简洁。对这道题来说,加不加影响不大。

#include <iostream>
#include <cmath>

using namespace std;

int findNum(void )
{
    int cnt = 0;
    int N = 0;          //EDCBA
    int Nswitch = 0;    //ABCDE
    for( cnt = 12345 * 4; cnt <= 98765; cnt += 4)    // 初始位置是40000,因为ABCDE最小为10000,那EDCBA最小为40000
    {
        Nswitch = 0;
        N = cnt;
        //将EDCBA变换为ABCDE
        do
        {
            Nswitch = Nswitch * 10 + N % 10;
            N /= 10;
        }while(N != 0);

        if( (cnt >> 2) == Nswitch)
        {
            return cnt; //找到该数并返回
        }
    }

    return -1;//找不到,返回-1
}

int main()
{
    int result = findNum();
    switch(result)
    {
        case -1:
            {
                printf("the number isn't exist!\n");
                break;
            }
        default:
            {
                printf("the ABCDE is  %d\n", result / 4);
                printf("the EDCBA is  %d\n", result);
                break;
            }
    }

    return 0;
}

2.  人人笔试题目:

(1) 给定一个有序数组a,长度为len,和一个数X,判断A数组里面是否存在两个数,他们的和为X,bool judge(int *a, int len, int x),存在返回true,不存在返回false

#include <iostream>
#include <cmath>

using namespace std;

bool judge(int *a, int len, int x)
{
    int i = 0, j = len - 1;
    while(i < j)
    {
        if( a[i] + a[j] == x)
        {
            return true;
        }
        else if( a[i] + a[j] > x)
        {
            j--;
        }
        else if( a[i] + a[j] < x)
        {
            i++;
        }
    }

    return false;
}
int main()
{
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    if(judge(a, 9, 13))
    {
        cout << "exists pair!" << endl;
    }
    else
    {
        cout << "not exists pair!" << endl;
    }
    return 0;
}
         (2)给定有n个数的数组a,其中超过一半的数为一个定值,在不进行排序、不开设额外数组的情况下,以最高效的算法找到这个数:int find(int *a, int n)
#include <iostream>
#include <cmath>

using namespace std;

int find(int *a, int n)
{
    int result = -1;
    int nTimes = 0;
    for(int i = 0; i < n; i++)
    {
        if( nTimes == 0)
        {
            nTimes = 1;
            result = a[i];
        }
        else if( result == a[i])
        {
            nTimes++;
        }
        else
        {
            nTimes--;
        }
    }

    return result;
}


int main()
{
    int a[] = {1, 2, 2, 4, 2, 2, 7, 2, 2};
    int result = find(a, 9);

    cout << "Elements: " << result << endl;

    return 0;
}


3. strtok()函数,第一次调用时,返回第一个字符,以后每次调用返回剩下的分割的字符串,直到结束时返回NULL

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

char * _tokstr = NULL;

char * mystrtok(char *s, char * tokens)
{
    if(s != _tokstr && s != NULL)
    {
        _tokstr = s;
    }

    int token[256];
    char * p = tokens;
    for( int i = 0; i < 256; i++)
    {
        token[i] = 0;
    }
    while( *p != '\0')
    {
        token[(unsigned char)(*p)] = 1;
        p++;
    }

    p = _tokstr;
    char * result = NULL;
    while( *p != '\0')
    {
        if( token[(unsigned char)(*p)] == 1)
        {
            *p = '\0';
            result = _tokstr;
            _tokstr = p+1;
            break;
        }
        p++;
    }

    return result;
}

int main()
{
    char str[] = "ab.,c,in.t,size\0";
    char tok[] = ",.\0";
    char * ret = mystrtok( str, tok);
    while( ret != NULL)
    {
        cout << ret << endl;

        ret = mystrtok( NULL, tok);
    }
    return 0;
}

4. 使用下标对数组进行排序:

        在寻找一个N个元素的数组,元素取自属于[0, N-1],判断数组中是否有重复元素。

        使用的方法有,遍历数组,将位置i的数字j 换到下标为j 的位置上,所有数字出现于自己位置或出现冲突时结束。所有数字出现在自己位置,则说明没有重复数字,出现冲突,则说明存在重复数字。

        使用本方法做判断,首先要将元素都放到自己作为下标的数组中。

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;
// true: 有重复  false:没有重复
bool JudgeRepeat( int a[], int n)
{
    bool hasOrder = false;
    while(!hasOrder)
    {
        hasOrder = true;
        for(int i = 0; i < n; i++)
        {
            if(a[i] != i)
            {
                if(a[i] == a[a[i]])
                    return true;
                hasOrder = false;
                int tmp = a[a[i]];
                a[a[i]] = a[i];
                a[i] = tmp;
            }
        }
    }

    return false;
}

int main()
{
    int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    random_shuffle(a, a+10);
    if( JudgeRepeat( a, 10))
    {
        cout <<"There are repeat Numbers!" << endl;
    }
    else
    {
        cout <<"There are no repeat Numbers!" << endl;
    }

    int b[10] = {0, 1, 1, 3, 4, 5, 6, 7, 8, 9};
    random_shuffle( b, b+10);
    if( JudgeRepeat( b, 10))
    {
        cout <<"There are repeat Numbers!" << endl;
    }
    else
    {
        cout <<"There are no repeat Numbers!" << endl;
    }

    return 0;
}

        使用这种方法对数组进行排序(注意一遍无法保证排序完成,因此需要多遍历几次数组才能保证全部有序,反例见本代码之后)

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;


int main()
{
    int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    random_shuffle(a, a+10);

    for(int i = 0; i < 10; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;

    bool hasOrder = false;
    while(!hasOrder)
    {
        hasOrder = true;
        for(int i = 0; i < 10; i++)
        {
            if(a[i] != i)
            {
                hasOrder = false;
                int tmp = a[a[i]];
                a[a[i]] = a[i];
                a[i] = tmp;
            }
        }
    }

    for(int i = 0; i < 10; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
    return 0;
}
反例代码:
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    random_shuffle(a, a+10);

    for(int i = 0; i < 10; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;

    for(int i = 0; i < 10; i++)
    {
        if(a[i] != i)
        {
            int tmp = a[a[i]];
            a[a[i]] = a[i];
            a[i] = tmp;
        }
    }

    for(int i = 0; i < 10; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
    return 0;
}

运行结果:

8 1 9 2 0 5 7 3 4 6
0 1 2 6 4 5 3 7 8 9

5. 


By  Andy   @  2013年10月8日

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值