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 最长最短单词(为什么会出现运行时错误)的更多相关文章

  1. AC日记——最长最短单词 openjudge 1.7 25

    25:最长最短单词 总时间限制:  1000ms 内存限制:  65536kB 描述 输入1行句子(不多于200个单词,每个单词长度不超过100),只包含字母.空格和逗号.单词由至少一个连续的字母构成 ...

  2. [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 ...

  3. [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 ...

  4. C++基础练习题(一): 查找最短单词

    /*<说明> 编程实现将字符串中最短的单词输出,在主函数中输入字符串,编写一个函数完成最短单词的查找 </说明>*/ #include<time.h> #inclu ...

  5. OpenJudge就算概论-最长单词2【寻找句子内部最长的单词】

    /*===================================== 最长单词2 总时间限制: 1000ms 内存限制: 65536kB 描述 一个以'.'结尾的简单英文句子,单词之间用空格 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. 输出字符串中最长的单词 C# 算法

    要求: 设计一个算法从一片英语文章或者英语字符串里面输出其中最长的单词. Input: string     Output: string 尽可能多的设计测试用例来测试这个算法. 考虑空间和时间复杂度 ...

随机推荐

  1. chmod---变更文件或目录的权限

    chmod命令用来变更文件或目录的权限.在UNIX系统家族里,文件或目录权限的控制分别以读取.写入.执行3种一般权限来区分,另有3种特殊权限可供运用.用户可以使用chmod指令去变更文件与目录的权限, ...

  2. PHP如何去掉多维数组的重复值

    1.定义函数 function array_unique_new($arr){ $t = array_map('serialize', $arr);//利用serialize()方法将数组转换为以字符 ...

  3. 树莓派 使用python来操作GPIO 控制LED灯

    一.创建python驱动和控制GPIO 先新建一个文件夹用于放置脚本 mkdir python_gpio 进入文件夹内新建一个gpio_blink.py的脚本 cd python_gpio touch ...

  4. 将yyyyMMdd格式的字符串转成日期DateTime格式

    1.DateTime dt= DateTime.ParseExact("20110720", "yyyyMMdd", Thread.CurrentThread. ...

  5. 【剑指offer】Q25:二叉树中和为某一值的路径

    说明:最烦的就是看别人的博客,题解里直接上代码,一行分析都没有.只是这个题... class BTNode(): def __init__(self, val = -1): self.val = va ...

  6. amaze ui中的icon button

    amaze ui中的icon button 说明几点: 1.链接效果 连接效果的本质一般都是a标签,好像很多button的链接效果都是用的a标签,submit表单提交或者button的type为sub ...

  7. ajax起步 (二)

    Ajax的关键在于XMLHttpRequest对象,如下基本用法: <!DOCTYPE html> <html> <head> <meta charset=& ...

  8. node搭建服务器

    创建简单的服务器,可以简单连接 var http = require("http"); var server = http.createServer(); server.on(&q ...

  9. 00079_增强for循环

    1.格式 /* * JDK1.5新特性,增强for循环 * JDK1.5版本后,出现新的接口 java.lang.Iterable * Collection开始继承Iterable * Iterabl ...

  10. [React] Compound Component (React.Children.map & React.cloneElement)

    Imaging you are building a Tabs component. If looks like: <Tabs> <TabList> <Tab> o ...