笔面试中的机试

笔面试中的机试

        计算机的从业人员,coding是必不可少的技能,真正的实战成了一些公司的考察内容。从未参加过这种实战性质的考核,前两场基本以失败告终,特此总结以做警告,以作为今后同样考核方式的一个参考与经验教训。

        两次机试的失误与教训:

        由于以前没有进行过实战性质的活动,包括竞赛,公司笔面试考核等,因此欠缺经验,吃亏不小。机试一般都是使用Online Judge系统作为评判,对于那些ACM大神,这无疑是小菜一点,但是对于我这个菜鸟(两三年前竟然不知道ACM为何物,汗颜至极啊)来说,这无疑会不知所措。

        碰到的问题:第一个是程序的输入输出的问题,一般调程序,直接将输入写到代码里面,数组或字符串,再或是其他的数据结构。很少自己通过文件或是标准输入输出进行输入(因为这样输入很麻烦),因此碰到了这些online judge时,需要从标准输入输出中读取输入的内容,并且输入的内容是格式化形式的,或者从文件中读取内容,这个内容也基本是格式化的。因为没做过,所以手忙脚乱,不知道scanf读进来的对不对,读取文件有没有简单的方法,两次都是在不断地鼓捣这个内容的输入与结果的输出。第二个,平时写程序很随意,只要运行成功了,简单的几个测试用例过了就OK了,不会过多考虑。但是碰到了这种online judge系统之后,最简化的情况是它会输入几个测试用例,如果测试用例过了,就算程序对了,不过就不对。这样就很容易挂掉,平时写程序并不会考虑这些。结果根据提示的例子写出的代码,只能运行一部分的测试用例,而没法通过所有的测试用例。

        针对这两个问题,要进行解决,第一个其实简单,把C/C++的输入输出好好熟悉一下就差不多了,再做一些练习,包括读文件写文件,读标准输入输出,以后碰到问题应该就不大了。但是第二个却不是那么好解决的问题,需要培养一种思维方式,在写代码之前,要尽可能多地想其用到的测试用例。每一个测试用例(有用的)其实就是程序中需要处理的一些逻辑的反映。

        用了一下午时间,总结了一下前者,见前一篇文章。后者还需要慢慢体会了,慢慢积累,多写多试多改才能达到一个比较好的效果。


总结就这么多吧,还是这些题目放在这吧,以作为以后的一个参考。

公司H:

        1. 对输入的正整数进行编码,编码规则为按照对应关系,将数字中的数字做如下规则的变换:
                (1,9)(2,8)(3,7)(4,6)(5,0)

        例如:
        输入:123
        输出:987

        无时间与空间限制。

#include <iostream>
using namespace std;

int main()
{
    int n = 0;
    int result[64] = {0};
    cin >> n;

    if(n < 0)
    {
        return 1;
    }
    if(n == 0)
    {
        cout << 5;
    }

    int count = 0;
    while( n > 0)
    {
        result[count] = n % 10;
        n /= 10;
        count++;
    }

    for( int i = 0; i < count; i++)
    {
        switch(result[i])
        {
        case 0:
            result[i] = 5;
            break;
        case 1:
            result[i] = 9;
            break;
        case 2:
            result[i] = 8;
            break;
        case 3:
            result[i] = 7;
            break;
        case 4:
            result[i] = 6;
            break;
        case 5:
            result[i] = 0;
            break;
        case 6:
            result[i] = 4;
            break;
        case 7:
            result[i] = 3;
            break;
        case 8:
            result[i] = 2;
            break;
        case 9:
            result[i] = 1;
            break;
        }
    }

    for( int i = count-1; i >= 0; i--)
    {
        cout << result[i];
    }

    return 0;
}

        2. 古人有"三天打鱼两天晒网"之说,给出一天,计算这一天是打鱼还是晒网,如果是打鱼输出Fishing,否则输出Sleeping。

        注:以1990年1月1日算起,这一天是第一天,是打鱼的日子。

        例如:
        输入:1991 01 03
        输出:Fishing

#include <iostream>

