您的位置:首页 > 其它

HDOJ 1020 1021 1022 1031 1032(water~)

2014-07-20 23:37 218 查看
  今日继续水题之旅,共ac5个,下午的时候在1022 Train Problem I那里耽误时间较长,简要总结下。

1021 

[align=left]Problem Description[/align]
There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).

 

[align=left]Input[/align]
Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000).

 

[align=left]Output[/align]
Print the word "yes" if 3 divide evenly into F(n).

Print the word "no" if not.

这里,咋一看就想到了数论的知识,直接手算了余数和周期,简单一循环就OK了,数学知识能帮大忙。

1031   Design T-Shirt

可以不用动态建立二维数组,但是如果用,要用对格式。

<span style="font-family:Verdana;font-size:18px;">int **a=new int *
;   //动态建立a
[m]
for(int i=0;i<m;i++)
{
a[i]=new int [m];
}</span>


补充利用vector建立动态数组的方法,比上边那个简洁。

<span style="font-family:Verdana;font-size:18px;">vector<vector<int> >arry(m,vector<int>(n))
</span>

注意<int> 后有一空格,头文件要包含vector。
1022   铁轨问题

给定栈的输入方式,判断能否以给定的方式输出,若能,给出方法(什么时候进,什么时候出)。自己想了想,参考了下别人的代码,核心如下。

<span style="font-family:Verdana;font-size:18px;">while(k<n)
{
if(j[i]==c[k])
{
a[f++]=1;
a[f++]=0;
i++;k++;
}
else if(!st.empty()&&c[k]==st.top())
{
a[f++]=0;
st.pop();
k++;
}
else if(i<n)
{
a[f++]=1;
st.push(j[i++]);
}
else
{
ok=0;
break;
}
}</span>


按顺序来,输入进栈出栈顺序后,分别给予从0开始的指针,以出栈顺序作为根据,只需判断三种情况:

①相同,操作为进栈出栈,两指针都后移一位。

②栈非空且出栈顺序指针指向的那个元素正好为栈顶元素,出栈

③如果上述两种情况都没遇到,那么就该压栈了。

a[ ]这个数组顺便记录了何时进何时出,以01为标志,方便输出。

还有很长的路要走,每天尽量多ac一道题,加油!少年!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: