您的位置:首页 > 其它

折纸问题

2016-03-20 16:54 281 查看
题目描述

请把纸条竖着放在桌⼦上,然后从纸条的下边向上⽅对折,压出折痕后再展 开。此时有1条折痕,突起的⽅向指向纸条的背⾯,这条折痕叫做“下”折痕 ;突起的⽅向指向纸条正⾯的折痕叫做“上”折痕。如果每次都从下边向上⽅ 对折,对折N次。请从上到下计算出所有折痕的⽅向。

给定折的次数n,请返回从上到下的折痕的数组,若为下折痕则对应元素为”down”,若为上折痕则为”up”.

测试样例:

1

返回:[“down”]

class FoldPaper {
public:
string d="down";
string u="up";

vector<string> foldPaper(int n) {
vector<string> result;
while(n--){
bool isDown=true;
vector<string> new_result;
new_result.push_back(d);
isDown=false;
for(int i=0;i<result.size();i++){
new_result.push_back(result[i]);
if(isDown){
new_result.push_back(d);
isDown=false;
}
else{
new_result.push_back(u);
isDown=true;
}

}
result=new_result;
}
return result;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: