华为校园招聘上机训练

题目描述

计算字符串最后一个单词的长度,单词以空格隔开。


输入描述:
 
 

一行字符串,非空,长度小于5000。

输出描述:
 
 

整数N,最后一个单词的长度。


输入例子:
hello world

输出例子:
5
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main ()
{
	//string s;
	char s[5000];
	//while(cin>>s)
	//while(cin.getline(s,256))
	while(gets(s))
		 //while(getline(cin,s))
	{
		unsigned int temp=0,n=0,i;
		//char a[5000];
		//n=s.size();
		for(n=0;s[n]!='\0';n++);

		//n=sizeof(s);
		//n=strlen(s);
		for(i=0;i<n;i++)
		{
			if(s[i]==' ')
				temp=i;
			
		}
        if(temp!=0)
				temp++;
		cout<<(n-temp)<<endl;
	}
	return 0;
}
	

题目描述

写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。


输入描述:
 
 

输入一个有字母和数字以及空格组成的字符串,和一个字符。

输出描述:
 
 

输出输入字符串中含有该字符的个数。


输入例子:
ABCDEF
A

输出例子:
1
 
 
#include <iostream>
//#include <string>
using namespace std;
int main()
{
	//char a[100];
    string a;
	char s;
	//while(gets(a))
	while(cin>>a)
	{
		//gets(&s);
		cin>>s;
		int n=0,m=0;
		for(n=0;a[n]!='\0';n++);
        if(s>='a'&&s<='z')
                s=s-32;   // 小写 65-90
		for(int i=0;i<n;i++)
		{
            if(a[i]>='a'&&a[i]<='z')
                a[i]=a[i]-32;
			if(s==a[i])
				m++;
		}
		cout<<m<<endl;
	}
	return 0;
}

题目描述

•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。


输入描述:
 
  

连续输入字符串(输入2次,每个字符串长度小于100)

输出描述:
 
  

输出到长度为8的新字符串数组


输入例子:
abc
123456789

输出例子:
abc00000
12345678
90000000
#include <iostream>
using namespace std;
int main()
{
	char a[100];
	char b[100];
	while(gets(a))
	{
		gets(b);
		int m=0,n=0;
		for(;a[m]!='\0';m++);
		for(;b[n]!='\0';n++);
		int i;
		for(i=0;i<m;i++)
		{
			//for(;((i)%8!=0)&&i<m;i++)
			{
				cout<<a[i];
			}
			if((i+1)%8==0)
			cout<<endl;
		}
		if((i==m)&&(i%8)!=0)
		{
			for(int q=0;q<(8-m%8);q++)
				cout<<0;cout<<endl;
		}
		for(i=0;i<n;i++)
		{
			//for(;((i)%8!=0)&&i<m;i++)
			{
				cout<<b[i];
			}
			if((i+1)%8==0)
				cout<<endl;
		}
		if((i==n)&&(i%8)!=0)
		{
			for(int q=0;q<(8-n%8);q++)
				cout<<0;cout<<endl;
		}
	}
	return 0;
}


题目描述

功能:输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 )

最后一个数后面也要有空格

详细描述:


函数接口说明:

public String getResult(long ulDataInput)

输入参数:

long ulDataInput:输入的正整数

返回值:

String




输入描述:
 
 

输入一个long型整数

输出描述:
 
 

按照从小到大的顺序输出它的所有质数的因子,以空格隔开。最后一个数后面也要有空格。


输入例子:
180

输出例子:
2 2 3 3 5
#include <iostream>
using namespace std;
int main ()
{
    long t;
	while(cin>>t)
	{
		for(long i=2;i<=t;i++)
		{
			if((t%i)==0)
			{
				int m=0;
				for(long q=2;q<i;q++)
				{
					if((i%q)==0)
					   m++;	
				}
				if(m==0)
					{
						cout<<i<<' ';
						t=t/i;
						i--;
				    }
			}
		}
		cout<<endl;
	}
	return 0;
}

题目描述

写出一个程序,接受一个正浮点数值,输出该数值的近似整数值。如果小数点后数值大于等于5,向上取整;小于5,则向下取整。


输入描述:
 
 

输入一个正浮点数值

输出描述:
 
 

输出该数值的近似整数值


输入例子:
5.5

输出例子:
6
#include <iostream>
using namespace std;
int main()
{
	float a;
	while(cin>>a)
	{
		unsigned int b = (int)a;
		if((a-b)>=0.5)
			b=b+1;
		cout<<b<<endl;
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值