向前走莫回头❤

【openjudge 计算概论(A)】[ 函数与字符串练习(2)]

1:单词翻转

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
char s[510];
int len;
int main()
{
  int i,last=0;
  gets(s);
  len=strlen(s);
  for (i=0;i<len;++i)
   if (s[i]==' ')
    {
    	for (int j=i-1;j>=last;j--)
    	 printf("%c",s[j]);
    	printf(" ");
    	last=i+1;
	}
  for (i=len-1;i>=last;i--)
   printf("%c",s[i]);
  return 0;
}
2:DNA排序

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct fnd{
	char s[110];
	int tm,num,nm;
}a[110];
inline int tmp(fnd x,fnd y)
{
 if (x.tm<y.tm) return 1;
 if (x.tm>y.tm) return 0;
 if (x.nm<y.nm) return 1;
 if (x.nm>y.nm) return 0;
}
int n,m;
int main()
{
 int i,j,t;
 scanf("%d%d",&n,&m);
 for(t=1;t<=m;++t)
  {
   cin>>a[t].s;
   for (i=0;i<n;++i)
    for (j=i+1;j<n;++j)
     if (a[t].s[i]>a[t].s[j]) a[t].tm++;
   a[t].num=t; a[t].nm=t;
  } 
  sort(a+1,a+m+1,tmp);
  for (i=1;i<=m;i++)
   printf("%s\n",a[i].s);
  return 0;
}

3:角谷猜想

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int x;
void pract(int x)
{
	if(x==1)
	  {printf("End\n"); return;}
	if (x%2) 
	   {int a=x*3+1; printf("%d*3+1=%d\n",x,a); pract(a);}
	 else 
	   {int a=x/2; printf("%d/2=%d\n",x,a); pract(a);}
	return;
}
int main()
{
 scanf("%d",&x);
 pract(x);
 return 0;
}

4:排队游戏

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct chuan{
	int x,op;
}a[110];
int top,len;
char s[110],c;
int main()
{
  int i;
  scanf("%s",s);
  len=strlen(s);
  c=s[0];
  for (i=0;i<len;++i)
   {
   	if (s[i]==c) a[++top].x=i,a[top].op=1;
   	 else
   	  {
   	  	if(a[top].op==1) printf("%d %d\n",a[top--].x,i);
   	  	 else a[++top].op=2,a[top].x=i;
		 }
   }
   return 0;
}

5:扩号匹配问题

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int d[110],t,h[110],t1,len;
char s[110];
bool p[110];
inline void init1(int x)
{
	d[++t]=x;
}
inline void init(int x)
{
	h[++t1]=x;
}
inline void pop()
{ 
  t--;
}
int main()
{
  while(scanf("%s",s)==1)
   {
     memset(d,0,sizeof(d));
     memset(h,0,sizeof(h));
     memset(p,0,sizeof(p));
	 len=strlen(s); t=0; t1=0;
     for (int i=0;i<len;++i)
      {
      	if (s[i]=='(') init1(i+1);
      	if (s[i]==')'&&t) pop();
      	 else if (s[i]==')'&&!t) init(i+1);
	  } 
     cout<<s<<endl;
     for(int i=t+1;i<=t+t1;++i)
        d[i]=h[i-t],p[h[i-t]]=1;
     t+=t1;
     sort(d+1,d+t+1);
     int last=0;
     for(int i=1;i<=t;++i)
       {
	    for (int j=1;j<d[i]-last;++j)
           printf(" "); 
        last=d[i];
        if (p[d[i]]) printf("?");
         else printf("$");
	   }
	 printf("\n");
    }
   return 0;
}




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