当前位置:网站首页>C语言练习----指针字符串、链表
C语言练习----指针字符串、链表
2022-06-29 17:09:00 【Sunshing15】
1. 指针与字符串----字符串对比
题目内容
题目内容:
题目说起来很简单,你会读到两个字符串,每个字符串占据一行,每个字符串的长度均小于10000字符,而且第一个字符串的长度小于第二个字符串的。你的程序要找出第一个字符串在第二个字符串中出现的位置,输出这些位置,如果找不到,则输出-1。
注意,第一个字符的位置是0。
注意,第一个字符串在第二个字符串中的位置可能不止一处。
注意,字符串中可能含有空格。
注意,两个字符串的长度一定大于0。
输入格式:
两个字符串,一行一个。
输出格式:
第一个字符串在第二个字符串中出现的位置,按照从小到到的顺序排列,每个数字后面有一个空格。
如果在第二个字符串中找不到第一个字符串,则输出-1。
输入样例:
abba
ababbba abbabbabbabbaacc
输出样例:
8 11 14 17
时间限制:500ms内存限制:32000kb
代码详情
#include <stdio.h>
#include <string.h>
int main(){
char ft[1000];//声明第一个较短的字符串
gets(ft); //读取第一个字符串
char sd[1000];
gets(sd);//读取第二个较长的字符串
int flag=1;
if(strstr(sd,ft)==NULL){//如果在第二个较长的字符串sd中找不到第一个较短的字符串ft,则输出-1
flag=0;
printf("-1");
}
char *p=sd;//定义指针p指向第二个字符串的首地址
while(flag){//只要可以找到满足条件
p=strstr(p,ft);//srstr()返回包含第二个字符串的第一个字符串的首地址
if(p==NULL){
flag = 0;
}else{
printf("%d ", p - sd);
}
p++;
}
return 0;
}
2. 链表----逆序输出数字
题目内容
题目内容:
你的程序会读入一系列的正整数,预先不知道正整数的数量,一旦读到-1,就表示输入结束。然后,按照和输入相反的顺序输出所读到的数字,不包括最后标识结束的-1。
输入格式:
一系列正整数,输入-1表示结束,-1不是输入的数据的一部分。
输出格式:
按照与输入相反的顺序输出所有的整数,每个整数后面跟一个空格以与后面的整数区分,最后的整数后面也有空格。
输入样例:
1 2 3 4 -1
输出样例:
4 3 2 1
时间限制:500ms内存限制:32000kb
代码详情
#include <stdio.h>
#include <stdlib.h>
typedef struct _node { //定义一个类型为struct _node的节点结构,节点中有值还有指向前一个节点的指针
int value;
struct _node *before;
} Node; //将 struct _node类型缩写为Node
int main(){
Node *head = NULL; //链表头结点初始化为空
int number;
scanf("%d", &number);
while(number != -1){
Node *p = (Node*)malloc(sizeof(Node));//每次为一个节点开辟一个内存空间
p->value = number;
p->before = head; //p->before为头结点,将新结点p连接到链表(即p的指针指向头结点)
head = p; //再将头结点更新为p结点
scanf("%d", &number);
}
//遍历方法1:for循环
//Node *q;
//for ( q = head; q; q=q->before) {
//printf("%d ", q->value);
//}
//free(q);
//遍历方法2:while循环
while(head){
printf("%d ", head->value);
head = head->before;
}
free(head);
return 0;
}
边栏推荐
- An error is reported in the Flink SQL rownumber. Who has met him? How to solve it?
- InheritableThreadLocal 在线程池中进行父子线程间消息传递出现消息丢失的解析
- 机器学习7-支持向量机
- 卷妹带你学数据库---5天冲刺Day1
- 从Mpx资源构建优化看splitChunks代码分割
- 首批!腾讯云通过中国信通院政务协同平台解决方案能力评估
- 解题元宇宙,网络游戏中的多元通信方案
- 6.26cf simulation game d: black and white questions
- 0基础自学STM32(野火)——使用寄存器点亮LED——GPIO功能框图讲解
- 腾讯云发布CDW ClickHouse升级版,为海量数据实时分析场景提供极速体验
猜你喜欢

适合中小企业的项目管理系统有哪些?

SLAM中的子图

AI and creativity

腾讯云发布自动化交付和运维产品Orbit,推动企业应用全面云原生化

I, a tester from a large factory, went to a state-owned enterprise with a 50% pay cut. I regret it

controller、service、dao之间的关系

Picture and text show you how to thoroughly understand the atomicity of MySQL transaction undolog

Perhaps in two years, ASML will be free to supply EUV lithography machines to China

In order to prevent being rectified after 00, a company requires employees not to sue the company

@Component与@Configuration区别
随机推荐
【现代信号处理第六次作业】
6.25atcoderabc257e - addition and multiplication 2
Mathematical knowledge: finding combinatorial number II - finding combinatorial number
使用kalibr標定工具進行單目相機和雙目相機的標定
基于汇编实现的流载体的LSB隐藏项目
PHP删除目录
知道创宇为能源行业资产管理助力,入选工信部2021物联网示范项目
Information | Zuckerberg was rated as the most careless CEO in the global IT industry; China Mobile R & D tethered UAV emergency communication high altitude base station
Practice | solution for image upload, rotation and compression on mobile terminal
How to use openmesh to realize the conversion between 3D files of different formats
Shenzhen internal promotion | Shenzhen Institute of computing science recruits assistant machine learning Engineer (school recruitment)
使用kalibr标定工具进行单目相机和双目相机的标定
第42期:MySQL 是否有必要多列分区
如何利用OpenMesh实现不同格式的3D文件间的转换
Viewing splitchunks code segmentation from MPX resource construction optimization
研究所的这些优势真香!上岸率还极高!
KUKA机器人外部轴配置你一定要知道的那些知识
【 OpenGL 】 Random Talk 1. The camera rotates around a point in the space by dragging the mouse
GNN notes: message propagation model
Redis bloom filter and cuckoo filter