华为机试-1

牛客网上华为初试的编程题
语言:C++ C

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

输入 hello world
输出 5

#include<iostream>
using namespace std;

#include<string>
int main()
{
    string s;
    int j=0;  
    getline(cin,s);//输入字符串
    for(int i=s.length()-1;i>=0;i--)//从字符的最后开始往前数,直到空格键
    {
        if(s[i]!=' ')
            j++;
        else
            break;
    }
    cout << j <<endl;
    return 0;
}
  • scanf()函数
int str[5000];
scanf("%s",str);

从键盘输入,读取除空白以外的所有字符串。scanf()函数跳过空白开始读取第一个非空白字符,并保存非空白字符直到再次遇到空白字符。空格hello空格只读取hello

  • scanf()在读取数字时会跳过空格、制表符和换行符!
  • %c只能输出或输入一个字符,%s输出的是一串字符。还有就是char a;string s;输入的时候scanf("%c",&a);这里的&不能少,而scanf("%s",s);这里不能有&符号
  • scanf("%c",&s);会吃掉回车符,和空格 解决办法:scanf(" %c",&s);加个空格
#include <stdio.h>
#include <string.h>

int main()
{
    char str[1000];
    int a=0,i=0;

    while(scanf("%s",str) != EOF)
	{}
   
    a=strlen(str);//此时的str存放最后一串单词
    printf("%d",a);
	
	return 0;
}

在输入时文件结束的标志:先按enter,再按ctrl+z,最后再按enter。(window)

#include <stdio.h>
#include <string.h>
int main()
{
    char str[5000]={0};
	int count=0;
	gets(str);
	int len=strlen(str)-1;
	while(str[len]!=' '&&len>=0)
	{
		count++;
		len--;
	}
	printf("%d\n",count);
	return 0;
}

2、计算字符个数

输入ABCDEF A
输出 1

#include<iostream>
using namespace std;

#include<string>
int main()
{
	string s;
	getline(cin,s);
	char c;
	cin >> c;
	int count=0;
	for(int i=0;i<s.length();i++)
	{
		if(s.at(i)==toupper(c)|| s.at(i)==tolower(c))
		count++;
	}
	cout << count;
	return 0;
}
  • toupper() :字符都转化成大写
  • tolower() : 字符都转化成小写
  • s.at(n) : 返回下标为n的字符
#include <string.h>
#include<stdio.h>
#include<ctype.h>//含tolower() toupper()函数
int main(void)
{
    char str[1024]={0},c='\0';
	fgets(str,sizeof(str),stdin);
	scanf("%c",&c);//这里输入的单个字符,%c
	int count=0;
	for(int i=0;i<strlen(str);i++)
	{
		if(str[i]==tolower(c)||str[i]==toupper(c))
			count++;
	}
	printf("%d",count);
	return 0;
}

3、明明的随机数

输入 5 2 3 2 5 1
输出 1 2 3 5

输入的数 0~1000

#include<iostream>
using namespace std;

#include<string>
int main()
{
	int N,n;
	while(cin >> N)
	{
		int arr[1001]={0};
		
		while(N--)
		{
			cin>>n;
			a[n]=1;
		}
		for(int i=0;i<1001;i++)
		{
		if(a[i])
		cout << i<<endl;
		}
	}
	return 0;
}

c语言里的头文件。
在这里插入图片描述
while(~scanf("%d", &n))就是当没有输入的时候退出循环

while(scanf("%d",&n)!=EOF)一个意思

  • 知识1:只有-1取反(~-1)是0
  • 知识2:scanf读入到EOF时返回-1
#include<stdio.h>
int  main()
{
    int N=0,n=0;
    while(~scanf("%d",&N))//还可以写成  while(scanf("%d",&n)!=EOF)
    {
         int arr[10001]={0};
        for(int k=0;k<N;k++)
        {
            scanf("%d",&n);
            arr[n]=1;
        }
        for(int i=0;i<1001;i++)
        {
            if(arr[i]==1)
                printf("%d\n",i);
        }
    }
    return 0;
}

4、字符串分隔

输入 abc 123456789
输出 abc00000 12345678 90000000

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
	char arr[1000]={0};
	scanf("%s",arr);
	char *p=arr;
	//如果p不等于'\0'
	int count=0;
	while(*p!='\0')
	{	
		printf("%c",*p);
		count++;
		p++;
		if(*p=='\0')
			break;
		if(count==8){

			printf("\n");
			count=0;
		}
	}	
	if(*p=='\0'&&count!=0)
		{
			for(count;count<8;count++)
				printf("0");
			printf("\n");
		}
	return 0;
}

51输出单向链表中倒数第k个结点

输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第1个结点为链表的尾指针。

链表结点定义如下:

struct ListNode

{

int m_nKey;

ListNode* m_pNext;

};

正常返回倒数第k个结点指针,异常返回空指针

输入:
8
1 2 3 4 5 6 7 8
4
输出:
5

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct LNode{
	int data;
	struct LNode *next;
} LNode;

