牛客网上华为初试的编程题
语言: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);
}
}