#YGAP13. 阅读题集锦(八)

阅读题集锦(八)

#include<bits/stdc++.h>
using namespace std;
int f[1024];
int n,cnt;
double ave;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>f[i];
        ave+=f[i];
    }
    ave/=n;
    for(int i=1;i<=n;i++){
        if(f[i]<ave) cnt++;
    }
    printf("%0.2lf %d",ave,cnt);
    return 0;
}
/*
  输入
  5
  73 76 83 91 77
 */

1.运行结果:{{ input(1) }}

#include<bits/stdc++.h>
using namespace std;
long long n,p,s,x;
int main(){
    scanf("%lld %lld",&n,&x);
    p=1;
    for(int i=1;i<=n;i++){
        p*=x;
        s+=p;
    }
    printf("%lld",s);
    return 0;
}
//输入:15 2

2.运行结果:{{ input(2) }}

#include<bits/stdc++.h>
using namespace std;
const int maxn=100000;
int f[maxn],s[maxn][3];
int main(){
    int n;cin>>n;
    for(int i=1;i<=n;i++) cin>>f[i];
    s[1][1]=1;
    s[1][2]=n;
    int last=2;
    while(last>1){
        last--;
        int head=s[last][1];
        int tail=s[last][2];
        int L=head;
        int R=tail;
        int x=f[head];
        while(L<R){
            while(L<R && f[R]<x) R--;
            if(L<R) f[L++]=f[R];
            while(L<R && f[L]>x) L++;
            if(L<R) f[R--]=f[L];
        }
        f[L]=x;
        if(head<L-1){
            s[last][1]=L+1;
            s[last++][2]=tail;
        }
        if(L+1<tail){
            s[last][1]=L+1;
            s[last++][2]=tail;
        }
    }
    int sum=f[2]-f[1];
    for(int i=3;i<=n;i++){
        sum=sum+f[i]-f[i-1];
    }
    cout<<sum;
    return 0;
}
/*
  第一小问:输入:
  3
  20 10 30
  第二小问:输入:
  10
  40 36 47 29 25 35 22 42 13 58
 */

​​

3.运行结果:{{ input(3) }} 4.运行结果:{{ input(4) }}

#include <iostream>
using namespace std;
void matric(int& A, int& B, int& C, int& D, int n) {
    int a1, b1, c1, d1, a2, b2, c2, d2;
    if (n == 1) {
        A = 0;
        B = 1;
        C = 1;
        D = 1;
        return;
    }
    matric(a1, b1, c1, d1, n / 2);
    a2 = a1 * a1 + b1 * c1;
    b2 = a1 * b1 + b1 * d1;
    c2 = c1 * a1 + d1 * c1;
    d2 = c1 * b1 + d1 * d1;
    if (n % 2 == 1) {
        A = b2;
        B = a2 + b2;
        C = d2;
        D = c2 + d2;
    } else {
        A = a2;
        B = b2;
        C = c2;
        D = d2;
    }
}

int main() {
    int n;
    cin >> n;
    if (n < 3) {
        cout << 1 << endl;
    } else {
        int a, b, c, d;
        matric(a, b, c, d, n - 2);
        cout << c + d << endl;
    }
    return 0;
}
/*
  第一小问:输入:3
  第二小问:输入:7
 */

5.运行结果:{{ input(5) }} 6.运行结果:{{ input(6) }}

#include <iostream>
using namespace std;
int main() {
    int sum=0;
    for(int i=2;i<=20;i++){
        bool prime=true;
        for(int j=2;j<i;j++){
            if(i%j==0){
                prime=false;
                break;
            }
        }
        sum+=prime;
    }
    cout<<sum;
    return 0;
}

7.运行结果:{{ input(7) }}

#include<bits/stdc++.h>
using namespace std;
int f[10]={2,3,5,7,11,13,17,19,23,29};
int main(){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    int n=10;
    a=(f[a]*f[b]-f[c]*f[d]%(n+n))%n;
    b=(f[b]*f[c]-f[d]*f[a]%(n+n))%n;
    c=(f[c]*f[d]-f[a]*f[b]%(n+n))%n;
    d=(f[d]*f[a]-f[b]*f[c]%(n+n))%n;
    printf("%d%d%d%d",a,b,c,d);
    return 0;
}
//输入:2357

8.运行结果:{{ input(8) }}

#include<bits/stdc++.h>
using namespace std;
int a[30];
int x,y,z,n;
int main(){
    scanf("%d %d %d",&x,&y,&z);
    while(z){
        a[++n]=z%10;
        z/=10;
    }
    int sum=0;
    for(int i=n;i>=1;i--) sum=sum*x+a[i];
    cout<<sum<<' ';
    n=0;
    while(sum){
        a[++n]=sum%y;
        sum/=y;
    }
    for(int i=n;i>=1;i--) cout<<a[i];
    return 0;
}
//输入:7 2 126

9.运行结果:{{ input(9) }}

#include<bits/stdc++.h>
using namespace std;
int f[110],a[110];
int cnt,i,j;
int find(int i,int h,int x){
    if(i<3) return 0;
    int m=h+f[i-1]-1;
    cnt++;
    if(x==a[m]) return m;
    if(x<a[m]) return find(i-1,h,x);
    return find(i-2,m+1,x);
}
int main(){
    f[1]=f[2]=1;
    for(i=3;i<=46;i++) f[i]=f[i-1]+f[i-2];
    int n,x,y;
    cin>>n>>x>>y;
    for(i=1;i<=n;i++) a[i]=3*i-1;
    i=1;
    while(f[i]-1<n) i++;
    for(j=n+1;j<=f[i]-1;j++) a[j]=INT_MAX;
    j=find(i,1,x);
    printf("%d%d",j,cnt);
    cnt=0;
    j=find(i,1,y);
    printf("%d%d",j,cnt);
    return 0;
}
//输入:8 14 7

10.运行结果:{{ input(10) }}