2013年复试题目

1. 编写程序,计算1~20000之间的质数,输出时要求每行10个数.

#include <iostream>
#include <iomanip>

using namespace std;

bool isPrim(const int );

int main()
{
    int count = 1;
    for(int i = 2; i < 20000; i++)//2是质数
    {
        if(isPrim(i))
        {
            cout << left << setw(10) << i << " ";
            if(count %10 == 0)
                cout << endl;
            count++;
        }
    }
    return 0;
}

bool isPrim(const int number)
{
    for(int i = 2; i < number/2; i++) //改进,判断条件可以改成i<=static_case<int> sqrt(static_cast<double>(number)))
        if(number%i == 0)
        return false;
    return true;
}

2. 编写简单的加密,解密程序.在main()函数中接收需要加密的字符串,进行加密。加密时,将字符指针+1,Encrpy的参数为字符指针。解密时将字符指针-1,Decrpy的参数亦为字符指针。

#include <iostream>

using namespace std;

void Encrpy(char []);
void Decrpy(char []);

int main()
{
    char ch[100];
    cin.getline(ch,100);
    Encrpy(ch);
    cout << ch << endl;
    Decrpy(ch);
    cout << ch << endl;
    return 0;
}

void Encrpy(char a[])
{
    int i = 0;
    while(a[i])
    {
        a[i]++;
        a++;
    }
}

void Decrpy(char a[])
{
    int i = 0;
    while(a[i])
    {
        a[i]--;
        a++;
    }
}

3. 编写如下算法:

  1. 选择排序
#include <iostream>

using namespace std;

void selectSort(int [],int );
void swap(int &, int &);

int main()
{
    int a[] = {7,4,3,6,5,8,1,9,0,2};
    selectSort(a,10);
    for(int i = 0; i < 10; ++i)
        cout << a[i] << " ";
    return 0;
}

void selectSort(int a[] ,int len)
{
    int small;
    for(int i = 0; i < 10; i ++)
    {
        small = i;
        for(int j = i; j < 10; j++)
            if(a[j] < a[small])
                small = j;
        swap(a[i],a[small]);
    }

}

void swap(int &a,int &b)
{
    int c;
    c = a;
    a = b;
    b = c;
}

  1. 桶排序

4. 对应于ASCII字符中33~126之间的字符,将其转化为10进制,8进制,16进制,以及ACII码输出到文件,在该文件中依次输出四张ASCII表格。

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
    ofstream ofs("output.txt",ios::out);
    for(int i = 33; i < 127; i++)
        ofs << setw(5) << left << dec << i
            << setw(5) << left << oct << i
            << setw(5) << left << hex << i
            << setw(5) << left << static_cast<char>(i) << endl;
    return 0;

}

5. 处理字符串(025)87234865-987,用strtok处理,以“区号 电话 分机号”的格式输出。

#include <iostream>
#include <cstring>
#include <iomanip>

using namespace std;

int main()
{
    char str[19];
    cout << "Enter number :(like(125)87123865-987)" << endl;
    cin.getline(str,19);
    char *areaCode, *phoneNumber, *Exc;
    areaCode = strtok(str,"() ");
    cout << strlen(areaCode) << endl;
    phoneNumber = strtok(NULL," -");
    Exc = strtok(NULL,"");
    cout << setw(20) << left << "quhao" << setw(20) << left << "number" << setw(20) << left << "exc" << endl;
    cout << setw(20) << left << areaCode << setw(20) << left << phoneNumber << setw(20) << left << Exc << endl;

    return 0;
}

6. 已知:Person类

包含3个数据成员(name,nationality,sex)和三个成员函数(构造函数,printName函数和printNationality函数),其中name的数据类型为Name类。Name类包含三个数据成员(first,middle,last)和两个成员函数(构造函数和printName函数)
定义Person类和Name类,并编写程序测试这两个类的所有接口。

同2012.4

猜你喜欢

转载自blog.csdn.net/Dcwjh/article/details/88690523
今日推荐