int main()
{
	int n;
	//scanf("%d", &n);
	while(scanf("%d", &n) != EOF)
	{
		int nums[10001];
		int k = 0;
		LNode *head = (LNode *)malloc(sizeof(LNode));
        head->next = NULL;
		LNode *p=head;
		for(int i=0; i < n; i++)
		{
			LNode *temp = (LNode *)malloc(sizeof(LNode));
			scanf("%d",&nums[i]);
			temp->data =nums[i];
			temp->next=NULL;
			p->next=temp;
			p=temp ;
		}
		LNode *tail = (LNode *)malloc(sizeof(LNode));
		tail->data=0;
		tail->next=NULL;
		p->next =tail;
		scanf("%d", &k);
        for(int i=0; i < n-k+1; i++)
		{
			head = head->next;
		}
		printf("%d\n", head->data);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
108题中有部分题目重合,因此么有收录在压缩文件中。 华为机试 ├─001 字符串最后一个单词长度 │ └─Source ├─002 计算字符个数 │ └─Source ├─003 明明的随机数 │ └─Source ├─004 字符串分隔 │ └─Source ├─005 进制转换 │ └─Source ├─006 质数因子 │ └─Source ├─007 取近似值 │ └─Source ├─008 合并表记录 │ └─Source ├─009 提取不重复的整数 │ └─Source ├─010 字符个数统计 │ └─Source ├─011 数字颠倒 │ └─Source ├─012 字符串反转 │ └─Source ├─013 句子逆序 │ └─Source ├─014 字典序排序 │ └─Source ├─015 求int型正整数在内存中存储是1的个数 │ └─Source ├─016 购物单 │ ├─Debug │ ├─Source │ │ └─Debug │ ├─Source - 时间优先 │ │ └─Debug │ └─Source - 空间优先 │ ├─Debug │ └─Release ├─017 坐标移动 ├─018 识别IP地址分类统计 │ └─Source │ └─Debug ├─019 错误记录 ├─020 密码验证合格程序 ├─021 密码破解 ├─023 删除字符串中出现次数最少字符 │ └─Source │ └─Debug ├─024 合唱队 │ └─Source │ ├─Debug │ └─Release ├─025 数据分类处理 │ └─Source │ └─Debug ├─026 查找兄弟单词 │ └─Source │ └─Debug ├─027 素数伴侣 │ └─Source │ └─Debug ├─028 字符串合并处理 │ └─Source │ └─Debug ├─030 密码截取(查找最长回文字符串) ├─031 蛇形矩阵 │ └─Source │ └─Debug ├─033 判断IP是否属于同一子网 │ └─Source │ └─Debug ├─034 称砝码 │ └─Source │ └─Debug ├─035 学英语 │ └─Source │ └─Debug ├─036 迷宫问题 │ └─Source │ └─Debug ├─037 数独问题 │ └─Debug ├─038 名字漂亮度 │ └─Source │ └─Debug ├─039 字符串截取 │ └─Source │ └─Debug ├─040 单链表删除数据 │ └─Source │ └─Debug ├─041 多线程 │ └─Source │ ├─Backup │ ├─Debug │ │ └─041.tlog │ └─Release │ └─041.tlog ├─042 表达式计算 │ └─Source │ └─Debug ├─043 计算字符串距离 │ └─Source │ └─Debug ├─044 杨辉三角形变形 ├─046 挑7 ├─047 完全数 │ └─Debug ├─048 高精度加法 ├─049 输出n个数中最小的k个 │ └─Debug ├─050 找出字符串只出现一次的字符 │ └─Debug ├─051 组成一个偶数最接近的2个质数 │ └─Debug ├─052 M个苹果放入N个盘子 ├─053 查找整数二进制中1的个数 ├─054 DNA子串 ├─055 MP3光标位置 │ └─Source │ └─Debug ├─056 查找2个字符串最大相同子串 │ └─Debug ├─057 配置文件恢复 │ └─Source │ └─Debug ├─058 24点计算 │ └─Debug ├─059 成绩排序 ├─060 矩阵相乘 ├─061 矩阵乘法次数计算 ├─062 字符串通配符 │ └─Debug ├─066 命令行解析 │ └─Source │ └─Debug ├─067 最大相同子串长度 │ └─Debug ├─068 火车编号进站 │ └─Debug ├─072 数组合并 ├─074 埃及分数 │ └─Source │ └─Debug ├─076 密码截取 │ └─Source ├─077 求最大连续bit数 ├─078 密码强度 ├─079 扑克牌大小 │ └─Source │ └─Debug ├─081 合法IP ├─082 棋盘格子走法 ├─083 在字符串中找出连续最长数字串 ├─084 int数组分组,两组和相等 │ └─Source │ └─Debug ├─086 人民币转换 │ └─Source │ └─Debug ├─087 表示数字 ├─090 自动售货系统 │ └─Source │ └─Debug └─091 24点输出 └─Debug

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值