華爲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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章