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 059c793c51df26dba67bdcccee6333ec1431f786..8195de2c9bfbe7b0b75ae67939c64393e4575449 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 4ef5ef7a3dc47eae3b55e6f46638e09457ec1a58..a8159a51b3ad1248ed1b26bd0184e1874d2f1bfe 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 0000000000000000000000000000000000000000..7e3962c935701c337ffc58f0ec45e8fc48e115f7 --- /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 0000000000000000000000000000000000000000..834656c59561a69dff895ad87f80487f1b15e594 --- /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 4ef5ef7a3dc47eae3b55e6f46638e09457ec1a58..66cb2b182294d11a6084a7950aae751d63ab242f 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 0000000000000000000000000000000000000000..4b76f65de0bf99fc15a8cf1bc5078835f7a5cdee --- /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 0000000000000000000000000000000000000000..03ab8ec456fa972b525b8c3ecc0e7ef2af96c4bc --- /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