您的位置:首页 > 其它

zoj1622----------------------------Switch

2012-08-03 10:15 204 查看
There are N lights in a line. Given the states (on/off) of the lights, your task is to determine at least how many lights should be switched (from on to off, or from off to on), in order to make the lights on and off alternatively.

Input

One line for each testcase.

The integer N (1 <= N <= 10000) comes first and is followed by N integers representing the states of the lights ("1" for on and "0" for off).

Process to the end-of-file.

Output

For each testcase output a line consists of only the least times of switches.

Sample Input

3 1 1 1

3 1 0 1

Sample Output

1

0

一个个挨着往后判断并变换

因为第一次变换没有考虑第一个灯

故需要再次先改变第一个灯

然后再判断变换

最后判断两次,哪次最小

输出即可

代码:

#include <iostream>
using namespace std;

int main()
{
int N,i,count,count1,count2;
while(cin>>N)
{
int a
,b
;
for(i=0;i<N;i++)
cin>>a[i];
for(i=0;i<N;i++)
b[i]=a[i];
count=0;
for(i=0;i+1<=N-1;i++)
{
if(a[i]==0&&a[i+1]==0)
{
a[i+1]=1;
count++;
}
else if(a[i]==1&&a[i+1]==1)
{
a[i+1]=0;
count++;
}
}
count1=count;
count=0;
for(i=0;i<N;i++)
a[i]=b[i];
if(a[0]==1)
{
a[0]=0;
count++;
}
else
{
a[0]=1;
count++;
}
for(i=0;i+1<=N-1;i++)
{
if(a[i]==0&&a[i+1]==0)
{
a[i+1]=1;
count++;
}
else if(a[i]==1&&a[i+1]==1)
{
a[i+1]=0;
count++;
}
}
count2=count;
if(count1<=count2)
cout<<count1<<endl;
else
cout<<count2<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: