---
# System prepended metadata

title: JudgeGirl 50057
tags: [binary, 3.5/5]

---

# JudgeGirl 50057

## Consecutive 0's and 1's

給予 $N$ 個32-bit integer，將他們二進位制中連續的0或連續的1輸出，一組一行，例如50和17448304632連起來的二進位：
`0000000000000000000000000011001001100111111111111111111111111111`

輸出後：
```
00000000000000000000000000
                          11
                            00
                              1
                               00
                                 11
                                   00
                                     111111111111111111111111111
```

注意每一組的第一位前面都要輸出一些空白，若他在第`s`位，則需輸出`s % 40`的空白，每行輸出數字完後都直接換行，不能有多空白

$1≤N≤10000$

## Keys

**記得用unsigned int**

```c
int curr = (num & (1 << i)) > 0
```
可以取得第`i`位位元

維護一個`last`紀錄上一位是0還是1，和一個`pos`紀錄現在在第幾位，當目前數字跟上一個不一樣的時候換行，並且輸出`pos % 40`個空格

```c=
#include <stdio.h>
 
int main() {
    int N;
    scanf("%d", &N);
 
    int last = -1, pos = 0, curr;
    unsigned int num, start;
    for (int i = 0; i < N; i++) {
        start = 1 << 31; // 將1放在最左邊 用int的話這個會是-1 會錯
        scanf("%d", &num);
 
        while (start != 0) {
            curr = (num & start) > 0;
            if (curr != last && last != -1) {
                printf("\n");
                for (int _ = 0; _ < pos % 40; _++) printf(" ");
            }
            printf("%d", curr);
            start = start >> 1;
            pos++;
            last = curr;
        }
    }
    printf("\n");
 
    return 0;
}
```