ZZULI 1679 Switch
来源:互联网 发布:2015网络银行交易金额 编辑:程序博客网 时间:2024/06/07 19:07
Switch
Time Limit:1000MS Memory Limit:65536K
Total Submit:43 Accepted:23
Description
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 13 1 0 1
Sample Output
10
思路:其实很简单,但是还是想了很长时间才想明白!题意就是给你一组数只有0和1,欲使相邻两个数不相同需要改变多少次求的是次数最少的;比如1 1 1 有两种结果 1 0 1 和0 1 0;所以最少的次数是1;
#include<stdio.h>int a[10001],b[10001],c[10001];int main(){int i,x,an1,an2,n;//an1和an2用来记录次数 while(scanf("%d",&n)!=EOF){for(i=0;i<n;i++)scanf("%d",&a[i]);x=0;//使第一个数字为1an1=0;for(i=0;i<n;i++){b[i]=1-x;x=1-x;//记录i前面的数if(b[i]!=a[i])an1++;}x=1;//使第一个数字为0an2=0;for(i=0;i<n;i++){c[i]=1-x;x=1-x;if(c[i]!=a[i])an2++;}an1=an1>an2?an2:an1;printf("%d\n",an1);}return 0;}
- ZZULI 1679 Switch
- switch
- Switch
- Switch
- switch
- switch
- switch
- Switch
- switch
- switch
- switch
- SWITCH
- Switch
- switch
- switch
- switch
- (switch)
- switch
- 2011.09.12_php 优化工具 xdebug
- 扩展巴科斯范式
- 递推、错排公式
- 实用小方法
- ASP.NET MVC模式 温习(一)排除MVC模式误区
- ZZULI 1679 Switch
- 12.1 理解基类中成员的访问限定符和派生类的限定方式
- 小论万花筒法则(一)
- 提交form会提示下载用JSON返回的以Action为名字的信息,怎么解决这个问题
- PHP垃圾回收机制
- 设计模式
- 2012C++程序设计实验报告【12.2】
- 两个字符串中匹配最大字符串
- VC高级调试方法-条件及数据断点的设定