Nvidia 笔试题

1. 

#include <iostream>
using namespace std;

struct A {
    int a;  //4
    long b; //4
    char c; //1
                    // align to 12
};

void main(){
    cout << sizeof(A)<<endl;
    A a[100];

    int dif = (char*)(&a)[1]-(char*)(&a)[0];
    cout<< (&a)[1]<<"  "<<(&a)[0]<< "   "<<dif<<endl;
     dif = (int*)(&a)[1]-(int*)(&a)[0];
    cout<< (&a)[1]<<"  "<<(&a)[0]<< "   "<<dif<<endl;
}

// result:
// 12
// 0018FF48  0018FA98   1200
// 0018FF48  0018FA98   300

2.

#include <iostream>
using namespace std;
#define SQUARE(a) (a * a)


void main(){
    int a=3;
    int b=4;
    cout << SQUARE (a+b)<<endl; // 19  只替换 相当于  a+b*a+b

    cout << SQUARE ( (a+b) )<<endl;  // 49
}

3.

#include <iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};

void main(){
    Node *a = new Node;

    a ->data = 10;
    a->next = NULL;
    Node *b= new Node;

    b -> data = 20;
    b->next = a;

    cout << (*(*b).next).data <<endl;  //   (*b).data 等同于 b->data ; 

    cout << b->next->data <<endl;  //
}

4. 继承与虚函数

#include <iostream>
using namespace std;

class Base {
public:
    void f(){ 
        cout << "Base::f" << endl; 
    }
};

class Derived: public Base
 {
public:
    void f(){ 
        cout << "Derived::f" << endl; 
    }
};

void main(){
    Base *b = new Derived();
    b->f();
}

结果为 

Base::f
#include <iostream>
using namespace std;

class Base {
public:
    virtual void f(){ 
        cout << "Base::f" << endl; 
    }
};

class Derived: public Base
 {
public:
    void f(){ 
        cout << "Derived::f" << endl; 
    }
};

void main(){
    Base *b = new Derived();
    b->f();
}

结果为

Derived::f

因为虚函数会被子类的同名函数替换,若子类的同名函数还是虚函数则还会被覆盖。

5. 逆置链表

#include <iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};

Node* reverseList(Node *head){
    Node *pre,*cur;
    pre= NULL;
    cur = head;

    Node *newList = new Node();
    newList->data= cur->data;
    newList->next = NULL;

    while (cur->next){
        pre=cur;
        cur=cur->next;
        Node *a = new Node();
        a->data = cur->data;
        a->next = newList;
        newList = a;
    }
    return newList;
}


void main(){
    Node *m = new Node();;
    m->data = 9;
    m->next = NULL;

    Node *n = new Node();
    n->data = 7;
    n->next = m;

    Node *k = new Node();
    k->data = 2;
    k->next = n;

    cout << k->data <<" "<< k->next->data<<" "<< k->next->next->data<<endl ;   // 2 7 9

    Node *result;
    result = reverseList(k);
    cout << result->data <<" "<< result->next->data<<" "<<result->next->next->data<<endl ;  // 9 7 2

}

6.数组全排列 

思路如下:

全排列是将一组数按一定顺序进行排列,如果这组数有n个,那么全排列数为n!个。现以{1, 2, 3, 4, 5}为例说明如何编写全排列的递归算法。

1、首先看最后两个数4, 5。 它们的全排列为4 5和5 4, 即以4开头的5的全排列和以5开头的4的全排列。由于一个数的全排列就是其本身,从而得到以上结果。

2、再看后三个数3, 4, 5。它们的全排列为3 4 5、3 5 4、 4 3 5、 4 5 3、 5 3 4、 5 4 3 六组数。即以3开头的和4,5的全排列的组合、以4开头的和3,5的全排列的组合和以5开头的和3,4的全排列的组合.从而可以推断,设一组数p = {r1, r2, r3, ... ,rn}, 全排列为perm(p),pn = p - {rn}。因此perm(p) = r1perm(p1), r2perm(p2), r3perm(p3), ... , rnperm(pn)。当n = 1时perm(p} = r1。

为了更容易理解,将整组数中的所有的数分别与第一个数交换,这样就总是在处理后n-1个数的全排列。

#include <iostream> 
using namespace std;
int n = 0;

void swap(char *a ,char *b)
{
    int m ;
    m = *a;
    *a = *b;
    *b = m;
} 
 
void perm(char list[],int k, int m )
{
    int i;
    if(k >m)
    {
        for(i = 0 ; i <= m ; i++)
        {
            cout<<"r"<<list[i];
             
        }
        cout<<endl;
        n++;
    }
    else
    {
        for(i = k ; i <=m;i++)
        {
            swap(&list[k],&list[i]);
            perm(list,k+1,m);
            swap(&list[k],&list[i]);
        }
    }
}

int main()
{
    char list[] ="12345";
    perm(list,0,4);
    cout<<"total:"<<n<<endl;     
    return 0;
}

posted on 2013-10-19 16:53  daniel+  阅读(394)  评论(0编辑  收藏  举报

导航