中兴笔试程序题

文本编辑器(15

要求: 

1)编辑文本;

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

3)具有输入输出界面。

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

#include<fstream.h>
#include<string.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>

void input()
{	   
	char str[100];
	ofstream out("d:\\test.txt");
	if(!out)
	{
		cout<<"Cannot open output file.\n";
		
	}
	cout<<"please input:"<<endl;
	gets(str);
	out<<str;
	cout<<endl;
	out.close();
	
}
void output()
{
	
	ifstream in("d:\\test.txt");
	if(!in)
	{
		cout<<"Cannot open input file.\n";
		
	}
	char str;
	while(in.get(str))
	{
		cout<<str;
	}
	cout<<endl;
	in.close();
}

void main()
{   
	int j=1;
    char i[10];
    int n;	
	do {
		cout<<"1.写入文本"<<endl;
		cout<<"2.读文本"<<endl;
		cout<<"3.退出程序"<<endl;
		cout<<"请选择:";
		cin>>i;
		if((i[0]<49)||(i[0]>51))
		{
			cout<<"Input error!!"<<endl;
			goto end;
		}
		else n=i[0]-48;
        switch(n){
		case 1:
			input();
			break;
		case 2:
			output();
			break;
		case 3:
			exit(0);
		default:
			cout<<"输入错误!"<<endl;
		}
end:
		;
	}while(j==1);
}

2

.

#include <iostream>
#include <cstdio>
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 <iostream>
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;i<N;i++)                            
                                         scanf("%d%d",&p[i].x,&p[i].y);
                         for(ans=0,i=0;i<N-1;i++)
                         {
                                         for(j=i+1,l=0;j<N;j++,l++)
                                         {
                                                           if(p[i].x==p[j].x)
                                                                             k[l]=INF;
                                                           else
                                                               k[l]=double(p[i].y-p[j].y)/(p[i].x-p[j].x);
                                         }
                                         qsort(k,l,sizeof(k[0]),cmp);
                                         for(j=0;j<l;j++)
                                         {
                                                           counter=1;
                                                           while(j+1<l && k[j]==k[j+1])
                                                           {
                                                                       j++;
                                                                       counter++;
                                                           }
                                                           if(counter>ans)
                                                                          ans=counter;
                                         }
                         }
                         printf("%d/n",ans+1);
    }
    return 0 ;
}
思路:分别求出其中一点与其它点的直线的斜率,进行排序,如果斜率相同则同一条直线。

#include <iostream>
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<maxNum;++i)
        cout << *maxIndex++ << ' ';
    cout << endl;
}


int main()
{
    char* str = "abcabcdabcd";
    FindStr(str); 

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值