2017-西工大-软件工程复试-机试代码参考

考试说明:

  1. 以下程序,可以任意选择C、C++、Java编程语言实现;
  2. 要求提交完整的工程文件,源程序文件和可执行文件;
  3. 要求符合软件编程规范,具有良好的程序注释;
  4. 程序需要考虑和处理异常情况,否则将扣分。

一、基本编程题(以下1-4题目,任意完成二道,每道20分,共计40分。)

1.编写程序实现折半查找、冒泡排序或者快速排序算法(任意实现其中一个)

//折半查找:
#include <iostream>
using namespace std;

int binary_search(int *A,int n,int key)
{
    int left=0,right=n-1;
    while(left<=right)
    {
        int mid=(left+right)>>1;
        if(key==A[mid])
            return mid;
        else if(key<A[mid])
            right=mid-1;
        else
            left=mid+1;
    }
    return -1;
}

int main()
{
    int A[8]={4,7,9,12,15,34,36,67};
    int key;
    cin>>key;
    cout<<binary_search(A,8,key);
    return 0;
}


//冒泡排序:
#include<iostream>
using namespace std;

void print(int arr[], int n){//打印数组
    for(int j= 0; j<n; j++){
           cout<<arr[j] <<" ";
        }
    cout<<endl;
}

void BubbleSort(int arr[], int n){//冒泡排序:每次把最大的放到最后
    for (int i = 0; i < n - 1; i++){
        for (int j = 0; j < n - i - 1; j++){
                    if (arr[j] > arr[j + 1]) {
                            int temp = arr[j];
                            arr[j] = arr[j + 1];
                            arr[j + 1] = temp;
                        }
                 }
         }
}

int main(){
    int n; //要排序的数字总数n
    cout<<"要排序的数字总数n:"<<endl;
    cin>>n;
    int s[n];
    cout<<"输入要排序的数字:"<<endl;
    for(int k=0;k<n;k++){//输入要排序的数字;
        cin>>s[k];
    }
    cout<<"排序前结果:"<<endl;
    print(s,n);
    BubbleSort(s,n);
    cout<<"排序后结果:"<<endl;
    print(s,n);
}


//快速排序:
#inlude<iostream>
using namespace std;

void print(int a[], int n)
{
    for(int j= 0; j<n; j++)
	{
           cout<<a[j] <<"  ";
        }
    cout<<endl;
}
void quickSort(int a[], int low ,int high)
{
	if(low<high)  //判断是否满足排序条件,递归的终止条件
	{
		int i = low, j = high;   //把待排序数组元素的第一个和最后一个下标分别赋值给i,j,使用i,j进行排序;
		int x = a[low];    //将待排序数组的第一个元素作为哨兵,将数组划分为大于哨兵以及小于哨兵的两部分
		while(i<j)
		{
		  while(i<j && a[j] >= x) j--;  //从最右侧元素开始,如果比哨兵大,那么它的位置就正确,然后判断前一个元素,直到不满足条件
		  if(i<j) a[i++] = a[j];   //把不满足位次条件的那个元素值赋值给第一个元素,(也即是哨兵元素,此时哨兵已经保存在x中,不会丢失)并把i的加1
		  while(i<j && a[i] <= x) i++; //换成左侧下标为i的元素开始与哨兵比较大小,比其小,那么它所处的位置就正确,然后判断后一个,直到不满足条件
		  if(i<j) a[j--] = a[i];  //把不满足位次条件的那个元素值赋值给下标为j的元素,(下标为j的元素已经保存到前面,不会丢失)并把j的加1
		}
	        a[i] = x;   //完成一次排序,把哨兵赋值到下标为i的位置,即前面的都比它小,后面的都比它大
		quickSort(a, low ,i-1);  //递归进行哨兵前后两部分元素排序 , low,high的值不发生变化,i处于中间
		quickSort(a, i+1 ,high);
	}
}

