【发布时间】:2015-08-25 22:22:54
【问题描述】:
这只是我的第二节编程课。有30个房间,我们要看看每个房间里有什么,然后统计一下。我已经使用 for 循环遍历了 30 个房间,并且我知道我必须使用一个位计数器来查看每个房间中的内容。我不知道如何计算一个单词中的位数。以下是示例输入/输出以及我目前的代码。
示例输入:
9 23 @Z
如果键是:
0 gold_bar
1 silver_bar
2 diamond
3 copper_ring
4 jumpy_troll
5 air
6 angry_troll
7 plutonium_troll
线是 9 23 @Z,然后房间在 9,23(字符 Z,二进制:01011010)有项目 1、3、4、6。silver_bar、copper_ring、jumpy_troll、angry_troll
#include <stdio.h>
#include <stdlib.h>
int main()
{
// contains x and y coordinate
int first, second;
char third[100];
char Map[30][30];
// map initialization
for(int x=0; x<30; x++){
for(int y=0; y<30; y++){
Map[x][y] = '.';
}
}
while(scanf("%d %d %s",&first, &second, third) != -1) {
// Condition 1: a zero coordinate
if (first==0 || second==0) exit(0);
// Condition 2: coordinate out of range
if (first<0 || first>30 || second<0 || second>30){
printf("Error: out of range 0-30!\n");
exit(1);
}
// bit counter
for( int bit_p=0; bit_p<8; bit_p++){
}
Map[second-1][first-1] = third[1];
return 0;
}
示例输入:
1 20 @@
2 21 @A
3 22 @#
4 23 @1
5 22 @@
6 22 @@
7 22 @@
8 22 @@
9 23 @Z Here be trolls � not!
10 23 @+
12 23 @@
13 24 @@
11 22 @@
14 22 @2
15 21 @1
16 20 @@
17 19 @@
18 20 @@
19 19 @@
20 18 @@
21 17 @*
22 16 @*
23 15 @%
0 14 @7
0 gold_bar
1 silver_bar
2 diamond
3 copper_ring
4 jumpy_troll
5 air
6 angry_troll
7 plutonium_troll
样本输出:
6 gold_bar
6 silver_bar
1 diamond
4 copper_ring
4 jumpy_troll
8 air
15 angry_troll
0 plutonium_troll
【问题讨论】:
-
@Olaf 我认为问题的标题具有误导性......
-
@Matthieu:如果真的只是计算是否设置了特定位,那么 OP 真的应该自己做研究。
-
我猜我们对计数的定义不一样...