using namespace std;

bool isLeap( int year)
{
    return (((year % 4 == 0)&&(year % 100 != 0)) || (year % 400 == 0));
}

int main()
{
    int y, m, d, tatalDay;
    cin >> y >> m >> d;

    if( y > 1990)
    {
        for( int i = 1990; i < y; i++)
        {
            if(isLeap(i))
            {
                tatalDay += 366;
            }
            else
            {
                tatalDay += 365;
            }
        }
    }


    if( m > 1)
    {
        for( int i = 1; i < m; i++)
        {
            switch( i)
            {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                tatalDay += 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                tatalDay += 30;
                break;
            case 2:
                if(isLeap(y))
                {
                    tatalDay += 29;
                }
                else
                {
                    tatalDay += 28;
                }
                break;
            }
        }
    }

    tatalDay += d;

    int ret = tatalDay % 5;
    if( ret >= 1 && ret <= 3)
    {
        cout << "Fishing";
    }
    else
    {
        cout << "Sleeping";
    }

    return 0;
}

        3. 给定一个英文句子,其中包含英文单词,逗号,句号,不再包含其他的符号。

        对该句子进行处理,删除其中的已经出现过的单词,也即删除重复单词(保留第一次出现的单词),以及逗号和句号。

        注:句子最长不超过200个字符
                句子中单词区分大小写,Where 和 where不是同一个单词
#include <iostream>
#include <set>
#include <string>
using namespace std;

int main()
{
    char str[256] = {0};
    string input, tmp;
    int index = 0;
    set<string> wordSet;
    set<string>::iterator ite;

    getline(cin, input);
    strcpy(str, input.c_str());

    tmp = "";
    while( str[index] != '\0')
    {
        if(str[index] == ',' || str[index] == '.' || str[index] == ' ')
        {
            if(tmp == "")
            {
                index ++;
                continue;
            }

            ite = wordSet.find(tmp);
            if(ite == wordSet.end())
            {
                wordSet.insert(tmp);
                cout << tmp << " ";
            }

            tmp = "";
            index++;
            continue;
        }

        if(str[index] == ' ')
        {
            index++;
            continue;
        }

        tmp = tmp + str[index];
        index++;
    }

    if(tmp != "")
        cout << tmp;

    return 0;
}	        

        公司G:

        Problem One

        Do you know how to read the phone numbers in English? Now let me tell you.
        For example, In China, the phone numbers are 11 digits, like: 15012233444. Someone divides the numbers into 3-4-4 format, i.e. 150 1223 3444. While someone divides the numbers into 3-3-5 format, i.e. 150 122 33444. Different formats lead to different ways to read these numbers:
        150 1223 3444 reads one five zero one double two three three triple four.
        150 122 33444 reads one five zero one double two double three triple four.
        Here comes the problem:
        Given a list of phone numbers and the dividing formats, output the right ways to read these numbers.


        Rules:
        Single numbers just read them separately.
        2 successive numbers use double.
        3 successive numbers use triple.
        4 successive numbers use quadruple.
        5 successive numbers use quintuple.
        6 successive numbers use sextuple.
        7 successive numbers use septuple.
        8 successive numbers use octuple.
        9 successive numbers use nonuple.
        10 successive numbers use decuple.
        More than 10 successive numbers read them all separately.


        Input
        The first line of the input gives the number of test cases, T. T lines|test cases follow. Each line contains a phone number N and the dividing format F, one or more positive integers separated by dashes (-), without leading zeros and whose sum always equals the number of digits in the phone number.
        Output
        For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the reading sentence in English whose words are separated by a space.
  

        Input 
  3
        15012233444 3-4-4
        15012233444 3-3-5
        12223 2-3

        Output 
        Case #1: one five zero one double two three three triple four
        Case #2: one five zero one double two double three triple four
        Case #3: one two double two three      

#include <iostream>
#include <fstream>

using namespace std;

