现在的位置: 首页 > 综合 > 正文

2013年华为机考参考代码8-29

2017年10月21日 ⁄ 综合 ⁄ 共 2401字 ⁄ 字号 评论关闭
/************************************************************************/
/* 
	2013-8-29华为机考 第三题,这个由于是最后一题,会比较难点;前面两题比较简单的
	给定两个字符串,设定每个字符的权值(1-26),字母大小写一样,求出两字符串的
	最大差值;
	思路:先去相同的字母,然后分别两个字符串map<char,int,greater<>>,
	长串是sum1,求出最大sum1和最小sum2,max=sum1-sum2就是答案了。
	author 2013-8-29-15:00
*/
/************************************************************************/
#include <iostream>
#include <map>
#include <set>
#include <iterator>
using namespace std;
char s1[300];
char s2[300];
void Max_large_diff(char *s1,char *s2,int len2)
{
	int i,j,k;
	char temp;
	int sum1,sum2;
	int sum=0;
	int finish=0;
	map<char,int,greater<int>>map_char1;
	map<char,int,greater<int>>map_char2;
	map<char,int,greater<int>>::iterator it;
	sum1=0;
	sum2=0;
	for(i=0;s1[i]!=0;i++)
		{
			temp =s1[i];
			for(j=0;s2[j]!=0 && finish!=len2;j++)
			{
				if(tolower(temp) == tolower(s2[j]))
				{
					s1[i]=-1;
					s2[j]=-1;
					finish++;
					break;
				}
			}
		}
	for (i =0;s1[i]!=0;i++)
		if(s1[i]!=-1)
				map_char1[s1[i]]++;
	for(i =0;s2[i]!=0;i++)
		if(s2[i]!=-1)
				map_char2[s2[i]]++;
	it = map_char1.begin();
	k=26;
	for(;it!=map_char1.end();it++)
	{
		sum1 = sum1 + (*it).second *k ;
		k--;
	}
	k = 1;
	it = map_char2.begin();
	for(;it!=map_char2.end();it++)
	{
		sum2 = sum2 + (*it).second *k;
		k++;
	}
	sum = sum1 - sum2;
	printf("%d\n",sum);
}
int main()
{
	while(scanf("%s",s1)!=EOF)
	{
		scanf("%s",s2);
		int n1= strlen(s1);
		int n2 = strlen(s2);
		if(n1 >= n2)
		{
			Max_large_diff(s1,s2,n2);
		}
		else
	<pre name="code" class="cpp">/************************************************************************/
/* 
	2013-8-29华为机考 第二题,一堆人按顺序报数,报到N,就出列,继续,问最后一个出列的是多少?直接模拟游戏,得出结果!
	author 2013-8-29-15:00
*/
/************************************************************************/
#include <iostream>
using namespace std;
int main()
{
	int s[100];
	int N,i,j;
	int number;
	int last_N;
	int count_N;
	while(scanf("%d",&N)!=EOF)
	{
		memset(s,0,sizeof(s));
		count_N =0;
		j = 0;
		if(N==1)
		{
			printf("1\n");
			continue;
		}
		while(1)
		{
			count_N++;
			j++;
			if(j==(N+1))
				j=1;
			while(s[j]!=0)
			{
				j++;
				if(j==(N+1))
					j=1;
			}
			if(count_N %3==0)
			{
				s[j] = 1;
				number = 0;
				for(int i=1;i<=N;i++)
				{
					if(s[i]==0)
					{
						number++;
						last_N = i;
					}
				}
				if(number ==1)
				{
					printf("%d\n",last_N);
					break;
				}
				count_N=0;
			}
			
		}
	}
	return 0;
}

{Max_large_diff(s2,s1,n1);}}return 0;}


/************************************************************************/
/* 
	2013-8-29华为机考 第一题,字符串的大小写转化,相当简单!
	author 2013-8-29-15:00
*/
/************************************************************************/
#include <iostream>
using namespace std;
int main()
{
	char s[1000];
	int n;
	while(scanf("%s",s)!=EOF)
	{
		for(n =0;s[n]!=0;n++)
		{
			if(s[n]>='a' && s[n]<='z')
				s[n] = s[n] - ('a'-'A');
			else if(s[n]>='A' && s[n]<='Z')
				s[n] = s[n] + ('a'-'A');
		}
		printf("%s\n",s);
	}
	return 0;
}

抱歉!评论已关闭.