中兴笔试程序题

文本编辑器(15

要求: 

1)编辑文本;

2)保存、打开指定位置的文本文件;

3)具有输入输出界面。

代码:(此代码在vc6.0中运行是正确的,在vs2010中是编译不过的)

#include
#include
#include
#include
#include

void input()
{	   
	char str[100];
	ofstream out("d:\\test.txt");
	if(!out)
	{
		cout<<"Cannot open output file.\n";
		
	}
	cout<<"please input:"<>i;
		if((i[0]<49)||(i[0]>51))
		{
			cout<<"Input error!!"<

2

.中兴笔试程序题_第1张图片

#include 
#include 
using namespace std;

struct TreeNode {
	char data;
	TreeNode *left;
	TreeNode *right;
};

void getHeight(TreeNode *T, int &h)
{
	if (T == NULL)
		h = 0;
	else {
		int left_h;
		getHeight(T->left, left_h);
		int right_h;
		getHeight(T->right, right_h);
		h = 1 + max(left_h, right_h);
	}
}

TreeNode *CreateBiTree(TreeNode *&T) {
	// 按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树,
	// 构造二叉链表表示的二叉树T。
	char ch;
	cin >> ch;
	if (ch == '#')
		T = NULL;
	else {
		if (!(T = (TreeNode *)malloc(sizeof(TreeNode))))
			return 0;
		T->data = ch;              // 生成根结点
		CreateBiTree(T->left);   // 构造左子树
		CreateBiTree(T->right);   // 构造右子树
	}
	return T;
} // CreateBiTree

void Free(TreeNode *&T)
{
	if (T == NULL)
		return;

	Free(T->left);
	//	T->left = NULL;
	Free(T->right);
	//	T->right = NULL;
	free(T);
	T = NULL;
}

int main()
{

	TreeNode *T = NULL;
	CreateBiTree(T);

	int height;
	getHeight(T, height);
	cout << height << endl;
	Free(T);

	return 0;
}

3.二维平面上有若干点,求出一条直线能穿越最多的点

#include 
using namespace std;
const double INF=2100000000;
struct point
{
       int x,y;
};
int cmp(const void *a ,const void *b)
{
    if( *(double*)a> *(double *)b)
        return 1;
    else
        return -1;
}
point p[1001];
double k[1001];
int main()
{
    int N;
    int i,j,l,counter,ans;
    while(scanf("%d",&N) !=EOF  && N)
    {
                         for(i=0;ians)
                                                                          ans=counter;
                                         }
                         }
                         printf("%d/n",ans+1);
    }
    return 0 ;
}
思路:分别求出其中一点与其它点的直线的斜率,进行排序,如果斜率相同则同一条直线。

#include 
using namespace std;

void FindStr(char* str)
{
    if(str == NULL)
        return;
    
    char* pStr = str;
    char* qStr = str+1;
    char* index = str;
    char* maxIndex = str;
    int num = 0;
    int maxNum = 0;

    while(*pStr != '\0')
    {
        while(*qStr != *pStr){
            num = 0;
            qStr++;
        }

        while(*qStr == *pStr && *pStr !='\0' && *qStr !='\0')
        {
            num++;
            index = pStr;
            qStr++;
            pStr++;
        }

        if(num>=1)
            index = index-num+1;
        if(num > maxNum){
            maxNum = num;
            maxIndex = index;
        }

        qStr = pStr+1;
    }

    cout << "Result: ";
    for(int i=0;i


你可能感兴趣的:(名企笔试机试题目)