题目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;
}

44万+

被折叠的 条评论
为什么被折叠?