int main()
{
    int n; //要排序的数字总数n
    cout<<"要排序的数字总数n:"<<endl;
    cin>>n;
    int s[n];
    cout<<"输入要排序的数字:"<<endl;
    for(int k=0;k<n;k++){//输入要排序的数字;
        cin>>s[k];
    }
    cout<<"排序前结果:"<<endl;
    print(s,n);
    quickSort(s,0,n-1);
    cout<<"排序后结果:"<<endl;
    print(s,n);

}

2.从键盘输入一个字符串(不多于80个字符),将其中的数字字符按原顺序组成一个新字符串,并输出。

#include<iostream>
#include<string.h>
using namespace std;
int main(){
    int a[30]={0};
    char str[200];//定义字符串的长度
    cout<<"请输入一个含有数字的字符串(不多于80个)"<<endl;
    cin>>str;
    bool flag=0;//判断是否是数字
    int i=0,j=0;
    int s=0;
    int num=strlen(str);//字符串的长度
    for(i=0;i<num;i++){//循环,逐个字符判断
        while(str[i]>='0' && str[i]<='9' &&i<num ){
            s=s*10+int(str[i])-int('0');
            i++;
            flag=1;
        }
        if(flag==1){
            a[j++]=s;
            s=0;
            flag=0;
        }
        i++;
    }
    i=0;
   cout<<"数字组成的字符串为:"
    while(i<j){
        cout<<a[i];
        i++;
    }
    cout<<endl;
    return 0;
}

3.编写程序,从文件中读入两组整数(2*9个数字),分别构造两个3*3维矩阵,最后计算和输出这两个3*3维矩阵的乘积。例如:

     

#include <iostream>
#include<fstream>
#include<iomanip>
using namespace std;
#define N 100
int main(){
    FILE *fp1,*fp2;//函数指针
	int a[N][N];
	int b[N][N];//定义两个输入矩阵
	fp1=fopen("3.in.txt","r");//打开文本
	fp2=fopen("3.out.txt","w");//保存文本
	int n1,m1,n2,m2;   //n:行 m:列
	fscanf(fp1,"%d%d",&n1,&m1);
	for(int i=1;i<=n1;i++){
		for(int j=1;j<=m1;j++){
		   fscanf(fp1,"%d",&a[i][j]);//矩阵a赋值
		}
	}
    fscanf(fp1,"%d%d",&n2,&m2);
	for(int i=1;i<=n2;i++){
		for(int j=1;j<=m2;j++){
		   fscanf(fp1,"%d",&b[i][j]);//矩阵b赋值
		}
	}
    for(int i=1;i<=n1;i++){//矩阵c计算赋值
		for(int j=1;j<=m2;j++){
			int temp=0;
			for(int k=1;k<=m1;k++){
			   temp+=a[i][k]*b[k][j];
			}
            cout<<temp<<" ";
			fprintf(fp2,"%d ",temp);//格式化输出到一个流文件中
		}
		fprintf(fp2,"\n");
		cout<<endl;
	}
    return 0;
}

4.编写程序,从键盘输入任一正整数n,计算并输出数字n的阶乘n!,n!=n*(n-1)!。例如输入数字为4,则4的阶乘为1×2×3×4=24。

#include <iostream>
using namespace std;

int main()
{
    int n;
    int A=1;//定义一个要输出结果的初始量,1
    cout<<"请输入正整数n:"<<endl;
    cin>>n;
    for(int i=1;i<=n;i++){//一个循环
        A=A*i;//n!=n*(n-1)!;
    }
    cout<<"结果:"<<A<<endl;
}

二. 高级程序题(以下5-9题目,任意完成二道,每道30分,共计60分。)

5.请编写程序实现对二叉树的遍历。

#include <iostream>
#include <stdlib.h>
using namespace std;
/// 构建结点
struct BinTreeNode
{
	int index;
	BinTreeNode *lchild;
	BinTreeNode *rchild;

};

/// 构建二叉树
BinTreeNode *create(BinTreeNode *T)
{
	int input;
	cout << "请输入数据:";
	cin >> input;
	if (0 == input)

	{
		T = NULL;
		return T;

	}
	T = (BinTreeNode *)malloc(sizeof(BinTreeNode));
	if (NULL == T)
	{
		cout << " 内存分配出错" << endl;
		exit(1);
	}
	T->index = input;
	T->lchild = create(T->lchild);
	T->rchild = create(T->rchild);
	return  T;

}

