华为机考(待续)

1.输入一串字符,只包含“0-10”和“,”找出其中最小的数字和最大的数字(可能不止一个),输出最后剩余数字个数。
如 输入  “3,3,4,5,6,7,7”

     输出   3

public static int findLeft(String input)
	{
		String nums[] = input.split(",");
		int size = nums.length;
		int array[] = new int[size];
		for(int i = 0; i < size; i++)
		{
			array[i] = Integer.parseInt(nums[i]);
		}
		Arrays.sort(array);
		int min = array[0];
		int max = array[size - 1];
		int result = size;
		for(int i = 0; i < size; i++)
		{
			if(array[i] == min)
			{
				result--;
			}
			if(array[i] == max)
			{
				result--;
			}
		}
		return result;
	}


2. 输入一组身高在170到190之间(5个身高),比较身高差,选出身高差最小的两个身高;若身高差相同,选平均身高高的那两个身高;从小到大输出;
如 输入   170 181 173 186 190
     输出   170 173

	public static void findHeight(int[] a)
	{
		Arrays.sort(a);
		int d = a[1] - a[0];
		int h1 = a[0];
		int h2 = a[1];
		for(int i = 2; i < a.length; i++)
		{
			if(a[i] - a[i-1] <= d)
			{
				h1 = a[i-1];
				h2 = a[i];
			}
		}
		System.out.println(h1 + " " + h2 );
	}

3. 一组人(n个),围成一圈,从某人开始数到第三个的人出列,再接着从下一个人开始数,最终输出最终出列的人

//f[i] = 0;
	//f[i] = (f[i-1]+M) % i;
	public static void Josephus(int N, int M)
	{
		int f[] = new int[100];
		f[1] = 0;
		for(int i = 2; i <= N; i++)
		{
			f[i] = (f[i - 1] + M) % i;
		}
		System.out.print((f[N]+1)+" ");
	}

4.一个字符串,如aAbas__a;要求,过滤为每个字符只出现一次:aAbs_;

#include <stdlib.h>
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	int mapFirst[255] = {0};
	int i;
	char *p = pInputStr;
	char *q = pInputStr;

	while(*p != '\0')
	{
		 if(mapFirst[*p] == 0)
		 {
		 	mapFirst[*p] = 1;
 			*q++ = *p++;
 		 }
 		 else
 		 {
 		 	*p++;
 		 }
	}
	*q = '\0';
	printf("%s",pInputStr);
}

int main()
{
	char t[] = "pppppppp";
	int len = strlen(t);
	printf("%d \n",len);
	char *out;
	stringFilter(t,len,out);
}

5. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
压缩规则:
1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".
2. 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"


void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	char *p = pInputStr;
	char *q = pInputStr;
	int map[255] = {0};
	int i;
	for(i = 0; i < lInputLen; i++)
	{
		if(map[pInputStr[i]] == 0)
		{
			map[pInputStr[i]] = 1;
		}
		else
		{
			map[pInputStr[i]]++;
		}	 
	}
	while(*p != '\0')
	{	 
		if(map[*p] > 1 && map[*p] < 10 )
		{
		
			char temp = *p;		
			int count = map[*p];
			*q++ = map[*p] + '0';
			while(count--)
			{
		 		p++;
			}
			*q++ = temp;	
		}
 		else if(map[*p] >= 10 )
	    {
			char str[20]={'0'};			
			int num1 = map[*p];
			int num2 = map[*p];
			int i = 0;	
			char temp = *p;	
			while(num2)
			{
				str[i++] = num2 % 10 +'0';
				num2 = num2 / 10; 
			
			}
			while(i--)
			{
				*q++ = str[i];		
			}

			*q++ = temp;
				
			while(num1--)
			{
				p++;
			}
			
 		} 
		else
		{
			*q++ = *p++;
		}
	}
	*q = '\0';
}

这道题实现的比较粗糙,有个需要注意到的地方是重复的字符次数可能大于10,由于避免了库函数所以代码不够简洁。


6. 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。

void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	int a = 0, b = 0;
	char operator;
	char *p = pInputStr;
	int i = 0;
	int j = 0; 
	while(*p != ' ')
	{
		a = a * 10 + *p - '0';
		p++;
	}
	p++;
	operator = *p++;
	if(*p++ != ' ')
	{
		pOutputStr[0] = 0+'0';
		pOutputStr[1] = '\0';
		return;
	}
 
	while(*p != '\0')
	{
		b = b * 10 + *p - '0';
		p++;
	}
	if(operator == '+')
	{
		sprintf(pOutputStr, "%d", a+b); 
	}
	else
	{
		sprintf(pOutputStr, "%d", a-b); 
	}
}

还是用到了一个库函数,sprintf,功能很强大。





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值