华为编程大赛—路灯统计

题目A:路灯统计

某省会城市街道纵横交错,为了监控路灯的运行状况,每条街道使用一个数字字符串标识该街道上所有路灯的运行状况。

 假设路灯只有如下3种状态(分别用数字0, 1, 2标识,一盏路灯只对应其中一种状态):

0 标识路灯熄灭; 1 标识路灯开启; 2 标识路灯故障;

请根据输入的字符串,找出该街道上连续的处于相同状态的路灯的最大个数。若两种状态的路灯数量相同,则返回最先出现的路灯状态。


#include "stdafx.h"
#include<vector>
#include<iostream.h>
using namespace std;


int lightscount(vector<int> a,int n)
{
    int i=0,j,maxcount=1,state;
	static int count=1;
	for(i=0;i<n;i++)
		if(a[i]<0||a[i]>2) {cout<<"输入有误"<<endl;return 0;}
    for(i=0;i<n;i++)
    cout<<a[i]<<endl;
	static int temp=a[0];
	for(j=1;j<n;j++)
	{ 
		if(a[j]==temp) {count=count+1;}
	    else 
		{
		  if(count>maxcount) {maxcount=count;state=temp;count=1;temp=a[j];}
		  else{count=1;temp=a[j];}
		} 
	} 
	if(count>maxcount) {maxcount=count;state=temp;}
	cout<<"最大连续路灯数及路灯状态结果:"<<maxcount<<" "<<state<<endl;
}


int main(int argc, char* argv[])
{
	vector<int> a;
	int i=0,value,size;
	cout<<"请输入路灯状态字符串(0 1 2):"<<endl;
	while(cin>>value)
		a.push_back(value);
	size=a.size();
	lightscount(a,size);
	getchar();
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值