寒假6.3

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.
Examples
Input
6
10
10
10
01
10
10
Output
3
Input
4
01
01
10
10
Output
2
判斷前一位置與后一位置上數(shù)是否相同即可,相同則連續(xù),不相同及斷開另起一列。

#include<iostream>
using namespace std;
int main()
{
    int n,x=0;
    cin >> n;
    char a[100001][2];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i][0] >> a[i][1];
    }
    for (int i = 0; i < n;i++ )
    {
        if (a[i][0] == a[i + 1][0])
        {
        }
        else
        {
            x++;
        }
    }
    cout << x;
    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評論 0 10
  • 說起“年味”不得不說起小的時(shí)候,張燈結(jié)彩,殺豬宰羊,熱氣騰騰,人聲喧鬧... 那濃濃的年味已經(jīng)變成一幅幅畫面刻在腦...
    瑾小兮閱讀 661評論 6 21
  • 親愛的梁,你還好嗎? 畢業(yè)這么多年,逐漸地,我們都淡出了同學(xué)的視野,而不經(jīng)意間,你卻進(jìn)入了我的夢。 每當(dāng)有外地同學(xué)...
    星韻r閱讀 357評論 1 5

友情鏈接更多精彩內(nèi)容