C++基础练习题(一): 查找最短单词
/*<说明>
编程实现将字符串中最短的单词输出,在主函数中输入字符串,编写一个函数完成最短单词的查找
</说明>*/
#include<time.h>
#include<iostream>
using namespace std; void shortestWord(char* in)
{
int i,j=;
int o[];
for(i=;*(in+i)!=;i++)
{
if(*(in+i)==' ')
{
o[j]=i; //o的作用是定位每一个空格的位置 通过空格位置间隔的大小判断单词的长短
j++;
}
}
j--;
int z=;
int k=o[]; //k是最短单词的个数 初始化为第一个空格的位置 因为后面的操作没有考虑第一个
int l[];
for(;j!=;j--)
{
if(o[j]-o[j-]-<k)
{
z=;
k=o[j]-o[j-]-;
l[]=j-;
}
else if(o[j]-o[j-]-==k)
{
z++; //z统计有多少个最短单词
l[z]=j-;
}
}
if(o[]==k)
{
for(int n=;n<k;n++)
{
printf("%c",*(in+n)); //如果第一个单词就是最短的 打印
}
printf(" ");
}
for(int m=z;m>;m--)
{ for(int n=;n<=k;n++)
{
printf("%c",*(in+o[l[m]]+n)); //打印其他最短单词
}
printf(" ");
}
}
void main()
{
char in[]="Learning a the parameters of neural networks is perhaps one of the most well studied problems within the field of machine learning. Early work on backpropagation algorithms showed that the gradient of the neural net learning objective could be computed efficiently and used within a gradient descent scheme to learn the weights of a network with multiple layers of non-linear hidden units. Unfortunately, this technique doesn’t seem to generalize well to networks that have very many hidden layers (i.e. deep networks). The common experience is that gradient-descent progresses extremely slowly on deep nets, seeming to halt altogether before making significant progress, resulting in poor performance on the training a set (under-fitting)";
int a=clock();
shortestWord(in);
int b=clock();
int c=b-a;
printf("%d",c);
getchar();
}
上面是自己写的代码 效果并不好 测试了一下运行效果2毫秒 太慢 而且没有考虑有连续空格的情况。
/*<书上答案>*/
#include<iostream>
#include<time.h>
using namespace std;
const int Max=;
char *findshort(char s[])
{
static char s1[Max]; //其地址要返回,所以设计为静态变量
char s2[Max];
int i=,j,len1=,len2=;
while(s[i++]!='\0');
s[i-]=' ';
s[i]='\0';
i=;
while(s[i]!='\0')
{
if(s[i]==' '&&s[i+]!='\0'&&s[i+]==' ') //跳过多余空格
{
i++;
continue;
}
if(s[i]!=' ') //提取一个单词到S2中
{
s2[len2++]=s[i];
}
else if(len1==)
{
len1=;
for(j=;j<len2;j++) //将S2复制到S1中
s1[len1++]=s2[j];
s1[len1]='\0';
len2=;
}
else if(len1>len2)
{
len1=;
for(j=;j<len2;j++) //将S2复制到S1中
s1[len1++]=s2[j];
s1[len1]='\0';
len2=;
}
else
{
len2=;
}
i++;
}
return s1;
}
void main()
{
char s[Max]="asddddd gg las sdlgaw va eg aoeng a ge a e gae geoia ae x eox ge x ieg ns e a dfge qdn i am ver";
cout<<"输入单词串:"; int a=clock();
cout<<"最短单词:"<<findshort(s)<<endl;
int b=clock();
int c=b-a;
cout<<c<<endl;
getchar();
}
这是答案中的 只打印了第一个最短单词 但是实现比自己写的代码快很多。
C++基础练习题(一): 查找最短单词的更多相关文章
- [LeetCode] Shortest Word Distance III 最短单词距离之三
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- noi25 最长最短单词(为什么会出现运行时错误)
noi25 最长最短单词(为什么会出现运行时错误) 一.总结 一句话总结:比如除以零,数组越界,指针越界,使用已经释放的空间,数组开得太大,超出了栈的范围,造成栈溢出 1.c++报runtime er ...
- js基础练习题(1)
1.字符串 视频教程地址: js基础练习题 1.如何连接两个或者两个以上字符串? var cssname = 'box' var num = 1 var html = '<div class=& ...
- Linux基础练习题(二)
Linux基础练习题(二) 1.复制/etc/skel目录为/home/tuer1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限. [root@www ~]# cp -r ...
- [LeetCode] Shortest Word Distance II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- AC日记——最长最短单词 openjudge 1.7 25
25:最长最短单词 总时间限制: 1000ms 内存限制: 65536kB 描述 输入1行句子(不多于200个单词,每个单词长度不超过100),只包含字母.空格和逗号.单词由至少一个连续的字母构成 ...
- [Swift]LeetCode244.最短单词距离 II $ Shortest Word Distance II
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [Swift]LeetCode245.最短单词距离 III $ Shortest Word Distance III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
随机推荐
- WCF binding的那些事!!!
原文地址:http://www.cnblogs.com/Anima0My/archive/2008/04/16/1156146.html WCF中常用的binding方式: BasicHttpBind ...
- 【转载】利用Unity自带的合图切割功能将合图切割成子图
虽然目前网上具有切割合图功能的工具不少,但大部分都是自动切割或者根据plist之类的合图文件切割的, 这种切割往往不可自己微调或者很难维调,导致效果不理想. 今天逛贴吧发现了一位网友写的切割合图插件很 ...
- 【AngularJS】—— 10 指令的复用
前面练习了如何自定义指令,这里练习一下指令在不同的控制器中如何复用. —— 来自<慕课网 指令3> 首先看一下一个小例子,通过自定义指令,捕获鼠标事件,并触发控制器中的方法. 单个控制器的 ...
- R语言 推荐算法 recommenderlab包
recommend li_volleyball 2016年3月20日 library(recommenderlab) library(ggplot2) # data(MovieLense) dim(M ...
- linux 中断理解
1.进程.线程只针对的是应用层,而内核调用.驱动没有这种概念,调用的都是内核调用里相同的函数或变量,所以应用层多个应用操作同个硬件时,特别是要加互斥操作,8250通过cs针脚决定发送数据给哪个串口 2 ...
- activiti查看流程图,有中文乱码
第一种 因为服务器缺少必要的字体到这的问题: 解决办法 <!-- 发布流程生成图片是正常显示中文 --> <property name="activi ...
- Android学习笔记(五)——活动的生命周期
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 为了能写出流畅连贯的程序,我们需要了解一下活动的生命周期. 一.返回栈 Android 中的活动是可以层叠的. ...
- Xcode 8 : iOS xib is missing from working copy、iOS misssing file
1,造成此问题可能是SVN的原因,通过命令行解决. 2.未使用SVN,竟然是icloud 造成的,通过 改变网络状态 + 重启Xcode 解决.
- u-boot 2011.09 调用kernel 的流程
这段时候我总是觉得有个问题,u-boot 的存在是不是就是为了调用kernel 而存在的. 所以,粗浅的跟了一下这个流程,还有很多细节上的东西没有做好,往指正. u-boot-2011.9 调用内核代 ...
- github 多个项目共用同一个key的方法
后面的项目不用添加ssh keys, 直接在项目下设置合作者(Collaborators), 搜索出去加进去即可免密码传代码.