向前走莫回头❤

【openjudge 计算概论(A)】[编程练习(数组练习)]

1:数组逆序重放

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[110],n;
int main()
{
	int i;
	scanf("%d",&n);
	for(i=1;i<=n;++i) scanf("%d",&a[i]);
	for(i=n;i>0;--i) printf("%d ",a[i]);
	return 0;
}

2:奇数单增序列

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[510],n,tot;
int main()
{
	int i;
	scanf("%d",&n);
	for(i=1;i<=n;++i) 
	 {
	 	int x;
	 	scanf("%d",&x);
	 	if(x%2) a[++tot]=x; 
	 }
	sort(a+1,a+tot+1);
	printf("%d",a[1]);
	for(i=2;i<=tot;++i) printf(",%d",a[i]);
	return 0;
}

3:找最大数序列

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[35][110],n,tot[35];
int maxn,num[35],cnt;
int main()
{
	int i=1;
	scanf("%d",&n);
	while(1)
	{
		scanf("%d",&a[i][++tot[i]]);
		char x=getchar();
		if(x=='\n'&&i==n) break;
		if(x=='\n') i++;
	}
	for(i=1;i<=n;++i)
	 {
	 	for(int j=1;j<=tot[i];++j) 
	 	 {
	 	 	if(a[i][j]>maxn) cnt=0,maxn=a[i][j],num[++cnt]=i;
	 	 	 else if(a[i][j]==maxn) num[++cnt]=i;
		  }
	 }
	printf("%d\n",maxn); 
	cnt=unique(num+1,num+cnt+1)-num-1;
	printf("%d",num[1]);
	for(i=2;i<=cnt;++i) printf(",%d",num[i]);
    return 0;
 } 

4:校门外的树

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
	int l,r,len;
}que[10010];
int d,q;
int tmp(node a,node b)
{
	if(a.l<b.l) return 1;
	if(a.l>b.l) return 0;
	if(a.len>b.len) return 1;
	if(a.len<b.len) return 0;
 } 
int main()
{
	int i,j;
	scanf("%d%d",&d,&q);
	for(i=1;i<=q;++i) scanf("%d%d",&que[i].l,&que[i].r),que[i].len=que[i].r-que[i].l+1;
	sort(que+1,que+q+1,tmp);
	int left=que[1].l,right=que[1].r;
	i=2; d++;
	while(i<=q)
	 {
	 	if(que[i].l<=right) right=max(right,que[i].r);
	 	 else d-=(right-left+1),left=que[i].l,right=que[i].r;
	 	i++;
	 }
	d-=(right-left+1);
	printf("%d\n",d);
	return 0;
}

5:距离排序

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
	int x,y,z;
}a[20];
struct question{
	int left,right;
	double sum;
}ans[210];
int n,cnt;
inline int abss(int x)
{
	if(x>=0) return x;
	 else return -x;
}
int tmp(question p,question q)
{
	return (p.sum>q.sum||p.sum==q.sum&&p.left<q.left||p.sum==q.sum&&p.left==q.left&&p.right<q.right);
 } 
int main()
{
	int i,j;
	scanf("%d",&n);
	for(i=1;i<=n;++i) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
	for(i=1;i<n;++i)
	 for(j=i+1;j<=n;++j)
	  {
	  	ans[++cnt].left=i,ans[cnt].right=j;
	    int x1=abss(a[i].x-a[j].x),y1=abss(a[i].y-a[j].y),z1=abss(a[i].z-a[j].z);
	    ans[cnt].sum=sqrt(x1*x1+y1*y1+z1*z1);
	  }
	sort(ans+1,ans+cnt+1,tmp);
	for(i=1;i<=cnt;++i) 
	 {
	 	int k=ans[i].left,l=ans[i].right;
	 	printf("(%d,%d,%d)-(%d,%d,%d)=%.2lf\n",a[k].x,a[k].y,a[k].z,a[l].x,a[l].y,a[l].z,ans[i].sum);
	 }
	return 0;
}

