在网上看到的2013年华为上机题目
1. 字符串转换
1. 字符串转换
问题描述
将输入的字符串(字符串仅包含'a'-'z'),按照如下规则
1)循环转换后输出:a->b,b->c,…,y->z,z->a;
2)若输入的字符串连续出现两个字母相同时,后一个字母需要连续转换2次。例如:aa 转换为 bc,zz 转换为 ab;
3)当连续相同字母超过两个时,第三个出现的字母按第一次出现算。
要求实现函数:void convert(char *input, char* output)
示例
输入:char*input="abcd"
输出:char*output="bcde"
输入:char*input="abbbcd"
输出:char*output="bcdcde"
#include<stdio.h>
#define N 80
void Convert(char *input, char *output)
{
int i, flag = 0;
for(i = 0; input[i] != '\0'; i++)
{
output[i] = (input[i] - 'a' + 1) % 26 + 'a';
if(flag)
output[i]++;
if(input[i] == input[i+1] && flag == 0)
flag = 1;
else
flag = 0;
}
output[i] = '\0';
}
int main(void)
{
char input[N], output[N];
gets(input);//gcc中没有gets函数,改用fgets
Convert(input, output);
puts(output);//gcc中没有puts函数改用fputs
}
2.字符串处理
问题描述:
在给定字符串中找出单词(单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词);
在给定字符串中找出单词(单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词);
找到单词后,按照长度进行降序排序,(排序时如果长度相同,则按出现的顺序进行排列),然后输出到一个新的字符串中;
如果某个单词重复出现多次,则只输出一次;
如果整个输入的字符串中没有找到单词,请输出空串。输出的单词之间使用一个“空格”隔开,最后一个单词后不加空格。
要求实现函数:void my_word(char input[], char output[])
要求实现函数:void my_word(char input[], char output[])
示例
输入:char input[]="some local buses, some1234123drivers" ,
输出:char output[]="drivers local buses some"
输入:char input[]="%A^123 t 3453i*()" ,
输出:char output[]=""
输入:char input[]="some local buses, some1234123drivers" ,
输出:char output[]="drivers local buses some"
输入:char input[]="%A^123 t 3453i*()" ,
输出:char output[]=""
解题思路 :提取单词,对单词排序,将单词连接
1)提取单词时把分隔符同一替换为空格,并删掉多余的空格,方便后面对字符串进行分割
2)使用strtok函数对单词进行分割,并将单词的首地址保存到指针数组中(动态分配的数组)
3)根据单词的长度对数组进行排序
4)根据排序的结果去掉重复的单词(此处3、4两步可在一块处理)
5)将单词进行连接,以空格分割,并存入字符串中
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define N 255
struct word
{
char *str;
int len;
};
typedef struct word *myword;
//根据字符串的长度进行排序
void mysort(myword w, int n)
{
int i, j;
struct word tmp;
for(i = 1; i < n; i++)
{
tmp = w[i];
for(j = i; j > 0 && w[j - 1].len < tmp.len; j--)
w[j] = w[j - 1];
w[j] = tmp;
}
}
void my_word(char *input, char *output)
{
int i,j,count, len, nword;
char *p;
struct word *pw, tmp;
i = j = count = len = nword = 0;
output[0] = '\0';
//去除无效字符,并以空格分割
do
{
if(isalpha(input[i]))
count++;
else
{
if(count > 1)
{
nword++;
output[len++] = ' ';
output[len] = '\0';
strncat(output, input + i - count, count);
len += count;
}
count = 0;
}
}while(input[i++]);
strcpy(input, output);
if(nword)
{
//分割字符串,然后对每个单词进行处理
pw = (myword)malloc(sizeof(struct word) * nword);
p = strtok(input, " ");
while(p)
{
pw[j].str = p;
pw[j++].len = strlen(p);
*(p - 1) = '\0';
p = strtok(NULL, " ");
}
//对单词排序
mysort(pw, nword);
//去除重复单词
for(i = nword - 1; i >= 0; i--)
{
for(j = i - 1; j >= 0; j--)
if(pw[i].len != pw[j].len)
break;
else if(strcmp(pw[i].str,pw[j].str) == 0)
{
pw[i].str = NULL;
break;
}
}
//连接单词形成新的字符串
output[0] = '\0';
len = 0;
strcat(output, pw[0].str);
len += pw[0].len;
for(i = 1; i < nword; i++)
if(pw[i].str)
{
output[len++] = ' ';
output[len] = '\0';
strcat(output, pw[i].str);
len += pw[i].len;
}
free(pw);
}
else
output[0] = '\0';
}
int main(void)
{
char input[N], output[N];
fgets(input, N, stdin);
my_word(input, output);
fputs(output, stdout);
return 0;
}
3 正数减法
问题描述:
两个任意长度的正数相减,这两个正数可以带小数点,也可以是整数,请输出结果。
问题描述:
两个任意长度的正数相减,这两个正数可以带小数点,也可以是整数,请输出结果。
输入的字符串中,不会出现除了数字与小数点以外的其它字符,不会出现多个小数点以及小数点在第一个字符的位置等非法情况,所以考生的程序中无须考虑输入的数值字符串非法的情况。
详细要求以及约束:
1).输入均为正数,但输出可能为负数;
2).输入输出均为字符串形式;
3).如果输出是正数则不需要带符号,如果为负数,则输出的结果字符串需要带负号例如:2.2-1.1 直接输出为“1.1”,1.1-2.2 则需要输出为“-1.1”
4).输出的结果字符串需要过滤掉整数位前以及小数位后无效的0,小数位为全0的,直接输出整数位
例如:相减结果为11.345,此数值前后均不可以带0,“011.345”或者“0011.34500”等等前后带无效0的均视为错误输出。
详细要求以及约束:
1).输入均为正数,但输出可能为负数;
2).输入输出均为字符串形式;
3).如果输出是正数则不需要带符号,如果为负数,则输出的结果字符串需要带负号例如:2.2-1.1 直接输出为“1.1”,1.1-2.2 则需要输出为“-1.1”
4).输出的结果字符串需要过滤掉整数位前以及小数位后无效的0,小数位为全0的,直接输出整数位
例如:相减结果为11.345,此数值前后均不可以带0,“011.345”或者“0011.34500”等等前后带无效0的均视为错误输出。
例如:1.1-1.1结果为0.0,则直接输出0。
要求实现函数:void Decrease(char *input1, char*input2, char *output)//input1被减数,input2减数,output结果
示例
输入:char *input1="2.2"
char *input2="1.1"
输出:char*output="1.1"
输入:char *input1="1.1"
char *input2="2.2"
输出:char *output="-1.1"
要求实现函数:void Decrease(char *input1, char*input2, char *output)//input1被减数,input2减数,output结果
示例
输入:char *input1="2.2"
char *input2="1.1"
输出:char*output="1.1"
输入:char *input1="1.1"
char *input2="2.2"
输出:char *output="-1.1"
解题思路:
1)查找两个字符串中小数点的位置
2)以小数点为中心对齐小数部分和整数部分,并去除小数点
3)两个字符串相减,若被减数小于减数,则用减数减去被减数,并将结果的符号改为负号
4)去掉字符串首尾多余的0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void MyDecrease(char *minuend, char* subtrahend, char *remainder)
{
int mlen, slen, rlen, mdot, sdot, rdot, i, borrow, result;
//获取字符串长度,假设没有小数点
mdot = mlen = strlen(minuend);
sdot = slen = strlen(subtrahend);
//查找被减数中小数点位置
for(i = 0; i < mlen; i++)
if(minuend[i] == '.')
{
mdot = i;
break;
}
//去掉被减数中的小数点
if(mdot != mlen)
{
memmove(minuend + mdot, minuend + mdot + 1, mlen - mdot + 1);
mlen--;
}
//查找减数中小数点的位置
for(i = 0; i < slen; i++)
if(subtrahend[i] == '.')
{
sdot = i;
break;
}
//去掉减数中的小数点
if(sdot != slen)
{
memmove(subtrahend + sdot, subtrahend + sdot + 1, slen - sdot + 1);
slen--;
}
//对齐小数部分
result = (mlen-mdot) - (slen-sdot);
if(result > 0)
{
memset(subtrahend + slen, '0', result);
slen += result;
subtrahend[slen] = '\0';
}
else if (result < 0)
{
result = -result;
memset(minuend + mlen, '0', result);
mlen += result;
minuend[mlen] = '\0';
}
//对齐整数部分
if(mlen > slen)
{
memmove(subtrahend + mlen - slen, subtrahend, slen);
memset(subtrahend, '0', mlen - slen);
slen = mlen;
}
else if(mlen < slen)
{
memmove(minuend + slen - mlen, minuend, mlen);
memset(minuend, '0', slen - mlen);
mlen = slen;
}
rlen = mlen;
//对齐之后相减
if(strcmp(minuend, subtrahend) > 0)
{
for(i = rlen - 1, borrow = 0; i >= 0; i--)
{
result = minuend[i] - subtrahend[i] - borrow;
if(result < 0)
{
borrow = 1;
result += 10;
}
else
borrow = 0;
remainder[i] = result + '0';
}
remainder[rlen] = '\0';
}
else if(strcmp(minuend, subtrahend) < 0)
{
for(i = rlen - 1, borrow = 0; i >= 0; i--)
{
result = subtrahend[i] - minuend[i] - borrow;
if(result < 0)
{
borrow = 1;
result += 10;
}
else
borrow = 0;
remainder[i] = result + '0';
}
remainder[rlen] = '\0';
borrow = 1;
}
else
{
remainder[0] = '0';
remainder[1] = '\0';
return;
}
//添加小数点
rdot = mdot > sdot ? mdot : sdot;
memmove(remainder + rdot + 1, remainder + rdot, rlen - rdot + 1);
remainder[rdot] = '.';
rlen++;
//去掉字符串后面多余的0
for(i = rlen - 1; i >= 0; i--)
if(remainder[i] != '0')
break;
if(remainder[i] == '.')
i--;
remainder[i+1] = '\0';
rlen = i + 1;
//去掉字符串前面多余的0
result = 0;
for(i = 0; i < rlen; i++)
{
if(remainder[i] == '0' && remainder[i+1] != '.')
result++;
else
break;
}
rlen -= result;
memmove(remainder, remainder + result, rlen + 1);
//添加负号
if(borrow)
{
memmove(remainder + 1, remainder, rlen + 1);
remainder[0] = '-';
}
}
int main(void)
{
char str1[20], str2[20], output[20];
double a, b;
//随机数测试
srand(time(NULL));
a = (rand() % 65535) / (double)(rand() % 1000 + 1);
b = (rand() % 65535) / (double)(rand() % 50 + 1);
printf("%g - %g = %g\n",a,b, a-b);
sprintf(str1, "%g", a);
sprintf(str2, "%g", b);
MyDecrease(str1, str2, output);
puts(output);
return 0;
}