pathy编程和python(python)
数据结构与算法的实验内容
第一题用python写的,第二题用c++写的。
在测试通过
#?第一题
def?main():
????text?=?raw_input()
????n?=?int(text[0:text.find('?')])
????m?=?int(text[text.find('?')+1:])
????root?=?build(n)
????check(root,?m)
def?build(n):
????raw_name?=?raw_input()
????name?=?raw_name.strip()
????root?=?{}
????root['name']?=?name
????root['layer']?=?0
????root['child']?=?{}
????root['sibling']?=?{}
????pre_node?=?root
????for?i?in?range(1,n):
????????raw_name?=?raw_input()
????????name?=?raw_name.strip()
????????cur_layer?=?raw_name.index(name)/2
????????cur_node?=?{}
????????cur_node['name']?=?name
????????cur_node['layer']?=?cur_layer
????????cur_node['child']?=?{}
????????cur_node['sibling']?=?{}
????????if?cur_layer?==?pre_node['layer']:
????????????pre_node['sibling']?=?cur_node????????????
????????elif?cur_layer?==?pre_node['layer']+1:
????????????pre_node['child']?=?cur_node
????????elif?cur_layer?==?pre_node['layer']+1:
????????????print?'error!\n'
????????????break
????????elif?cur_layer??pre_node['layer']:
????????????path?=?getpath(root,?pre_node['name'])
????????????pre_subling?=?{}
????????????for?p?in?path:
????????????????if?p['layer']??cur_layer:
????????????????????break
????????????????else:
????????????????????pre_subling?=?p
????????????pre_subling['sibling']?=?cur_node
????????pre_node?=?cur_node
????return?root
def?check(root,?m):
????#X?is?a?child?of?Y
????#X?is?the?parent?of?Y
????#X?is?a?sibling?of?Y
????#X?is?a?descendant?of?Y
????#X?is?an?ancestor?of?Y
????for?i?in?range(0,m):
????????text?=?raw_input()
????????X?=?text[0:text.find('?')]
????????Y?=?text[text.rfind('?')+1:]
????????if?'child'?in?text:
????????????path?=?getpath(root,?X)
????????????if?len(path)=2?and?path[-2]['name']?==?Y:
????????????????print?True
????????????else:
????????????????print?False
????????if?'parent'?in?text:
????????????path?=?getpath(root,?Y)
????????????if?len(path)=2?and?path[-2]['name']?==?X:
????????????????print?True
????????????else:
????????????????print?False????????
????????if?'sibling'?in?text:
????????????pathX?=?getpath(root,?X)
????????????pathY?=?getpath(root,?Y)
????????????if?len(pathX)=2?and?len(pathY)=2?and?pathX[-2]['name']?==?pathY[-2]['name']:
????????????????print?True
????????????else:
????????????????print?False??
????????if?'descendant'?in?text:
????????????path?=?getpath(root,?X)
????????????for?p?in?path:
????????????????if?p['name']?==?Y:
????????????????????break
????????????if?p['name']?==?Y:
????????????????print?True
????????????else:
????????????????print?False
????????if?'ancestor'?in?text:
????????????path?=?getpath(root,?Y)
????????????for?p?in?path:
????????????????if?p['name']?==?X:
????????????????????break
????????????if?p['name']?==?X:
????????????????print?True
????????????else:
????????????????print?False????????????????
????????????????
def?getpath(root,?node_name):????
????path?=?[root]
????next_node?=?root['child']
????while?True:
????????while?next_node?!=?{}:
????????????path.append(next_node)
????????????if?next_node['name']?==?node_name:
????????????????return?path
????????????next_node?=?next_node['child']
????????????
????????next_node?=?path.pop()
????????if?path?==?[]:
????????????return?path
????????else:
????????????next_node?=?next_node['sibling']
????????????
if?__name__?==?"__main__":
????main()
//?第二题
#include?iostream
#include?algorithm
using?namespace?std;
const?int?MAX?=?224;
typedef?struct
{
????int?index;
????double?no;
????int?range;
}ELEM;
int?cmp_des(?const?ELEM?a,?const?ELEM?b?){
????return?a.nob.no;?//?降序
}
int?cmp_aes(?const?ELEM?a,?const?ELEM?b?){
????return?a.indexb.index;?//?升序
}
int?main()
{
????int?n,m;
????cinnm;
????double?data[5][MAX];
????int?paiming[5][MAX];
????ELEM?tmp[MAX];
????int?country[MAX];
????int?i,j;
????for?(i=0;in;i++)
????{
????????cindata[1][i]data[2][i]data[0][i];
????????data[3][i]?=?data[1][i]/data[0][i];
????????data[4][i]?=?data[2][i]/data[0][i];
????}
????for?(i=0;im;i++)
????????cincountry[i];
????
????for?(i=1;i=4;i++)
????{
????????for?(j=0;jn;j++)
????????{
????????????tmp[j].index?=?j;
????????????tmp[j].no?=?data[i][j];
????????}
????????sort(tmp,tmp+n,cmp_des);
????????for?(j=0;jn;j++)
????????{
????????????if?(j0??tmp[j].no==tmp[j-1].no)
????????????????tmp[j].range?=?tmp[j-1].range;
????????????else
????????????????tmp[j].range?=?j+1;
????????}
????????sort(tmp,tmp+n,cmp_aes);
????????for?(j=0;jn;j++)
????????{
????????????paiming[i][j]?=?tmp[j].range;
????????}
????}
????int?top_12,top_34,top_1234;
????for?(i=0;im;i++)
????{
????????top_12?=?paiming[1][country[i]]=paiming[2][country[i]]?1:2;
????????top_34?=?paiming[3][country[i]]=paiming[4][country[i]]?3:4;
????????top_1234?=?paiming[top_12][country[i]]=paiming[top_34][country[i]]?top_12:top_34;
????????printf("%d:%d",(int)paiming[top_1234][country[i]],(int)top_1234);
????????if?(im-1)
????????????printf("?");
????}
????return?0;
}
pathy编程和c需要哪个更适合未来发展?
pathy编程和c语言都要学的,c语言是基础,大厦想要牢靠,c必须要精通呢,可以去电脑学校看看
浙大远程教育数据结构与算法13秋实验代码
我用python和c++写的,如果你不想要python的,请告知。
#?表达式转换
def?main():
????text?=?raw_input()
????root?=?build(text)
????litx?=?[]
????travel(root,?litx)
????rslt?=?litx[0]
????for?i?in?range(1,len(litx)):
????????rslt?=?rslt?+?'?'?+?litx[i]
????print?rslt
def?build(text):
????left?=?readnode(text);
????if?left['text']?==?'':
????????return?left['node']
????text?=?left['text']
????right?=?readnode(text[1:])
????root?=?{}
????root['value']?=?text[0]
????root['left']?=?left['node']
????root['right']?=?right['node']
????text?=?right['text']
????????
????while?text?!=?'':
????????left?=?root
????????right?=?readnode(text[1:])
????????root?=?{}
????????root['value']?=?text[0]
????????root['right']?=?right['node']
????????if?signlevel(left['value'])?=?signlevel(text[0]):
????????????root['left']?=?left
????????else:
????????????root['left']?=?left['right']
????????????left['right']?=?root
????????????root?=?left
????????text?=?right['text']
????????????
????return?root
def?travel(root,?litx):
????if?root['left']?!=?{}:
????????travel(root['left'],?litx)
????if?root['right']?!=?{}:
????????travel(root['right'],?litx)
????litx.append(root['value'])
?????????????????????
def?readnode(text):
????#?print?'---------------------------readnode'
????#?print?text
????node?=?{}
????if?text[0]?==?'(':
????????index?=?readbrace(text)
????????node?=?build(text[1:index])
????else:
????????index?=?readnumber(text)
????????if?text[0]?==?'+':
????????????node['value']?=?text[1:index+1]
????????else:
????????????node['value']?=?text[0:index+1]
????????node['left']?=?{}
????????node['right']?=?{}
????#?print?node
????#?print?'++++++++++++++++++++++++++readnode'
????return?{'node':node,'text':text[index+1:]}
????
def?readnumber(text):
????index?=?0
????for?i?in?range(1,len(text)):
????????if?text[i]?in?['+',?'-',?'*',?'/']:
????????????break
????????index?=?i
????return?index
def?readbrace(text):
????count?=?1
????i?=?0
????while?count??0:
????????i?=?i+1
????????if?text[i]?==?'(':
????????????count?=?count+1;
????????if?text[i]?==?')':
????????????count?=?count-1;
????return?i
def?signlevel(sign):
????if?sign?in?['+',?'-']:
????????return?1
????else:
????????return?2
if?__name__?==?"__main__":
????main()
????
#?家谱处理
def?main():
????text?=?raw_input()
????n?=?int(text[0:text.find('?')])
????m?=?int(text[text.find('?')+1:])
????root?=?build(n)
????check(root,?m)
def?build(n):
????raw_name?=?raw_input()
????name?=?raw_name.strip()
????root?=?{}
????root['name']?=?name
????root['layer']?=?0
????root['child']?=?{}
????root['sibling']?=?{}
????pre_node?=?root
????for?i?in?range(1,n):
????????raw_name?=?raw_input()
????????name?=?raw_name.strip()
????????cur_layer?=?raw_name.index(name)/2
????????cur_node?=?{}
????????cur_node['name']?=?name
????????cur_node['layer']?=?cur_layer
????????cur_node['child']?=?{}
????????cur_node['sibling']?=?{}
????????if?cur_layer?==?pre_node['layer']:
????????????pre_node['sibling']?=?cur_node????????????
????????elif?cur_layer?==?pre_node['layer']+1:
????????????pre_node['child']?=?cur_node
????????elif?cur_layer?==?pre_node['layer']+1:
????????????print?'error!\n'
????????????break
????????elif?cur_layer??pre_node['layer']:
????????????path?=?getpath(root,?pre_node['name'])
????????????pre_subling?=?{}
????????????for?p?in?path:
????????????????if?p['layer']??cur_layer:
????????????????????break
????????????????else:
????????????????????pre_subling?=?p
????????????pre_subling['sibling']?=?cur_node
????????pre_node?=?cur_node
????return?root
def?check(root,?m):
????#X?is?a?child?of?Y
????#X?is?the?parent?of?Y
????#X?is?a?sibling?of?Y
????#X?is?a?descendant?of?Y
????#X?is?an?ancestor?of?Y
????for?i?in?range(0,m):
????????text?=?raw_input()
????????X?=?text[0:text.find('?')]
????????Y?=?text[text.rfind('?')+1:]
????????if?'child'?in?text:
????????????path?=?getpath(root,?X)
????????????if?len(path)=2?and?path[-2]['name']?==?Y:
????????????????print?True
????????????else:
????????????????print?False
????????if?'parent'?in?text:
????????????path?=?getpath(root,?Y)
????????????if?len(path)=2?and?path[-2]['name']?==?X:
????????????????print?True
????????????else:
????????????????print?False????????
????????if?'sibling'?in?text:
????????????pathX?=?getpath(root,?X)
????????????pathY?=?getpath(root,?Y)
????????????if?len(pathX)=2?and?len(pathY)=2?and?pathX[-2]['name']?==?pathY[-2]['name']:
????????????????print?True
????????????else:
????????????????print?False??
????????if?'descendant'?in?text:
????????????path?=?getpath(root,?X)
????????????for?p?in?path:
????????????????if?p['name']?==?Y:
????????????????????break
????????????if?p['name']?==?Y:
????????????????print?True
????????????else:
????????????????print?False
????????if?'ancestor'?in?text:
????????????path?=?getpath(root,?Y)
????????????for?p?in?path:
????????????????if?p['name']?==?X:
????????????????????break
????????????if?p['name']?==?X:
????????????????print?True
????????????else:
????????????????print?False????????????????
????????????????
def?getpath(root,?node_name):????
????path?=?[root]
????next_node?=?root['child']
????while?True:
????????while?next_node?!=?{}:
????????????path.append(next_node)
????????????if?next_node['name']?==?node_name:
????????????????return?path
????????????next_node?=?next_node['child']
????????????
????????next_node?=?path.pop()
????????if?path?==?[]:
????????????return?path
????????else:
????????????next_node?=?next_node['sibling']
????????????
if?__name__?==?"__main__":
????main()
//?奥运排行榜
#include?iostream
#include?algorithm
using?namespace?std;
const?int?MAX?=?224;
typedef?struct
{
????int?index;
????double?no;
????int?range;
}ELEM;
int?cmp_des(?const?ELEM?a,?const?ELEM?b?){
????return?a.nob.no;?//?降序
}
int?cmp_aes(?const?ELEM?a,?const?ELEM?b?){
????return?a.indexb.index;?//?升序
}
int?main()
{
????int?n,m;
????cinnm;
????double?data[5][MAX];
????int?paiming[5][MAX];
????ELEM?tmp[MAX];
????int?country[MAX];
????int?i,j;
????for?(i=0;in;i++)
????{
????????cindata[1][i]data[2][i]data[0][i];
????????data[3][i]?=?data[1][i]/data[0][i];
????????data[4][i]?=?data[2][i]/data[0][i];
????}
????for?(i=0;im;i++)
????????cincountry[i];
????
????for?(i=1;i=4;i++)
????{
????????for?(j=0;jn;j++)
????????{
????????????tmp[j].index?=?j;
????????????tmp[j].no?=?data[i][j];
????????}
????????sort(tmp,tmp+n,cmp_des);
????????for?(j=0;jn;j++)
????????{
????????????if?(j0??tmp[j].no==tmp[j-1].no)
????????????????tmp[j].range?=?tmp[j-1].range;
????????????else
????????????????tmp[j].range?=?j+1;
????????}
????????sort(tmp,tmp+n,cmp_aes);
????????for?(j=0;jn;j++)
????????{
????????????paiming[i][j]?=?tmp[j].range;
????????}
????}
????int?top_12,top_34,top_1234;
????for?(i=0;im;i++)
????{
????????top_12?=?paiming[1][country[i]]=paiming[2][country[i]]?1:2;
????????top_34?=?paiming[3][country[i]]=paiming[4][country[i]]?3:4;
????????top_1234?=?paiming[top_12][country[i]]=paiming[top_34][country[i]]?top_12:top_34;
????????printf("%d:%d",(int)paiming[top_1234][country[i]],(int)top_1234);
????????if?(im-1)
????????????printf("?");
????}
????return?0;
}
Java和pathy哪个好
题主你好!
没有接触过pathy,网上也没有搜到更多相关的信息,所以不太清楚,就说说自己熟悉的Java吧
个人觉得Java还是挺好的,入门来说,也不是很难,主要是编程思维的理解,而且用的人多,有问题查起来也方便,建议Java