6:错误探测

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[110][110],n;
int main()
{
	int i,j; 
	while(scanf("%d",&n)==1&&n)
	 {
	 	int sum,t1=0,t2=0;
	 	for(i=1;i<=n;++i) 
	     {
	     	sum=0;
	     	for(j=1;j<=n;++j) scanf("%d",&a[i][j]),sum+=a[i][j];
		    if(!(sum%2)) t1++;
		  } 
	 	for(i=1;i<=n;++i)
	 	 {
	 	 	sum=0;
	 	 	for(j=1;j<=n;++j) sum+=a[j][i];
	 	 	if(!(sum%2)) t2++;
		  }
		if(t1==n&&t2==n) {printf("OK\n"); continue;}
		if(t1==n&&t2==n-1||t1==n-1&&t2==n) {printf("Corrupt\n"); continue;}
		if(t1==n-1&&t2==n-1)
		 {
		 	int s1,n1,s2,n2;
		 	for(i=1;i<=n;++i)
		 	 {
		 	 	s1=0; 
		 	 	for(j=1;j<=n;++j) s1+=a[i][j];
		 	 	if(s1%2) {n1=i; break;}
			  }
			for(i=1;i<=n;++i)
			 {
			 	s2=0;
			 	for(j=1;j<=n;++j) s2+=a[j][i];
			 	if(s2%2) {n2=i; break;}
			 }
			printf("Change bit (%d,%d)\n",n1,n2);
		 }
		else printf("Corrupt\n");
	  } 
	return 0;
}

7:计算鞍点

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[6][6],num[6];
int main()
{
	int i,j;
	for(i=1;i<=5;++i)
	 for(j=1;j<=5;++j)
	  scanf("%d",&a[i][j]);
	for(i=1;i<=5;++i)
	 {
	 	int maxn=0;
	 	for(j=1;j<=5;++j)
	 	 if(a[i][j]>maxn) maxn=a[i][j],num[i]=j;
	  } 
	bool p=0;
	for(i=1;i<=5;++i) 
	 {
	 	int maxn=0x7fffffff,nm;
	 	for(j=1;j<=5;++j)
	 	 if(a[j][i]<maxn) nm=j,maxn=a[j][i];
	 	if(num[nm]==i) printf("%d %d %d\n",nm,i,maxn),p=1;
	 }
	if(!p) printf("not found\n");
	return 0;
 } 

8:求序列中的众数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1001
using namespace std;
struct number
{
    string s;
    int x;
    int t;
    bool operator <(const number& a)const{
        if (x==a.x) return t>a.t;
        return x<a.x;
    }
}a[MAXN];
int top;
int n;
int find(string s)
{
    for (int i=1;i<=top;i++)
        if (a[i].s==s) return i;
    return -1;
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        string s;
        cin>>s;
        if (s[0]!='-'&&s[0]!='+')
        {
            int t=0;
            while (s[t]=='0') s[t]=' ',t++;
            if (s[t]=='0'||s[t]==' ') s="0";
            string temp;
            for (int j=0;j<s.length();j++) if (s[j]!=' ') temp+=s[j];
            s=temp;
            if (s=="") s="0";
        }
        else
        {
            int t=1;
            while (s[t]=='0') s[t]=' ',t++;
            if (s[t]==0||s[t]==' ') 
            {
                s="0";
            }
            else
            {
                string temp;
                if (s[0]!='+') t=0;
                else t=1;
                for (t;t<s.length();t++) 
                    if (s[t]!=' ') temp+=s[t];
                s=temp;
            }
            if (s=="") s="0";
        }
        if (find(s)==-1) {a[++top].s=s;a[top].x=1;a[top].t=i;}
        else a[find(s)].x++;
    }
    sort(a+1,a+top+1);
    if (a[top].x==n)
    {
        cout<<"no"<<endl;
        exit(0);
    }
    cout<<a[top].s<<endl;
}








posted @ 2016-10-20 21:39  lris0-0  阅读(83)  评论(0编辑  收藏  举报
过去的终会化为美满的财富~o( =∩ω∩= )m