zoukankan      html  css  js  c++  java
  • 每日一小练——最长平台问题

    上得厅堂,下得厨房,写得代码,翻得围墙,欢迎来到睿不可挡的每日一小练!


    题目:最长平台问题


    内容:一直一个已经从小到大排序的数组,这个数组中的一个平台就是连续的一串同样的元素。而且这个元素不能再延伸。

    比如,在1,2,2,3,3,3,4,5,5,6中1,2,2,3,3,3,4,5,5,6都是平台.试编写一个程序,接受一个数组,把这个数组中最长的平台找出来。在这个样例中, 3,3,3就是该数组的中的最长的平台


    说明:
    这个程序十分简单,可是编写好却不easy,因此在编敲代码时应注意考虑以下几点:
      1.使用变量越少越好
      2.是否能仅仅把数组的元素每个都仅仅查一次就得到结果。
      3.程序语句越少越好

    ps:这个问题以前困扰过David Gries这位知名的计算机科学家。


    我的解法:上来没多想。打开vs2013就敲了起来,问题果然非常easy,分分钟就超神。

    。奥,不正确就攻克了!

    #include <iostream>
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int index = 0;             //数组下标索引
    	int indexEnd = 0;          //目标索引
    	int count = 0;             //计数器
    	int tempCount = 0;         //暂时计数器
    	int arrayNum[100] = { 0 }; //零不算输入元素,所以结尾判零就可以
    	cout << "请输入一个数字序列,数字间以空格隔开,用0表示输入结束:" << endl;
    	while((cin >> arrayNum[index])&&arrayNum[index]!=0)
    		++index;
    	index = 0;
    	while (arrayNum[index] != 0)
    	{		
    		//cout << arrayNum[index] << endl;
    		if (arrayNum[index + 1] != 0)
    		{
    			if (arrayNum[index] == arrayNum[index + 1])
    			{
    				tempCount++;
    			}
    			else
    			{
    				if (tempCount > count)
    				{
    					count = tempCount;
    					indexEnd = index;
    				}
    				tempCount = 0;
    			}
    		}
    		++index;
    	}
    	cout << "输入数字序列为:" << endl;
    	index = 0;
    	while (arrayNum[index] != 0)
    	{
    		cout << arrayNum[index];
    		++index;
    	}
    	cout << endl;
    	cout << "最大的平台是:" << endl;
    	cout << arrayNum[indexEnd] <<endl;
    	cout << "连续次数为:" << endl;
    	cout << count + 1 << endl;
    	getchar();
    	getchar();
    	return 0;
    }
    

    实验结果:



    然后看了下答案,瞬间认为自己应该在多考虑一下这个问题,计算机科学家的解法确实代码少了非常多。。

    #include <iostream>
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int index = 0;             //数组下标索引
    	//int indexEnd = 0;          //目标索引
    	int length = 1;            //平台长度
    	int arrayNum[100] = { 0 }; //零不算输入元素。所以结尾判零就可以
    	cout << "请输入一个数字序列,数字间以空格隔开,用0表示输入结束:" << endl;
    	while((cin >> arrayNum[index])&&arrayNum[index]!=0)
    		++index;
    	index = 0;
    	for (index = 1; arrayNum[index] != 0;index++)
    	{		
    		if (arrayNum[index] == arrayNum[index - length])
    			length++;
    	}
    	cout << "输入数字序列为:" << endl;
    	index = 0;
    	while (arrayNum[index] != 0)
    	{
    		cout << arrayNum[index];
    		++index;
    	}
    	cout << endl;
    	cout << "连续次数为:" << endl;
    	cout << length << endl;
    	getchar();
    	getchar();
    	return 0;
    }
    

    实验结果:



    为了是能更好的对照我和科学家的差距,我把程序的核心代码对照一下

    //科学家的
    for (index = 1; arrayNum[index] != 0;index++)
    {		
    	if (arrayNum[index] == arrayNum[index - length])
    		length++;
    }
    //////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////
    //我的
    while (arrayNum[index] != 0)
    {		
    	if (arrayNum[index + 1] != 0)
    	{
    		if (arrayNum[index] == arrayNum[index + 1])
    		{
    			tempCount++;
    		}
    		else
    		{
    			if (tempCount > count)
    			{
    				count = tempCount;
    				indexEnd = index;
    			}
    			tempCount = 0;
    		}
    	}
    	++index;
    }

    由于数组顺序已经排好了所以科学家用一个变量直接探測平台最远点的想法确实精妙。

    嘿嘿。慢慢学习哈~加油!


                       -End-

    參考文献:《c语言名题精选百则》




  • 相关阅读:
    河南省第十届ACM省赛G:Plumbing the depth of lake
    南洋理工oj 题目92 图像有用区域
    初学欧拉图,知识总结,后续增加
    初学并查集知识总结后续增加
    南阳oj 题目42 一笔画问题
    南阳oj 题目 90 整数划分
    南阳oj题目20吝啬的国度 菜鸟的进阶之路
    南阳oj 题目21 三个水杯
    UVA-540 Team Queue
    HDU-1596 find the safest road
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5167218.html
Copyright © 2011-2022 走看看