/// 先序遍历
void PreOrder(BinTreeNode *T)
{
	if (NULL != T)
	{
		cout << T->index << " ";
		PreOrder(T->lchild);
		PreOrder(T->rchild);
	}
}

/// 中序遍历
void InOrder(BinTreeNode *T)
{
	if (NULL != T)
	{
		InOrder(T->lchild);
		cout << T->index << " ";
		InOrder(T->rchild);

	}

}

/// 后序遍历
void PostOrder(BinTreeNode *T)

{

	if (NULL != T)
	{
		PostOrder(T->lchild);
		PostOrder(T->rchild);
		cout << T->index << " ";
	}
}



int main()

{
	BinTreeNode *T = NULL ;
	T = create(T);
	if (NULL == T)
	{
		cout << "T = NULL" << endl;
		exit(1);
	}
	cout << "先序遍历" << endl;
	PreOrder(T);

	cout  << endl << "中序遍历" << endl;
	InOrder(T);

	cout << endl << "后序遍历" << endl;
	PostOrder(T);
	cout << endl;
	system("pause");
	return 0;

}

6.读入一个文本文件,并指定某个单词(单词长度不多于5个字符),例如“we”,统计并输出文件中包含所指定单词的数量(注意不区分大小写)。

#include <iostream>
#include <fstream>
using namespace std;
int main(){
    string getWord, testWord;
    int n=0;//计数
    cout<<"输入要查找的特定单词\n";
    cin>>testWord;
    ifstream infile("3.in.txt");

    while(infile>>getWord){
        if(getWord==testWord) 
            ++n;
    }
    cout<<n;
    return 0;
}

7.从键盘上随机输入20个正整数,找出并输出所包含的质数(质数是除了1和它本身之外,不能被其他数整除的正整数,例如2,3,5,11,13等)。

#include<iostream>
#include<math.h>
using namespace std;

bool isPrime(int a)//判断函数
{
    if (a<2)//小于2不是
        return false;
    for (int i=2;i<=sqrt(a);i++)
        if (a%i==0)
            return false;
    return true;
}

int main()
{
    int n; //n个数
    cout<<"输入总数:(20)"<<endl;
    cin>>n;
    cout<<"输入数据:"<<endl;
    for (int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        if (isPrime(a))
            cout<<a<<" ";
    }
    return 0;
}

8.请编写程序,将用户输入的一个字符串(不多于100个字符),其中的单词按照逆序,重新输出。例如输入字符串“this is a dog”,输出为“dog a is this”。

#include<iostream>
#include<stack>
#include<sstream>
#include<string>

using namespace std;

int main(){

    string line,word;
    std::stack<string> words;
    getline(cin, line);

    istringstream sstr(line);

    while (sstr >> word){
        words.push(word);
    }

    while (!words.empty()){
        cout << words.top() << " ";
        words.pop();
    }

    cout << endl;

    return 0;
}

9.编写一个考试成绩统计程序,将从键盘上输入的学生考试成绩(最多40个),计算输出学生平均成绩,以及不及格学生数量(成绩<60分)和成绩优秀学生数量(成绩>=90分)。

#include <iostream>
using namespace std;

int main()
{
    int n;
    cout<<"请输入学生人数(最多40个):"<<endl;
    cin>>n;
    cout<<"请输入学生成绩:"<<endl;
    int A[n];
    int j=0,k=0,sum=0;
    for(int i=0;i<n;i++){
        cin>>A[i];
        if(A[i]<60){
            j++;
        }
        if(A[i]>=90){
            k++;
        }
        sum=sum+A[i];
    }
    double ave;
    ave=sum/n;
    cout<<"平均成绩: "<<ave<<endl;
    cout<<"不及格人数: "<<j<<endl;
    cout<<"优秀人数: "<<k<<endl;
    return 0;
}

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值