当前位置:网站首页>牛客网刷题——第三天
牛客网刷题——第三天
2022-07-21 13:39:00 【熬夜磕代码丶】
作者简介: 博主在读计科双非本科,目前大二,正在学习JAVA,数据库,操作系统,计算机网络,数据结构,JAVA Web等…
个人主页:熬夜磕代码丶
作品专栏: 从0到1,学习算法
我变秃了,也变强了
给大家介绍一款程序员必备刷题平台——牛客网
点击注册一起刷题收获大厂offer吧
一、数字在升序数组中出现的次数
给定一个长度为 n 的非降序数组和一个非负数整数 k ,要求统计 k 在数组中出现的次数
暴力求解:
int GetNumberOfk(int* data, int dataLen, int k)
{
int count = 0;
for (int i = 0; i < dataLen; i++)
{
if (*(data + i) == k)
count++;
if (count != 0 && *(data + i) != k)//避免多余循环
break;
}
return count;
}
二、整数转换
编写一个函数,确定需要改变几个位才能将整数A转成整数B
int converInteger(int A, int B)
{
int count = 0;
while (A != B)
{
if (A % 2 != B % 2)
count++;
A /= 2;
B /= 2;
}
return count;
}
三、图片整理
Lily上课时使用字母数字图片教小朋友们学习英语单词,每次都需要把这些图片按照大小(ASCII码值从小到大)排列收好。请大家给Lily帮忙,通过代码解决。Lily使用的图片使用字符"A"到"Z"、“a"到"z”、"0"到"9"表示。
输入描述:
一行,一个字符串,字符串中的每个字符表示一张Lily使用的图片。
输出描述:
Lily的所有图片按照从小到大的顺序输出
这里简单使用下qsort排序即可A出来
#include<stdio.h>
#include<string.h>
int cmp_char(const void* e1,const void *e2)
{
return *(char*)e1-*(char*)e2;
}
int main()
{
char ch[1002]={
0};
scanf("%s",ch);
int sz=strlen(ch);
qsort(ch,sz,1,cmp_char);
printf("%s",ch);
return 0;
}
四、字符个数统计
编写一个函数,计算字符串中含有的不同字符的个数。字符在 ASCII 码范围内( 0~127 ,包括 0 和 127 ),换行表示结束符,不算在字符里。不在范围内的不作统计。多个相同的字符只计算一次
例如,对于字符串 abaca 而言,有 a、b、c 三种不同的字符,因此输出 3 。
输入描述:
输入一行没有空格的字符串。
输出描述:
输出 输入字符串 中范围在(0~127,包括0和127)字符的种数。
int main()
{
char arr[501] = {
0 };
int count[128] = {
0 };//把字符串对应的Ascll码作为下标
int sum = 0;//计算不同字符个数
scanf("%s", arr);
for (int i = 0; i < strlen(arr); i++)
{
count[arr[i]]++;
}
//只要count[i]不等于0,sum++
for (int i = 0; i < 128; i++)
{
if (count[i])
sum++;
}
printf("%d", sum);
return 0;
}
五、箭形图案
KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“”组成的箭形图案。
输入描述:
本题多组输入,每行一个整数(2~20)。
输出描述:
针对每行输入,输出用“”组成的箭形。
#include<stdio.h>
int main()
{
int n = 0;
while (scanf("%d", &n) != EOF)
{
int i = 0;
int j = 0;
int k = 0;
for (i = 0; i < n + 1; i++)
{
for (j = 0; j < n - i; j++)
{
printf(" ");
}
for (k = 0; k <= i; k++)
{
printf("*");
}
printf("\n");
}
for (i = 0; i < n; i++)
{
for (j = 0; j <= i; j++)
{
printf(" ");
}
for (k = 0; k < n - i; k++)
{
printf("*");
}
printf("\n");
}
}
return 0;
}
边栏推荐
猜你喜欢
性能领域:你知道的越多,不知道的也就越多
Figure neural network: gat learning, understanding and pit entry
“中国网事·感动2022”二季度网络感动人物评选结果揭晓
高度关注!2022开放原子开源峰会最新议程一览
Revit API:EditScope
[comprehensive pen test] difficulty 3.5/5, multi solution popular binary tree pen test
solo 博客的 Bubble 皮肤 文章详情的顶部图片怎么去掉
pdf. JS how to Preview PDF files of Base64 type
Map collection traversal in multiple ways
Matlab least square fitting
随机推荐
Write it down once Net analysis of the memory leakage of the background service of a fire Internet of things
How to clean the C disk
ES6 from getting started to mastering 04: default values and remaining parameters of functions
C#使用Objects Comparer进行对象比较
【英语口语】01 - 原子介绍
pdf.js 怎么预览 base64 类型的 pdf 文件
ES6 从入门到精通 # 02:let 和 const 命令
获取美团,饿了么外卖cps和权益链接
C# OPC客户端代码
一文深入浅出理解国产开源木兰许可系列协议
如何以创新驱动增长战略
【js基础】js中常用的操作数组的方法
Photovoltaic power generation system and its MPPT control
HTTP cache policy, strong cache, negotiation cache
Map collection traversal in multiple ways
如何快速的将 DataTable 导入到 Excel 中 ?
C OPC client code
判断一个人靠不靠谱,就看这3点
C语言解题——Number Sequence
npm install 报 -4058错误