提交 62a1babb 编写于 作者: M Mars Liu

add oct

上级 b5568f5f
...@@ -23,7 +23,7 @@ int main(char** args){ ...@@ -23,7 +23,7 @@ int main(char** args){
```c ```c
int check(int x) { unsigned int check(unsigned int x) {
if(x < 2){ if(x < 2){
return 0; return 0;
} }
...@@ -48,7 +48,7 @@ int check(int x) { ...@@ -48,7 +48,7 @@ int check(int x) {
```c ```c
int check(int x) { unsigned int check(unsigned int x) {
return x & (x-1) == 0; return x & (x-1) == 0;
} }
...@@ -57,7 +57,7 @@ int check(int x) { ...@@ -57,7 +57,7 @@ int check(int x) {
### 边界条件处理的不对 ### 边界条件处理的不对
```c ```c
int check(int x) { unsigned int check(unsigned int x) {
for(int value = x; value > 0; value = value/2) { for(int value = x; value > 0; value = value/2) {
if((value % 2) != 0){ if((value % 2) != 0){
return 0; return 0;
......
...@@ -6,5 +6,5 @@ ...@@ -6,5 +6,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": ["count.json"]
} }
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "check.md",
"exercise_id":"277ffa9a36b648c8a55a512c7e5daf99"
}
\ No newline at end of file
# 计算位数
计算整数的二进制形式有多少位。
## template
```c
#include <stdio.h>
$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
...@@ -6,5 +6,5 @@ ...@@ -6,5 +6,5 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": ["oct.json"]
} }
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "oct.md",
"exercise_id":"b15add5a05e84ecdbdf862dcfa722b30"
}
\ No newline at end of file
# 八进制转换
将正整数输出为八进制形式的字符串。现已有申请内存的`create_buffer`函数和释放内存的`free_buffer`
函数,正确的字符串转换函数是:
## template
```c
#include <stdio.h>
#include <stdlib.h>
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册