1,简单题,不记得了
2,普通题
描述:给定一个输入,介于0~999999之间,并且给定的数是回文数,e.g. 123321,那么统计直到下一个回文数之间的间隔,并且当统计到999999时,下一个数为0,即是循环计数。
e.g. 输入:123321
输出:1100
解答:该题最难之处在于回文数判断,但是这个对于一般程序猿应该不难,尤其是对于这种有限制(0~999999,没有溢出)的判断来说。另一个难点在于999999,当输入是小于999999的前一个回文数(假设为m)时,那么输出就是999999-m,没有变化;唯一的不同是当输入为999999时,下一个数为0,该数也是回文数,则输出为-999999.第二点应该也是机试该题没有满分的原因吧。
代码:
// Author:
// Date: 2014/7/17
// Description: This file deals with the test problems
#include<stdio.h>
#include<stdlib.h>
// Is palindromic ,yes return 1, else return 0. The input is between 0--999999
int IsPalindrome(int i_value)
{
int flag = 1 ;
int i = 0 ;
int value[128] = {0} ;
while(0 != i_value)
{
value[i] = i_value % 10 ;
i_value /= 10 ;
i++ ;
}
for (int j = 0; j < i/2; j ++)
{
if (value[j] != value[i - j - 1])
{
flag = 0 ;
break ;
}
}
return flag ;
}
int main()
{
//=============1. given a "123321", find out the integral till the next palindromic number.
int a ;
scanf("%d",&a) ;
int start = a ;
do
{
a ++ ;
if (999999 + 1 == a)
{
a = 0 ;
}
} while (0 == IsPalindrome(a));
int ans = a - start ;
printf("%d\n",ans) ;
return 0 ;
}
3,困难题
描述:字符串匹配问题,给定两个字符串,求字符串2,在字符串1中的最先匹配结果。字符串2中可以存在'*'符号,且该符号可以代表任意字符,即字符串2中存在通配符。
e.g. 输入:abcdefghabef, a*f
输出:abcdef
解答:该问题有很多小的考点,尤其是华为机试的喜欢考的小考点。
其一,两个输入中间的逗号(这个我专门问了监考,必须得考虑进去),貌似华为喜欢在输入这块折腾人;
其二,在于字符串2中的通配符的匹配问题,通配符匹配需要考虑的问题有:
1)通配符是否会在第一位,这个我想了很久,但是如果在第一位的话,那么依据题意,通配符可以匹配任意字符(串),则其结束位置就为字符串2与字符串1匹配的位置;
2)通配符如果位于结束的时候,怎么处理?以我的理解则直接匹配到目标字符串的结束;
3)如果只有通配符,怎么处理?
4)四是,如果有多个通配符怎么处理?
下面的程序对通配符的前三个小问都给了解答,但是第4个没有做到,如果有人实现了,欢迎交流。
程序很乱,这是在华为机考时做的,但是当时没有考虑到通配符位于开头和结尾的情况和只有通配符的情况,结果160分得了101.
程序的主题思想是:先判断是否存在通配符,如果不存在,简化为一般的匹配问题,这时,只要匹配上,则输出就是key的内容;如果存在通配符,则按照上述三种情况(低四种情况未实现)来分别完成。下面程序乱的地方在于,把通配符位于开头和结尾的情况全部融合在了最后一个else里面。
代码:
// Author:
// Date: 2014/7/17
// Description: This file deals with the test problems
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include <string>
using namespace std;
#define N 128
//Is there a star in the key string? Yes, return the position, else return -1.
int IsStar(string s)
{
int flag = -1 ;
for (int i = 0; i < s.size(); i ++)
{
if ('*' == s[i])
{
flag = i ;
break;
}
}
return flag ;
}
int main()
{
//=============2.match problem
// input
char dst_str[N] ;
string key ;
int ii = 0 ;
cin>>dst_str[ii] ;
while (',' != dst_str[ii]) //Note that the ',' in Chinese is different from that in English.
{
ii ++ ;
cin>>dst_str[ii] ;
}
cin>>key ;
int L1 = ii ;//The length of dst_str.
int L2 = key.size() ;
//int ans = IsStar(key) ;
//cout<<ans<<endl ;
if (-1 == IsStar(key)) // no star
{
int flag = 0 ;
for (int i = 0; i <= L1 - L2; i ++ )
{
int j = 0 ;
while ( j < L2 && dst_str[i + j] == key[j])//
{
j ++ ;
}
if (j == L2)
{
flag = 1 ;
break;
}
}
if (0 == flag)
{
cout<<"No match"<<endl ;
}
else
cout<<key<<endl ;
}
else if (1 == L2 && -1 != IsStar(key)) //The key string contains only '*'.
{
for (int i = 0; i < L1; i ++)
{
cout<<dst_str[i] ;
}
cout<<endl ;
}
else
{
int indx_start = 0, indx_stop = 0 ;
for (int i = 0; i <= L1; i ++ )
{
indx_start = i ;
int j = 0, indx = 0 ;
int flag = 0 ;
if (indx == IsStar(key)) //There is a star at position 0.
{
indx ++ ;
while (dst_str[i + j] != key[indx])
{
j++ ;
if (i+j == L1) //The 1st is *, but there is no match.
{
flag = 1 ;
indx_stop = indx_start -1 ;//The end is less than the start.
break;
}
}
}
while (dst_str[i + j] == key[indx] && flag == 0)//
{
if (indx != IsStar(key))
{
j ++ ;
indx ++ ;
}
if (indx == IsStar(key)) //There is a star at position indx.
{
indx ++ ;
if (L2 == indx)//The star is at the end of key.
{
flag = 1 ;
indx_stop = L1 ;
break;
}
else
{
while (dst_str[i + j] != key[indx])
{
j++ ;
}
}
}
if ( L2 == indx)
{
flag = 1 ;
indx_stop = i + j ;
break;
}
}
if (1 == flag) //found
{
break;
}
}
if (indx_stop < indx_start)
{
cout<<"No Match"<<endl ;
}
else
{
for (int i = indx_start; i < indx_stop; i ++)
{
cout<<dst_str[i] ;
}
cout<<endl ;
}
}
return 0 ;
}
下面这个也是这个问题,但是我还没有认真看。