博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3786 寻找直系亲属
阅读量:6976 次
发布时间:2019-06-27

本文共 1949 字,大约阅读时间需要 6 分钟。

题目地址: 

题目思路: 我们用p[i]=j 来表示 i 的孩子是j (因为一个parent只有一个孩子,但是一个孩子有两个parent不好形成对应)

然后初始化为-1,这样如果遇到父母不清楚的(‘-’) 还是-1,一遇到就break掉,把count还原为0;  如果是家族树上存在的两个结点,那么一定可以通过这样知道他们相隔的代数,于是就可以知道相应的称谓  一堆if-else  比较考基本功

代码:

#include
#include
#include
using namespace std;int p[26];/*int chartoint(char ch){ if(ch=='0') return 0; else if(ch=='1') return 1; else if(ch=='2') return 2; else if(ch=='3') return 3; else if(ch=='4') return 4; else if(ch=='5') return 5; else if(ch=='6') return 6; else if(ch=='7') return 7; else if(ch=='8') return 8; else if(ch=='9') return 9; else if(ch=='-') return -1;}*/int main(){ int n,m; while(cin>>n>>m) { if(n==0&&m==0) break; memset(p,-1,sizeof(p)); char c; char p1; char p2; string s; for(int i=0;i
>s; c=s[0]; p1=s[1]; p2=s[2]; if(p1!='-') p[p1-'A']=c-'A'; if(p2!='-') p[p2-'A']=c-'A'; } char c1,c2; for(int i=0;i
>s; c1=s[0]; c2=s[1]; int count1=0,count2=0; int target1=c1-'A'; int target2=c2-'A'; int cur=target1; while(cur!=target2) { cur=p[cur]; if(cur==-1) { count1=0; break; } else count1++; } if(count1==0) { cur=target2; while(cur!=target1) { cur=p[cur]; if(cur==-1) { count2=0; break; } else count2++; } } if(count1==0&&count2==0) { cout<<'-'<
=3) { cout<<"great-"; count1--; } cout<<"grandparent"<
=3) { cout<<"great-"; count2--; } cout<<"grandchild"<

转载于:https://www.cnblogs.com/814jingqi/p/3310426.html

你可能感兴趣的文章
Python OpenCV学习笔记之:图像读取,显示及保存
查看>>
计算机职业目标
查看>>
2月国内搜索市场:360继续上升 百度下降0.62%
查看>>
HTML样式offset[Direction] 和 style.[direction]的区别
查看>>
使用memcache做web缓存
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
华胜天成ivcs云系统初体验2
查看>>
MASQUERADE --random 端口不随机
查看>>
阿里云 Aliplayer高级功能介绍(二):缩略图
查看>>
从1.5K到18K 一个程序员的5年成长之路(二)
查看>>
从HelloWorld看Knative Serving代码实现
查看>>
制作一个简单的linux
查看>>
【ZooKeeper Notes 14】数据模型
查看>>
Expect自动化控制简单介绍
查看>>
我的友情链接
查看>>
Vmware虚拟机的复制后无法使用的问题和解决
查看>>
好程序员web前端技术分享媒体查询
查看>>
开博前的话
查看>>
【老孙随笔】注意啦,精神集中点儿!
查看>>