1.斐波那契数列,变量版,变量覆盖
a = 1, b = 1;
for(int i = 1; i <=n; i++){
cout<<a+b<<” ”
a = b;
b = c
}
2.进击的奶牛前置版:变量覆盖
cnt=1;pre=a[1];
for(int i=2;i<=n;i++){
if(a[i]-pre>=mid){
cnt++,pre=a[i];
}
}
3.分段数列:变量版,数组版,两种方法,类似小马过河,一种直接累加,超了重新赋值
#include<stdc++.h>//bits/
using namespace std;
int n,l,sum,cnt = 1,a[100005];
int main(){
cin>>n>>l;
for(int i = 1; i <= n; i++){
cin>>a[i];
if(sum + a[i] <= l){
sum = sum + a[i];
}
else{
sum = a[i];
cnt++;
}
}
cout<<cnt<<endl;
return 0;
}
/*
7 5
3 8 9 12 13 14 18
*/
4.数组的双向关系:
#include<stdc++.h>//bits/
using namespace std;
int n,a[100005],b[100005];
int main(){
cin>>n;
for(int i = 1; i <= n; i++){
cin>>a[i];
b[a[i]] = i;
}
for(int i = 1; i <= n; i++){
cout<<b[i]<<" ";
}
return 0;
}
/*
输入:
5
3 1 4 5 2
输出
2 5 1 3 4
*/