华为OJ训练题(一)

1、字符串最后一个单词长度

描述 计算字符串最后一个单词的长度,单词以空格隔开。
知识点 字符串,循环
运行时间限制 0M
内存限制 0
输入 一行字符串,长度小于128。
输出 整数N,最后一个单词的长度。
样例输入 hello world
样例输出 5

C++代码实现:

#include<iostream>
#include<string>
using namespace std;
void main()
{
	string A;
    getline(cin, A);
    int len = A.length();
    int ans = 0;
    while(A[len-1]!=' ' && len){
        len--;
        ans++;
    }
    cout<<ans<<endl;
   
}

2、名字的漂亮度

描述:
     给出一个名字,该名字有26个字符串组成,定义这个字符串的“漂亮度”是其所有字母“漂亮度”的总和。
     每个字母都有一个“漂亮度”,范围在1到26之间。没有任何两个字母拥有相同的“漂亮度”。字母忽略大小写。
     给出多个名字,计算每个名字最大可能的“漂亮度”。
知识点:
字符串
运行时间限制 0M
内存限制 0
输入:
整数N,后续N个名字
N个字符串,每个表示一个名字


输出:
每个名称可能的最大漂亮程度
样例输入
                2 
                zhangsan 
lisi
样例输出
                 192 
                 101

C++代码实现:

#include <iostream>
#include <string>
#include <algorithm>   //提供排序函数
#include <ctype.h>
using namespace std;

int Beauty(char a[])
{
	int b[26]={0};  //存储每个字符的个数
	int sum=0;
	for(int i=0;a[i]!='\0';i++)
	{
		a[i]=tolower(a[i]);  //全部先转换成小写
	}
	for(int j=0; a[j]!='\0'; j++)
	{
		b[a[j]-97]++;
	}
	sort(b,b+26);
	for(int k=25; k>=0; k--)
	{
		sum+=(k+1)*b[k];
	}
	return sum;
}

int main()
{   
	int M;
	char array[100][100];
	cin>>M;
	getchar(); //清除回车
	for(int i=0;i<M;i++)
		gets(array[i]);
	for(int m=0;m<M;m++)
		cout<<Beauty(array[m])<<endl; 
	return 0;
}


3、字符逆序

描述
将一个字符串str的内容颠倒过来,并输出。str的长度不超过100个字符。 如:输入“I am a student”,输出“tneduts a ma I”。
输入参数:
inputString:输入的字符串


返回值:
输出转换好的逆序字符串
 
知识点 字符串
运行时间限制 10M
内存限制 128
输入
输入一个字符串,可以有空格
输出
输出逆序的字符串
样例输入 I am a student
样例输出 tneduts a ma I

C++代码实现:

#include <iostream>
#include <string>
using namespace std;

void main()
{
	string A;
	string B;
	getline(cin, A);
	int length=A.length();
	getchar();
	for(int k=length-1;k>=0;k--)
	{
		cout<<A[k];
	}
}

4、公共字符串计算

描述
题目标题:
计算两个字符串的最大公共字串的长度,字符不区分大小写
详细描述:
接口说明
原型:
int getCommonStrLength(char * pFirstStr, char * pSecondStr);
输入参数:
     char * pFirstStr //第一个字符串
     char * pSecondStr//第二个字符串
 
知识点 字符串,查找
运行时间限制 10M
内存限制 128
输入
输入两个字符串
输出
输出一个整数
样例输入 asdfas werasdfaswer
样例输出 6

C++代码实现:

#include <iostream>
#include <string>
#include <algorithm>
#include <ctype.h>
#include <strstream>
using namespace std;

int getCommonStrLength(char pFirstStr[], char pSecondStr[])
{
	int start1,start2;
	int count=0,Max=0;
    for(int i=0;pFirstStr[i]!='\0';i++)
	{
		pFirstStr[i]=tolower(pFirstStr[i]);  //全部先转换成小写
	}
	for(int j=0;pSecondStr[j]!='\0';j++)
	{
		pSecondStr[j]=tolower(pSecondStr[j]);  //全部先转换成小写
	}

	for(int k=0;pFirstStr[k]!='\0';k++)
		for(int l=0;pSecondStr[l]!='\0';l++)
		{
			start1=k;start2=l;
			while(pFirstStr[start1]==pSecondStr[start2] && start1<strlen(pFirstStr) && start2<strlen(pSecondStr))
			{
				start1++;
				start2++;
				count++;
			}
			if(count>Max)
				Max=count;
			count=0;
		}
		return Max;
}

