zoukankan      html  css  js  c++  java
  • zoj 1622 Switch 开关灯 简单枚举

    ZOJ Problem Set - 1622
    Switch

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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

    这道题很简单,我觉得之所以写是因为提供了自己的思路,在这里将奇数调为开,偶数调为关,

    和为num1,这样可以得到另一面就是N-num1就是奇数调为关,偶数调为开,这样哪个结果小于N/2哪个就能成为最小的切换数

    #include<stdio.h>
    #include<iostream>
    #include<cstring>
    using namespace std;
    int a[10005];
    int main()
    {
    	int i,n,tempo,tempj,temp;
    	while(scanf("%d",&n)!=EOF)
    	{
    		memset(a,0,sizeof(a));
    		tempo=tempj=temp=0;
    		for(i=0;i<n;i++)
    		{
    			scanf("%d",&a[i]);
    			if(i%2==0)
    			{
    				if(a[i]==1)
    					tempj++;
    				else
    					tempo++;
    			}
    			else
    			{
    				if(a[i]==0)
    					tempj++;
    				else
    					tempo++;
    			}
    		}
    		if(tempj>tempo)
    			temp=tempo;
    			else
    			temp=tempj;
    		printf("%d
    ",temp);
    	}
    	return 0;
    }
    
    
    
    


  • 相关阅读:
    linux centos下载地址
    什么是镜像文件?
    Linux下处理JSON的命令行工具:jq---安装
    CentOS7安装第三方yum源EPEL
    CentOS 6.5 下编译安装 Nginx 1.8.0
    CentOS 6.7 如何启用中文输入法
    Linux Yum 命令使用举例
    YUM 安装及清理
    Linux常用命令之rpm安装命令
    使用git代替FTP部署代码到服务器的例子
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3365984.html
Copyright © 2011-2022 走看看