zoukankan      html  css  js  c++  java
  • Codeforces 344A Magnets

    Description

    Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other.

    Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own.

    Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed.

    Input

    The first line of the input contains an integer n (1 ≤ n ≤ 100000) — the number of magnets. Then n lines follow. The i-th line (1 ≤ i ≤ n) contains either characters "01", if Mike put the i-th magnet in the "plus-minus" position, or characters "10", if Mike put the magnet in the "minus-plus" position.

    Output

    On the single line of the output print the number of groups of magnets.

    Sample Input

    Input
    6
    10
    10
    10
    01
    10
    10
    Output
    3
    Input
    4
    01
    01
    10
    10
    Output
    2

    Hint

    The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets.

    The second testcase has two groups, each consisting of two magnets.

    题意:给你一个数n,表示接下来会有n行 01或者 10,0表示磁极的正级,1,表示磁极的负极,目的是让你把这n个01或者10连接成一个字符串,判断这个字符串有多少快组成,也就是说有多少各连续的1,1或者0,0把这个长串分成了几个部分。。。

    分析: 简单模拟,瞎搞。

    code:

     1 /*************************************************************************
     2     > File Name: cf.cpp
     3     > Author: PrayG
     4     > Mail: 
     5     > Created Time: 2016年07月10日 星期日 12时57分34秒
     6  ************************************************************************/
     7 
     8 #include<iostream>
     9 #include<cstdio>
    10 #include<bits/stdc++.h>
    11 #include<string>
    12 using namespace std;
    13 const int maxn = 200005;
    14 string arr;
    15 int main()
    16 {
    17     string str;
    18     int n,ans= 1;
    19     cin >> n;
    20     for(int t = 1; t <= n; t++)
    21     {
    22         str.clear();
    23         cin >> str;
    24         if(t == 1)
    25         {
    26             arr += str;
    27         }
    28         else
    29         {
    30             int len = arr.length();
    31            // printf("len = %d
    ",len);
    32            // cout <<"str = " <<  str << endl;
    33             if(arr[len-1] == str[0])
    34             {
    35                 //cout << "arr  = " << arr << endl;
    36                 ans++;
    37             }
    38             arr += str;
    39         }
    40     }
    41     cout << ans << endl;
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    第二次作业
    复盘一个商品期货的通用模型
    C#如何获取枚举(Enum)变量的值
    [C#]Socket通信BeginReceive异步接收数据何时回调Callback
    [C#]浮点数除零无法捕获异常的解决办法
    js对字符串进行编码方法总结
    web最全资源网址
    简单粗暴地理解js原型链--js面向对象编程
    常见前端九十道面试题及答案-韩烨
    C语言文件读写,复制
  • 原文地址:https://www.cnblogs.com/PrayG/p/5657963.html
Copyright © 2011-2022 走看看