void main()
{ 
   /*
   char pFirstStr[100];
   char pSecondStr[100];
   gets(pFirstStr);
   gets(pSecondStr);
   */
   char hebing[100];
   gets(hebing);
   char pFirstStr[100];
   char pSecondStr[100];
   istrstream strin(hebing); //定义一个字符串输入流对象,并指定s为输入字符串
   strin >>pFirstStr >>pSecondStr; //从s中读取字符串
   cout<<getCommonStrLength(pFirstStr,pSecondStr);

}

5、字符串反转

描述
写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串。例如:
知识点 数组,指针
运行时间限制 10M
内存限制 128
输入
输入N个字符
输出
输出该字符串反转后的字符串
样例输入 abcd
样例输出 dcba

C++代码实现:

#include <iostream>
#include <string>
#include <algorithm>
#include <ctype.h>
using namespace std;

void reverse(string str)
{
	int len=str.size();
	for(int i=len-1;i>=0;i--)
	{
        cout<<str[i];
	}
}

void main()
{
	string A;
	getline(cin,A);
	reverse(A);
}

6、统计大写字母个数

描述
找出给定字符串中大写字符(即'A'-'Z')的个数
接口说明
    原型:int CalcCapital(String str);
    返回值:int
 
 
知识点 字符串
运行时间限制 10M
内存限制 128
输入
输入一个String数据
输出
输出string中大写字母的个数
需考虑字符串为空  如果为空输出0
样例输入 add123#$%#%#A
样例输出 1

C++代码实现:

#include <iostream>
#include <string>
#include <algorithm>
#include <ctype.h>
using namespace std;

int CalcCapital(string str)
{
   int len=str.size();  //求字符串长度  or  str.length()
   int count=0;
   if(str.empty())      //判断字符串是否为空
   {
	   return 0;
   }
   else
   {
     for(int i=0;str[i]!='\0';i++)
	 {
		 if(str[i]<=90 && str[i]>=65)
			 count++;
	 }
   }
   return count;
}

void main()
{
   string A;
   getline(cin,A);
   int result=CalcCapital(A);
   cout<<result;
}

7、挑7

描述
输出7有关数字的个数,包括7的倍数,还有包含7的数字(如17,27,37...70,71,72,73...)的个数
知识点 循环
运行时间限制 0M
内存限制 0
输入
一个正整数N。(N不大于30000)
输出
不大于N的与7有关的数字个数,例如输入20,与7有关的数字包括7,14,17.
样例输入 20
样例输出 3


C++代码实现:

#include <iostream>
#include <string>
#include <algorithm>
#include <ctype.h>
using namespace std;

void main()
{
	int N;
	cin>>N;
	int count=0;
	for(int i=1;i<=N;i++)
	{
       if(i%7==0)
	   {
		   count++;
	   }
	   else
	   {
		   int temp=i;
		   while(temp>0)
		   {
			   if(temp%10==7)
			   {
				   count++;
				   break;
			   }
			   temp=temp/10;
		   }
	   }
	}
	cout<<count;
}


8、找出字符串中第一个出现的字符

描述
找出字符串中第一个只出现一次的字符
详细描述:
接口说明
原型:
bool FindChar(char* pInputString, char* pChar);
输入参数:
char* pInputString:字符串
输出参数(指针指向的内存区域保证有效):
char* pChar:第一个只出现一次的字符
如果无此字符 请输出'.'
 
知识点 字符串,循环
运行时间限制 10M
内存限制 128
输入
输入一串字符
输出
输出一个字符
样例输入 asdfasdfo
样例输出 o

C++代码实现:

#include <iostream>
#include <string>
#include <algorithm>
#include <ctype.h>
using namespace std;

char find(string inputChar)
{
   int len=inputChar.size();

   for(int i=0;i<len;i++)
   { 
	   int flag=0;   //标识位,0表示这个字符只出现一次。
	   for(int j=i+1;j<len;j++)
	   {
            if(inputChar[i]==inputChar[j])
			{
				flag=1;   //1表示在当前字符后面存在于该字符相同的字符。
			}
	   }
	   if(flag==0)
		   return inputChar[i];
	   else 
		   return '.';
   }

   
}

void main()
{
   string M;
   getline(cin,M);
   char s=find(M);
   cout<<s;
}


发布了44 篇原创文章 · 获赞 1 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章