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
  • 相关阅读:
    Mysql高可用集群环境介绍
    JVM 介绍
    SpringCloud Stream 消息驱动
    SpringCloud Bus
    Vue父组件向子组件传递一个动态的值,子组件如何保持实时更新实时更新?
    cordova编译crosswalk-webview插件报错的处理办法
    Xcode10升级项目报错library not found for -lstdc++.6.0.9
    cordova 使用WKWebView 适配iphoneX及解决不能拨打电话问题
    Webpack, VSCode 和 Babel 组件模块导入别名
    vscode debugger for chrome 调试webpack的配置问题
  • 原文地址:https://www.cnblogs.com/PrayG/p/5657963.html
Copyright © 2011-2022 走看看