1. 题目
![](https://i-blog.csdnimg.cn/blog_migrate/5d8b86d2ea8f70af77faa10b61f75906.png)
示例输入
ABD##EH###CF#I##G##
示例输出
4
2. 代码及思路
- 看到这个题目的第一想法,就是找两个相邻的“#”(考试不得分的想法。。)于是就敲了以下代码。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv) {
string str;
int cnt = 0;
cin >> str;
int i=0, j=1, len = str.length();
while(j < len){
if(str[i] == '#' && str[i] == str[j]){
cnt++;
i++;
j++;
}
i++;
j++;
}
cout << cnt << endl;
return 0;
}
- 然后接下来是老老实实地敲前序遍历的代码。
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
char data;
struct node *lchild;
struct node *rchild;
}BTree;
BTree *createBTree(){
char c = getchar();
if(c == '#'){
return NULL;
}
else{
BTree *t = (BTree*) malloc(sizeof(BTree));
t->data = c;
t->lchild = createBTree();
t->rchild = createBTree();
return t;
}
}
int cal_BTree(BTree *t){
int n1 = 0, n2 = 0;
if(t->lchild == NULL && t->rchild == NULL){
return 1;
}
if(t->lchild){
n1 = cal_BTree(t->lchild);
}
if(t->rchild){
n2 = cal_BTree(t->rchild);
}
return n1 + n2;
}
int main(int argc, char *argv[]) {
BTree *t = createBTree();
int num = cal_BTree(t);
printf("%d\n", num);
return 0;
}