zoukankan      html  css  js  c++  java
  • Write and Erase

    You are playing the following game with Joisino.

    • Initially, you have a blank sheet of paper.
    • Joisino announces a number. If that number is written on the sheet, erase the number from the sheet; if not, write the number on the sheet. This process is repeated N times.
    • Then, you are asked a question: How many numbers are written on the sheet now?

    The numbers announced by Joisino are given as A1,…,AN in the order she announces them. How many numbers will be written on the sheet at the end of the game?

    Constraints

     

    • 1≤N≤100000
    • 1≤Ai≤1000000000(=109)
    • All input values are integers.

    Input

     

    Input is given from Standard Input in the following format:

    N
    A1
    :
    AN
    

    Output

     

    Print how many numbers will be written on the sheet at the end of the game.

    Sample Input 1

     

    3
    6
    2
    6
    

    Sample Output 1

     

    1
    

    The game proceeds as follows:

    • 6 is not written on the sheet, so write 6.

    • 2 is not written on the sheet, so write 2.

    • 6 is written on the sheet, so erase 6.

    Thus, the sheet contains only 2 in the end. The answer is 1.

    Sample Input 2

     

    4
    2
    5
    5
    2
    

    Sample Output 2

     

    0
    

    It is possible that no number is written on the sheet in the end.

    Sample Input 3

     

    6
    12
    22
    16
    22
    18
    12
    

    Sample Output 3

     

    2
    


    该题就是判断一个数出现的次数,偶数为0,奇数+1;
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    long long a[1000010];
    int main()
    {
        int n,x=1,ans=0;//x表示一个数出现的次数,赋值1 
        cin>>n;
        for(int i=0;i<n;i++)
           cin>>a[i];
        sort(a,a+n);//先排序,更方便判断奇偶 
        for(int i=1;i<n;i++)
        {
            if(a[i]==a[i-1])//出现相同的数,x+1然后mod2 
              x=(x+1)%2;
            else
            {
                ans+=x;//单独的一个数,ans+1,x重新赋值为1,进行循环 
                x=1;
            }      
        }
        if(x%2)//再次判断 ,若不为0,则ans+1 
          ans++;
        cout<<ans<<endl;
        return 0;
    } 
  • 相关阅读:
    PHP程序员7小时学会Kotlin 第二小时
    PHP程序员7小时学会Kotlin系列
    PHP程序员7小时学会Kotlin系列
    技术人员如何形成正确的价值观
    景德镇特色的部门级别与权限
    hhvm的正确安装姿势 http://dl.hhvm.com 镜像
    有钱没钱
    SB心结
    优秀的技术Leader
    BaaS模式的开发思路
  • 原文地址:https://www.cnblogs.com/ylrwj/p/10792504.html
Copyright © 2011-2022 走看看