瀏覽代碼

New problem done

master
Lachlan Jacob 5 年之前
父節點
當前提交
f07c636b11
共有 3 個檔案被更改,包括 45 行新增0 行删除
  1. 21
    0
      461/main.c
  2. 19
    0
      461/problem.txt
  3. 5
    0
      461/run.sh

+ 21
- 0
461/main.c 查看文件

@@ -0,0 +1,21 @@
#include <stdio.h>

int countBits(int);

int hammingDistance(int x, int y) {
return countBits(x ^ y);
}

int countBits(int v) {
int c;
for (c = 0; v; v >>= 1) {
c += v & 1;
}
return c;
}

int main() {
printf("Expected: 2\n");
printf("Got: %d\n", hammingDistance(1, 4));
return 0;
}

+ 19
- 0
461/problem.txt 查看文件

@@ -0,0 +1,19 @@
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ x, y < 231.

Example:

Input: x = 1, y = 4

Output: 2

Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑

The above arrows point to positions where the corresponding bits are different.

+ 5
- 0
461/run.sh 查看文件

@@ -0,0 +1,5 @@
#!/bin/bash

gcc -o main main.c
./main
rm main

Loading…
取消
儲存