作业以及课上习题笔记
# P1044 [NOIP2003 普及组] 栈
栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表。
栈有两种最重要的操作,即 pop(从栈顶弹出一个元素)和 push(将一个元素进栈)。
栈的重要性不言自明,任何一门数据结构的课程都会介绍栈。宁宁同学在复习栈的基本概念时,想到了一个书上没有讲过的问题,而他自己无法给出答案,所以需要你的帮忙。
题目描述
宁宁考虑的是这样一个问题:一个操作数序列,1,2,…,n(图示为 1 到 3 的情况),栈 A 的深度大于 n。
现在可以进行两种操作,
- 将一个数,从操作数序列的头端移到栈的头端(对应数据结构栈的 push 操作)
- 将一个数,从栈的头端移到输出序列的尾端(对应数据结构栈的 pop 操作)
使用这两种操作,由一个操作数序列就可以得到一系列的输出序列,下图所示为由 1 2 3 生成序列 2 3 1 的过程。
(原始状态如上图所示)
你的程序将对给定的 n,计算并输出由操作数序列 1,2,…,n 经过操作可能得到的输出序列的总数。
我的答案
#include <iostream>
#include <cstdio>
using namespace std;
int a,b[10000];
int main(){
b[0]=1;
b[1]=1;
scanf("%d",&a);
for(int c=2;c<=a;c++){
for(int d=0;d<c;d++){
b[c]+=b[d]*b[c-d-1];
}
}
printf("%d",b[a]);
return 0;
}
不难,可以找到规律。
P1075 [NOIP2012 普及组] 质因数分解
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for (int i=2;i<=n;i++)
if (n%i==0) {
cout<< n/i;
break;
}
return 0;
}
P1152 欢乐的跳
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int f[200000000];
int main() {
int n;
scanf("%d",&n) ;
int a[n];
for (int i=0; i<n; ++i)
scanf("%d",&a[i]) ;
for (int i=1; i<n; ++i)
f[abs (a[i] - a[i-1])] = 1;
for (int i=1; i<n; ++i)
if (f[i] == 0) {
printf("Not jolly\n") ;
return 0;
}
printf("Jolly\n") ;
return 0;
}
P1179 [NOIP2010 普及组] 数字统计
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int l,r,temp,res;
cin>>l>>r;
for(int i=l;i<=r;i++){
int temp=i;
while(temp!=0){
if(temp%10==2)
res++;
temp/=10; }}
cout<<res;
return 0;
}
P1423 小玉在游泳
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a=1;
double b=0,c=2;
double x;
cin>>x;
while(b+c<x){
a++;
b+=c;
c*=0.98;
}
cout<<a<<endl;
return 0;
}
P1534 不高兴的津津(升级版)
#include <iostream>
#include <cstdio>
using namespace std;
int a[10001];
int main()
{
int n,x,y,m=0,b=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
m=m+x+y-8;
b+=m;
}
printf("%d\n",b);
return 0;
}
P1567 统计天数
#include <iostream>
#include <cstdio>
using namespace std;
int a[1000000];
int main(){
int af=0,b=1,c=0;
scanf("%d",&af);
for(int i=1;i<=af;i++){
scanf("%d",&a[i]);
if(a[i] > a[i-1]) {
b++;
c=max(c,b);
}
else b=1;
}
cout<<c<<endl;
return 0;
}
P3954 [NOIP2017 普及组] 成绩
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char** argv)
{
int a=0,b=0,c=0;
scanf("%d%d%d",&a,&b,&c);
cout<<a*0.2+b*0.3+c*0.5;
return 0;
}
P2669 [NOIP2015 普及组] 金币

浙公网安备 33010602011771号