2015届华为校园招聘机试题
第一题(60分):
按要求分解字符串,输入两个数M,N;M代表输入的M串字符串,N代表输出的每串字符串的位数,不够补0。例如:输入2,8, “abc” ,“123456789”,则输出为“abc00000”,“12345678“,”90000000”
#include
#include
using namespace std;
void solve(char *str , int n , int len)
{
?int i , j , k , quotient , remainder;
?quotient = len / n;? ? ? ? ? ? ? ? //原字符串被分解的个数
?remainder = len - n * quotient;? ? //剩余的字符串的个数
?for(i = 0 ; i < len ; i += n)
?{
? if(len - i < n)
? {
? ? k = n - len + i;
? ? for(j = i ; j < len ; ++j)
? ? printf("%c" , str[j]);
? ? for(j = 0 ; j < k ; ++j)
? ? putchar('0');
? }
? else
? {
? ?for(j = i ; j < i + n ; ++j)
? ? printf("%c" , str[j]);
? }
? putchar(' ');
?}
?printf("\n");
}
int main(void)
{
?int i , m , n , len;
?char str[1000];
?while(scanf("%d %d", &m , &n) != EOF)
?{
? for(i = 0 ; i < m ; ++i)
? {
? ?scanf("%s" , str);
? ?len = strlen(str);
? ?solve(str , n , len);
? }
?}
?return 0;
}
第一题:拼音转数字
输入是一个只包含拼音的字符串,请输出对应的数字序列。转换关系如下:
描述:? ? ? 拼音? ? ? ? yi? er? san? si? wu? liu? qi? ba? jiu
? ? ? 阿拉伯数字? ? ? ? 1? 2? 3? ? ? 4? 5? ? 6? ? 7? 8? 9
输入字符只包含小写字母,所有字符都可以正好匹配
运行时间限制:无限制
内存限制:? ? ? 无限制
输入:? ? ? ? ? ? ? 一行字符串,长度小于1000
输出:? ? ? ? ? ? ? 一行字符(数字)串
样例输入:? ? ? yiersansi
样例输出:? ? ? 1234
#include
#include
using namespace std;
void solve(char *str , int len)
{
?int i;
?for(i = 0 ; i < len ; )
?{
? switch(str[i])
? {
? case 'y':
? ?putchar('1');
? ?i += 2;
? ?break;
? case 'e':
? ?putchar('2');
? ?i += 2;
? ?break;
? case 's':
? ?if(str[i + 1] == 'a')
? ?{
? ? putchar('3');
? ? i += 3;
? ?}
? ?else
? ?{
? ? putchar('4');
? ? i += 2;
? ?}
? ?break;
? case 'w':
? ?putchar('5');
? ?i += 2;
? ?break;
? case 'l':
? ?putchar('6');
? ?i += 3;
? ?break;
? case 'q':
? ?putchar('7');
? ?i += 2;
? ?break;
? case 'b':
? ?putchar('8');
? ?i += 2;
? ?break;
? case 'j':
? ?putchar('9');
? ?i += 3;
? ?break;
? }
?}
?printf("\n");
}
int main(void)
{
?int len;
?char str[1000];
?while(scanf("%s" , str) != EOF)
?{
? len = strlen(str);
? solve(str , len);
?}
?return 0;
}
第二题:去除重复字符并排序
运行时间限制:无限制
内容限制:? ? ? 无限制
输入:? ? ? ? ? ? ? 字符串
输出:? ? ? ? ? ? ? 去除重复字符并排序的字符串
样例输入:? ? ? aabcdefff
样例输出:? ? ? abcdef
#include
#include
#include
using namespace std;
void solve(char *str , int len)
{
?int i , hash[256];
?memset(hash , 0 , sizeof(hash));
?for(i = 0 ; i < len ; ++i)
?{
? if(0 == hash[str[i]])
? ?hash[str[i]] = 1;
?}
?for(i = 0 ; i < 256 ; ++i)
?{
? if(0 != hash[i])
? ?putchar(i);
?}
?printf("\n");
}
int main(void)
{
?int len;
?char str[1000];
?while(scanf("%s" , str) != EOF)
?{
? len = strlen(str);
? solve(str , len);
?}
?return 0;
}
第三题:等式变换
输入一个正整数X,在下面的等式左边的数字之间添加+号或者-号,使得等式成立。
1 2 3 4 5 6 7 8 9 = X
比如:
12-34+5-67+89 = 5
1+23+4-5+6-7-8-9 = 5
请编写程序,统计满足输入整数的所有整数个数。
输入:? ? ? 正整数,等式右边的数字
输出:? ? ? 使该等式成立的个数
样例输入:5
样例输出:21
#include
#include
using namespace std;
int ops[21];
const char sym[3] = {'+' , '-' , ' '};
int result , num;
void dfs(int layer, int currentResult, int lastOp, int lastSum)
{
?lastSum *= (layer > 9) ? 100 : 10;
?lastSum += layer;
?if(layer == 9)
?{
? currentResult += (lastOp) ? (-1 * lastSum) : lastSum;
? if(currentResult == result)
? {
? ?++num;
? ?printf("1");
? ?for(int i = 2 ; i <= 9 ; ++i)
? ?{
? ? if(sym[ops[i-1]] != ' ')
? ? ?printf(" %c ", sym[ops[i-1]]);
? ? printf("%d", i);
? ?}
? ?printf(" = %d\n" , result);
? }
? return;
?}
?ops[layer] = 2;
?dfs(layer + 1 , currentResult , lastOp , lastSum);? //Continue
?currentResult += (lastOp)? (-1 * lastSum) : lastSum;
?ops[layer] = 0;
?dfs(layer + 1 , currentResult , 0 , 0);? //Plus
?ops[layer] = 1;
?dfs(layer + 1 , currentResult , 1 , 0);? //Minus
}
int main(void)
{
?while(scanf("%d", &result) != EOF)
?{
? num = 0;
? dfs(1 , 0 , 0 , 0);
? printf("%d\n" , num);
?}
?return 0;
}