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 ;
}
下面這個也是這個問題,但是我還沒有認真看。