zoj1622

来源:互联网 发布:数据结构与算法知识点 编辑:程序博客网 时间:2024/05/16 09:05

题目大意:

给N个灯的状态,你的任务是至少变换几盏灯的状态可以使得灯开关交错

解题思路:

本方法,数一下两种情况下那种需要的次数少选哪个

代码如下:

#include<stdio.h>int main(){ int s[10000]; int n,i; int switch1=0,switch2=0; while(scanf("%d",&n)!=EOF) {  switch1=0;switch2=0;  for(i=0;i<n;++i)   scanf("%d",&s[i]);  for(i=0;i<n;++i){   if(i%2==0){    if(s[i]!=1)     switch1++;   }   else{    if(s[i]!=0)     switch1++;   }  }  for(i=0;i<n;++i){   if(i%2==0){    if(s[i]!=0)     switch2++;   }   else{    if(s[i]!=1)     switch2++;   }  }  printf("%d\n",switch1<=switch2?switch1:switch2); } return 0;}
0 0
原创粉丝点击