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
一个个挨着往后判断并变换
因为第一次变换没有考虑第一个灯
故需要再次先改变第一个灯
然后再判断变换
最后判断两次,哪次最小
输出即可
代码:
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; }
相关文章推荐
- zoj 1622 Switch
- ZOJ 1622 Switch
- zoj 1622 Switch
- (水水水)zoj 1622 switch
- zoj 1622 Switch 开关灯 简单枚举
- ZOJ 1622 Switch
- zoj 1622 switch
- zoj 1622 Switch 开关灯 简单枚举
- ZOJ Problem Set–1622 Switch
- zoj 1622
- ZOJ 1622
- ZOJ-1622
- 1622_Switch
- 1622 Switch
- zoj1622
- ZOJ 1871. Steps
- ZOJ 2475 Benny's Compiler 有向图判读点是否存在环内
- ZOJ 3626(树形DP+背包+边cost)
- UI:SwitchButton,可滑动的button
- ZOJ 1010