noi25 最长最短单词(为什么会出现运行时错误)
noi25 最长最短单词(为什么会出现运行时错误)
一、总结
一句话总结:比如除以零,数组越界,指针越界,使用已经释放的空间,数组开得太大,超出了栈的范围,造成栈溢出
数组越界,指针越界,使用已经释放的空间,数组开得太大,超出了栈的范围,造成栈溢出1、c++报runtime error是什么意思?
runtime error (运行时错误)就是程序运行到一半,程序就崩溃了。
2、为什么会出现运行时错误?
①除以零
②数组越界:int a[3]; a[10000000]=10;
③指针越界:int * p; p=(int *)malloc(5 * sizeof(int)); *(p+1000000)=10;
④使用已经释放的空间:int * p; p=(int *)malloc(5 * sizeof(int));free(p); *p=10;
⑤数组开得太大,超出了栈的范围,造成栈溢出:int a[100000000];一般来说,在
3、算法题如何查错(比如这里的运行错误)?
把代码按样例运行自己推演一遍,就知道错在哪了 ,比如这里的运行错误,
拿特殊案例推演(这里用的空字符串案例就发现最后代码有问题),容易发现错误,就是推演
二、noi25 最长最短单词(为什么会出现运行时错误)
1、题目描述
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
-
输入1行句子(不多于200个单词,每个单词长度不超过100),只包含字母、空格和逗号。单词由至少一个连续的字母构成,空格和逗号都是单词间的间隔。
试输出第1个最长的单词和第1个最短单词。
- 输入
- 一行句子。
- 输出
- 两行输出:
第1行,第一个最长的单词。
第2行,第一个最短的单词。 - 样例输入
-
I am studying Programming language C in Peking University
- 样例输出
-
Programming
I - 提示
- 如果所有单词长度相同,那么第一个单词既是最长单词也是最短单词。
- 来源
- 习题(12-2)
2、代码
代码一:
#include<iostream>
#include<cstring>
char a[];// 数组要足够大
using namespace std;
int main()
{
gets(a);
int minn = , maxn = , c, d, e, q = , b = ;
int l = strlen(a);
a[l] = ' ';//不加这一句会出现runtime error
for(int i = ; i <= l; ++i)
{
if(a[i] != ' ' && a[i] != ',')
{
++ b;
}
else if(b > ) //为单词结束符号
{
if(b > maxn)
{
maxn = b;
c = i - b;//记录单词的头位置
}
if(b < minn)
{
minn = b;
d = i - b;
}
b = ;//单词长度清0,开始计算下一个单词
} }
//输出最长单词和最短单词
for(int i = c; i <= c + maxn - ; ++i)
cout << a[i];
cout << endl;
for(int i = d; i <= d + minn - ; ++i)
cout << a[i];
return ;
}
代码一种不加第十句,会出现运行时错误。
10 a[l] = ' ';//不加这一句会出现runtime error
再不加第十句,并且第11句换成小于l,也报运行时错误,原因可能是 字符串没有被访问完全而再次访问,也就是说字符串没有访问到'\0'。
11 for(int i = 0; i <= l; ++i)
代码二:
#include<iostream>
#include<cstring>
char a[];// 数组要足够大
using namespace std;
int main()
{
gets(a);
int minn = , maxn = , c, d, e, q = , b = ;
int l = strlen(a);
//a[l] = ' ';//
for(int i = ; i <=l; ++i)
{
if(a[i]=='\0'){
if(b > ){
if(b > maxn)
{
maxn = b;
c = i - b;//记录单词的头位置
}
if(b < minn)
{
minn = b;
d = i - b;
}
b = ;//单词长度清0,开始计算下一个单词
} }
if(a[i] != ' ' && a[i] != ',')
{
++ b;
}
else if(b > ) //为单词结束符号
{
if(b > maxn)
{
maxn = b;
c = i - b;//记录单词的头位置
}
if(b < minn)
{
minn = b;
d = i - b;
}
b = ;//单词长度清0,开始计算下一个单词
} }
//输出最长单词和最短单词
for(int i = c; i <= c + maxn - ; ++i)
cout << a[i];
cout << endl;
for(int i = d; i <= d + minn - ; ++i)
cout << a[i];
return ;
}
三、为什么会出现运行时错误
runtime error可能的原因
runtime error (运行时错误)就是程序运行到一半,程序就崩溃了。
比如说:
①除以零
②数组越界:int a[3]; a[10000000]=10;
③指针越界:int * p; p=(int *)malloc(5 * sizeof(int)); *(p+1000000)=10;
④使用已经释放的空间:int * p; p=(int *)malloc(5 * sizeof(int));free(p); *p=10;
⑤数组开得太大,超出了栈的范围,造成栈溢出:int a[100000000];一般来说,在
oj上做题都把数组设成全局变量,减少5出现的可能。
有的时候再出现这样的错误还会给提示
Runtime Error(ARRAY_BOUNDS_EXCEEDED) // array bounds exceed 数组越界
Runtime Error(DIVIDE_BY_ZERO) //divisor is nil 除零
Runtime Error(ACCESS_VIOLATION) //illegal memory access 非法内存读取
Runtime Error(STACK_OVERFLOW) //stack overflow 系统栈过载
这样可以照着上面查找错误。
解答:
出现runtime error的原因是 数组越界访问,原因是因为没有非下面的c赋初值,
8 int minn = 110, maxn = 0, c, d, e, q = 0, b = 0;
多组数据下,一定会造成数组的越界访问的
50 for(int i = c; i <= c + maxn - 1; ++i)
51 cout << a[i];
52 cout << endl;
noi25 最长最短单词(为什么会出现运行时错误)的更多相关文章
- AC日记——最长最短单词 openjudge 1.7 25
25:最长最短单词 总时间限制: 1000ms 内存限制: 65536kB 描述 输入1行句子(不多于200个单词,每个单词长度不超过100),只包含字母.空格和逗号.单词由至少一个连续的字母构成 ...
- [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] Shortest Word Distance II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- C++基础练习题(一): 查找最短单词
/*<说明> 编程实现将字符串中最短的单词输出,在主函数中输入字符串,编写一个函数完成最短单词的查找 </说明>*/ #include<time.h> #inclu ...
- OpenJudge就算概论-最长单词2【寻找句子内部最长的单词】
/*===================================== 最长单词2 总时间限制: 1000ms 内存限制: 65536kB 描述 一个以'.'结尾的简单英文句子,单词之间用空格 ...
- [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 ...
- [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 ...
- 输出字符串中最长的单词 C# 算法
要求: 设计一个算法从一片英语文章或者英语字符串里面输出其中最长的单词. Input: string Output: string 尽可能多的设计测试用例来测试这个算法. 考虑空间和时间复杂度 ...
随机推荐
- U-BOOT概述及源码分析(一)
嵌入式Linux系统从软件角度通常可以分为以下4个层次: 引导加载程序 | Linux内核 | 文件系统 | 用户应用程序 嵌入式Linux系统中典型分区结构: 正常启动过程中,Bootloader首 ...
- 【Henu ACM Round#16 A】 Bear and Game
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 看看什么时候t[i]-t[i-1]>15. 输出t[i-1]+15就好. 不存在这样的i就输出min(t[n]+15,90) ...
- Vim操作的四种模式
Vim的四种模式一.启动Vim1.双击桌面的图标,就可以启动Vim(是图形界面的)2.在开始菜单---点--运行 接着输入 vim 或者gvim,就可以启动Vim或Gvim了.二.Vim的模式1.Vi ...
- hdu 1171 Big Event in HDU(01背包)
代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; in ...
- [Android]新建项目继承Activity不继承ActionBarActivity
在SDK更新后,在eclipse新建Android项目时.我们常常会碰到这样一种事情:新建的MainActivity不再继承Activity而是继承ActionBarActivity,因为一些人的开发 ...
- webgoat 7.1 实战指南
WSASP中文文档参考链接: http://www.owasp.org.cn/owasp-project/2017-owasp-top-10 OWASP Top 10 2017中文版V1.3http: ...
- vim 基础学习之查找
普通模式下 /->正向查找 n-向下查找 N-向上查找 ?->反向查找 N-向下查找 n-向上查找 <C-r><C-w> <C-r>-引用,例如引用寄存 ...
- a标签中的javascript:;是什么
a标签中的javascript:;是什么 一.问题 <a>标签中href="javascript:;"表示什么意思?? <a id="jsPswEdit ...
- Vue中Mixins使用
mixins是一种分发Vue组件中可复用功能的一种灵活方式. mixins是一个JavaScript对象,可以包含组件中的任意选项,比如Vue实例中生命周期的各个钩子函数,也可以是data.compo ...
- FZU 1921 栀子花开
栀子花开 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on FZU. Original ID: 19216 ...