我再也不敢用getchar了
查看原帖
我再也不敢用getchar了
305926
qwq_it_is_me楼主2021/7/30 16:10

在本地电脑上的程序,可以输出正确答案

#include<stdio.h>
#include<string.h>

struct node;
typedef struct node node;
typedef node* tree;
struct node{
	char name[100];
	tree c=NULL, sib=NULL;
};
/*
void addsib(tree t, char* n){
	if(t->sib==NULL){
		t->sib = new node;
		strcpy(t->sib->name, n);
	}
	else{
		addsib(t->sib, n);
	}
}*/

void addc(tree t, char* n){
	if(t->c==NULL){
		t->c = new node;
		strcpy(t->c->name,n);
	}
	else{
		tree tmp=t->c;
		t->c=new node;
		t->c->sib=tmp;
		strcpy(t->c->name,n);
	}
}

tree findsib(tree t, char* n){
	if(t->sib==NULL)return NULL;
	if(strcmp(t->sib->name,n)==0) return t->sib;
	else return findsib(t->sib,n);
}

tree findc(tree t, char* n){
	if(t->c==NULL)return NULL;
	if(strcmp(t->c->name,n)==0) return t->c;
	else return findsib(t->c,n);
}

char s[100];

void makdir(int cur, tree t, bool isnew){
	int i =0;
	if(cur==0)getchar();
	while(s[i]=getchar(), s[i]!='/'&&s[i]!='\n'&&s[i]!='\r'){
		i++;
	}
	s[i+1]='\0';
	if(s[i]=='/')s[i]='\0';
	if(s[i]=='\n'){
		if(s[0]=='\n')return;
		else{
			s[i]='\0';
			if(findc(t, s)){
				return;
			}
			else{
				tree tmp=t->c;
				t->c= new node;
				t->c->sib=tmp;
				strcpy(t->c->name, s);
				return;
			}
		}
	}
	if(isnew){
		t->c= new node;
		strcpy(t->c->name, s);
		makdir(cur+1, t->c,true);
		return;
	}

	tree ret=findc(t, s);
	if(ret==NULL){
		addc(t, s);
		makdir(cur+1,t->c,true);
		return;
	}
	else makdir(cur+1,ret,false);
}



tree root;
int n;

int cnt=0;
void count(tree t){
	cnt++;
	//printf("%s;\n",t->name);
	if(t->c!=NULL) count (t->c);
	if(t->sib!=NULL) count (t->sib);
	return;
}


int main(){
	root = new node;
	root->name[0]='R';
	root->name[1]='\0';
	tree tmp;
	scanf("%d", &n);
	getchar();
	for(int i = 0; i<n; i++){

		makdir(0,root,false);
		cnt=0;
		count(root);
		printf("%d\n", cnt-1);
	}
	return 0;
}

在你谷就不行

后来弃用getchar 改了个函数


int n;

int cnt=0;
void count(tree t){
	cnt++;
	//for(int i = 0; i<10; i++)printf("%d ",t->name[i]);
    //printf(";\n");
    //printf("%s;\n", t->name);
	if(t->c!=NULL) {
    //    printf("%s->c->name=", t->name);
        count (t->c);
    }
	if(t->sib!=NULL) {
    //    printf("%s->sib->name=", t->name);
        count (t->sib);
    }
	return;
}

还是scanf安逸

2021/7/30 16:10
加载中...