void GetNumStr( int n, char str[])
{
	switch ( n)
	{
	case 0: 
		strcpy(str, "zero");
		break;
	case 1: 
		strcpy(str, "one");
		break;
	case 2: 
		strcpy(str, "two");
		break;
	case 3: 
		strcpy(str, "three");
		break;
	case 4: 
		strcpy(str, "four");
		break;
	case 5: 
		strcpy(str, "five");
		break;
	case 6: 
		strcpy(str, "six");
		break;
	case 7: 
		strcpy(str, "seven");
		break;
	case 8: 
		strcpy(str, "eight");
		break;
	case 9: 
		strcpy(str, "nine");
		break;
	}
}

void GetSuccesiveStr(int n, char str[])
{
	switch ( n)
	{
	case 2: 
		strcpy(str, "double");
		break;
	case 3: 
		strcpy(str, "triple");
		break;
	case 4: 
		strcpy(str, "quadruple");
		break;
	case 5: 
		strcpy(str, "quintuple");
		break;
	case 6: 
		strcpy(str, "sextuple");
		break;
	case 7: 
		strcpy(str, "septuple");
		break;
	case 8: 
		strcpy(str, "octuple");
		break;
	case 9: 
		strcpy(str, "nonuple");
		break;
	case 10: 
		strcpy(str, "decuple");
		break;
	}

	return ;
}

int Successive(char str[], int len)
{
	int ret = 1;
	
	for (int i = 1; i < len; i++)
	{
		if ( str[i] != str[i-1])
		{
			return ret;
		}

		ret++;
	}

	return ret;
}

void GetResult( int len, char src[], string &str)
{
	int index = 0;
	char numstr[64];
	char sucstr[64];
	while ( index < len)
	{
		int succ = Successive( src + index, len - index);

		if ( succ <= 1)
		{			
			memset(numstr, 0, sizeof(numstr));
			GetNumStr( src[index] - '0', numstr);
			str += " ";
			str += numstr;			
		}
		else if (succ <= 10)
		{
			memset(sucstr, 0, sizeof(sucstr));			
			GetSuccesiveStr( succ, sucstr);
            str += " ";
			str += sucstr;

			memset(numstr, 0, sizeof(numstr));
			GetNumStr( src[index] - '0', numstr);
			str += " ";
			str += numstr;			
		}
		else if (succ > 10)
		{			
			for ( int i = 0; i < succ; i++)
			{
				memset(numstr, 0, sizeof(numstr));
				GetNumStr( src[index + i] - '0', numstr);
				str += " ";
				str += numstr;			
			}
		}

		index += succ;
	}
	
	return ;
}

int GetCount(char str[], int &stroflen)
{
	int index = 0;
	char numstr[16];
	int ret = 0;
	int len = strlen( str);
	memset( numstr, 0, sizeof(numstr));	
	while ( index < len && str[index] != '-')
	{
		numstr[index] = str[index];		
		index ++;
		stroflen++;
	}
	numstr[index] = '\0';

	ret = atoi( numstr);

	return ret;
}

void main()
{
	ifstream cin("A-large-practice.in");
	ofstream cout("A-large-practice.out");
	int num = 0, count = 0;
	char phonestr[512];
	char formatstr[512];
	string result;

	cin >> num;
	while( num)
	{
		count++;
		memset( phonestr, 0, sizeof(phonestr));
		memset( formatstr, 0, sizeof(formatstr));
		cin >> phonestr >> formatstr;
		
		result = "";
		int len = (int)strlen( formatstr), index = 0, strindex = 0, numbercount = 0;
		while(index < len)
		{
            if ( formatstr[index] == '\0' || formatstr[index] == '-')
            {
				index++;
				continue;					
            }

			int inc = 0;
			GetResult( GetCount(formatstr + index, inc), phonestr + strindex, result);
			inc = 0;
			strindex += GetCount(formatstr + index, inc);
	            			
			index += inc;
		}
		// cout << phonestr << " " << formatstr << endl;
		cout << "Case #" << count << ":" << result.c_str() << endl;	
		num--;
	}
	
	return ;
}

Problem Two:



By Andy @ 2013-9-29


你可能感兴趣的:(笔面试中的机试)