6-題解

基礎級-6-(1-5)題解 - (飆程式網分站)

---------------------------------------------------------

6-1

---------------------------------------------------------

#include <cstdio>

#include <cstdlib>

#include <cstring>

using namespace std;

int x[105]={0};

int main()

{

int a,b,i,ans=0,n;

scanf("%d",&n);

while(n--)

{

scanf("%d %d",&a,&b);

for(i=a; i<=b; i++) x[i]++;

}

for(i=1; i<=100; i++)

if(x[i]>ans) ans=x[i];

printf("%d\n",ans);

return 0 ;

}

---------------------------------------------------------

6-2

---------------------------------------------------------

#include <cstdio>

int main()

{

int n,i,a[100]={0};

scanf("%d",&n);

for(i=0;i<n;i++) scanf("%d",&a[i]);

for(i=n-1;i>=0;i--) printf("%d ",a[i]);

return 0;

}

---------------------------------------------------------

6-3

---------------------------------------------------------

#include<stdio.h>

#include<algorithm>

int main(){

long long i,x;

while(scanf("%lld",&x)!=EOF)

{

long long n[x];

for(i=0;i<x;i++)scanf("%lld",&n[i]);

std::sort(n,n+x);

for(i=0;i<x;i++)if(n[i]!=i+1)break;

printf(i==x?"Yes\n":"No\n");

}

return 0;

}

---------------------------------------------------------

6-4

---------------------------------------------------------

#include<stdio.h>

int main(){

int n,i,b,c;

while(scanf("%d",&n)!=EOF)

{

int x[n];

b=0;

c=0;

for(i=0;i<n;i++)scanf("%d",&x[i]);

for(i=1;i<=n;i++)

{

if(b>=i)

{

if(x[i-1]!=b-i+c)break;

}

else if(x[i-1]!=i)

{

if(b==0&&x[i-1]>i){b=x[i-1];c=i;}

else break;

}

}

printf(i==n+1?"Yes\n":"No\n");

}

return 0;

}

---------------------------------------------------------

6-5

---------------------------------------------------------

#include <stdio.h>

int a[110];

int check(int p,int q)

{

int i,used[110]={0},len=q-p+1;

for(i=p; i<=q; i++) used[a[i]]=1;

for(i=1; i<=len; i++)

if(!used[i]) return 0;

return 1;

}

int main()

{

int n,i,j,count=0;

scanf("%d",&n);

for(i=0; i<n; i++) scanf("%d",&a[i]);

for(i=0; i<n; i++)

for(j=i; j<n; j++)

{

if(check(i,j)) count++;

}

printf("%d\n",count);

return 0;

}

=========================================================