From 62a1babbe6200a1f696ce55235e7bbaf747e8ca5 Mon Sep 17 00:00:00 2001 From: Mars Liu Date: Thu, 21 Oct 2021 21:54:47 +0800 Subject: [PATCH] add oct --- .../check.md" | 6 +- .../config.json" | 2 +- .../count.json" | 6 ++ .../cout.md" | 74 +++++++++++++ .../config.json" | 2 +- .../oct.json" | 6 ++ .../oct.md" | 100 ++++++++++++++++++ 7 files changed, 191 insertions(+), 5 deletions(-) create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/2.\344\272\214\350\277\233\345\210\266\346\225\260/count.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/2.\344\272\214\350\277\233\345\210\266\346\225\260/cout.md" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/3.\345\205\253\350\277\233\345\210\266/oct.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/3.\345\205\253\350\277\233\345\210\266/oct.md" diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/1.\344\275\215\345\222\214\345\255\227\350\212\202/check.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/1.\344\275\215\345\222\214\345\255\227\350\212\202/check.md" index 059c793..8195de2 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/1.\344\275\215\345\222\214\345\255\227\350\212\202/check.md" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/1.\344\275\215\345\222\214\345\255\227\350\212\202/check.md" @@ -23,7 +23,7 @@ int main(char** args){ ```c -int check(int x) { +unsigned int check(unsigned int x) { if(x < 2){ return 0; } @@ -48,7 +48,7 @@ int check(int x) { ```c -int check(int x) { +unsigned int check(unsigned int x) { return x & (x-1) == 0; } @@ -57,7 +57,7 @@ int check(int x) { ### 边界条件处理的不对 ```c -int check(int x) { +unsigned int check(unsigned int x) { for(int value = x; value > 0; value = value/2) { if((value % 2) != 0){ return 0; diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/2.\344\272\214\350\277\233\345\210\266\346\225\260/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/2.\344\272\214\350\277\233\345\210\266\346\225\260/config.json" index 4ef5ef7..a8159a5 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/2.\344\272\214\350\277\233\345\210\266\346\225\260/config.json" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/2.\344\272\214\350\277\233\345\210\266\346\225\260/config.json" @@ -6,5 +6,5 @@ "C语言" ], "children": [], - "export": [] + "export": ["count.json"] } \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/2.\344\272\214\350\277\233\345\210\266\346\225\260/count.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/2.\344\272\214\350\277\233\345\210\266\346\225\260/count.json" new file mode 100644 index 0000000..7e3962c --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/2.\344\272\214\350\277\233\345\210\266\346\225\260/count.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "check.md", + "exercise_id":"277ffa9a36b648c8a55a512c7e5daf99" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/2.\344\272\214\350\277\233\345\210\266\346\225\260/cout.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/2.\344\272\214\350\277\233\345\210\266\346\225\260/cout.md" new file mode 100644 index 0000000..834656c --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/2.\344\272\214\350\277\233\345\210\266\346\225\260/cout.md" @@ -0,0 +1,74 @@ +# 计算位数 + +计算整数的二进制形式有多少位。 + +## template + +```c +#include + +$code + +int main(char** args){ + printf("bits(%d) has %d bits\n", 100, bits(100)); + printf("bits(%d) has %d bits\n", 32, bits(32)); + printf("bits(%d) has %d bits\n", 10, bits(10)); + printf("bits(%d) has %d bits\n", 25, bits(25)); + printf("bits(%d) has %d bits\n", 7, bits(7)); + return 0; +} +``` + +## 答案 + +```c +unsigned int bits(unsigned int x){ + unsigned int n = 0; + while(x){ + x >>= 1; + n++; + } + return n; +} +``` + +## 选项 + +### 位移计算没有保存 + +```c +unsigned int bits(unsigned int x){ + unsigned int n = 0; + while(x){ + x >> 1; + n++; + } + return n; +} +``` + +### 没有括号导致流程错误 + +```c +unsigned int bits(unsigned int x){ + unsigned int n = 0; + while(x) + x >>= 1; + n++; + + return n; +} +``` + +### 运算符错误 + +```c +unsigned int bits(unsigned int x){ + unsigned int n = 0; + while(x){ + x << 1; + n++; + } + return n; +} +``` \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/3.\345\205\253\350\277\233\345\210\266/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/3.\345\205\253\350\277\233\345\210\266/config.json" index 4ef5ef7..66cb2b1 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/3.\345\205\253\350\277\233\345\210\266/config.json" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/3.\345\205\253\350\277\233\345\210\266/config.json" @@ -6,5 +6,5 @@ "C语言" ], "children": [], - "export": [] + "export": ["oct.json"] } \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/3.\345\205\253\350\277\233\345\210\266/oct.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/3.\345\205\253\350\277\233\345\210\266/oct.json" new file mode 100644 index 0000000..4b76f65 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/3.\345\205\253\350\277\233\345\210\266/oct.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "oct.md", + "exercise_id":"b15add5a05e84ecdbdf862dcfa722b30" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/3.\345\205\253\350\277\233\345\210\266/oct.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/3.\345\205\253\350\277\233\345\210\266/oct.md" new file mode 100644 index 0000000..03ab8ec --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/3.\345\205\253\350\277\233\345\210\266/oct.md" @@ -0,0 +1,100 @@ +# 八进制转换 + +将正整数输出为八进制形式的字符串。现已有申请内存的`create_buffer`函数和释放内存的`free_buffer` +函数,正确的字符串转换函数是: + +## template + +```c +#include +#include + +unsigned int count(unsigned int x) { + unsigned int n = 0; + while(x){ + x >>= 3; + n++; + } + return n; +} + +int create_buffer(unsigned int x, char** ref){ + unsigned int length = count(x); + *ref = malloc(length*sizeof(char)); + return length; +} + +$code + +void free_buffer(char** buffer) { + free(*buffer); +} + +void print_oct(unsigned int x){ + char* oct_string = NULL; + int len = create_buffer(x, &oct_string); + put_oct(x, oct_string, len); + printf("%d oct: %s \n", x, oct_string); + free_buffer(&oct_string); +} + +int main(char** args){ + print_oct(100); + print_oct(25); + print_oct(64); + print_oct(15); + return 0; +} +``` + +## 答案 + +```c +void put_oct(unsigned int x, char* buffer, int n) { + char start = '0'; + while(x){ + n--; + buffer[n] = start + x % 8; + x >>= 3; + } +} +``` + +## 选项 + +### 索引错误,会导致内存写越界 + +```c +void put_oct(unsigned int x, char* buffer, int n) { + char start = '0'; + while(x){ + buffer[n] = start + x % 8; + n--; + x >>= 3; + } +} +``` + +### 没写括号导致流程错误 + +```c +void put_oct(unsigned int x, char* buffer, int n) { + char start = '0'; + while(x) + buffer[--n] = start + x % 8; + x >>= 3; +} +``` + +### 位移操作错误 + +```c +void put_oct(unsigned int x, char* buffer, int n) { + char start = '0'; + while(x){ + n--; + buffer[n] = start + x % 8; + x >>= 8; + } +} +``` \ No newline at end of file -- GitLab