1:单词替换
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[110],s1[110],s2[110];
int len,l1,l2;
int main()
{
//freopen("int.txt","w",stdout);
int i,j;
gets(s+1); gets(s1+1); gets(s2+1);
len=strlen(s+1); l1=strlen(s1+1);
s[++len]=' ';
i=1;
while(i<=len)
{
int last=i;
while(s[i]!=' ') ++i;
if(l1==i-last)
{
bool p=1;
for(j=1;j<=l1;++j)
if(s1[j]!=s[last+j-1]) {p=0; break;}
if(p) printf("%s ",s2+1);
else for(j=last;j<=i;++j) printf("%c",s[j]);
}
else for(j=last;j<=i;++j) printf("%c",s[j]);
++i;
}
return 0;
}
2:字符串排序
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
string s[110];
int n,T;
int main()
{
int i;
scanf("%d",&T);
while(T--)
{
scanf("%d\n",&n);
for(i=1;i<=n;++i) getline(cin,s[i]);
sort(s+1,s+n+1);
for(i=1;i<=n;++i) cout<<s[i]<<endl;
printf("\n");
}
return 0;
}
3:带通配符的字符串匹配
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[30],s1[30];
int len,l2;
int main()
{
int i,j;
gets(s);
gets(s1);
len=strlen(s); l2=strlen(s1);
i=0; j=0;
while(i<len&&j<l2)
{
while(i<len&&j<l2&&s[i]!='*'&&s[i]!='?')
if(s[i]!=s1[j]) {printf("not matched\n"); return 0; }
else i++,j++;
while(s[i]=='?'&&i<len&&j<l2) i++,j++;
while((s[i]=='*'||s[i]=='?')&&i<len&&j<l2)
if(s[i]=='*') ++i;
else ++j,++i;
while(s1[j]!=s[i]&&j<l2) ++j;
if(i==len&&j==l2) {printf("matched\n"); return 0;}
if(i==len&&j<l2) {printf("not matched\n"); return 0; }
if(j==l2&&i<len)
{
while(s[i]=='*') ++i;
if(i==len) printf("matched\n");
else printf("not matched\n");
return 0;
}
}
if(i==len&&j>l2) printf("matched\n");
else printf("not matched\n");
return 0;
}
4:用二分法求方程的根
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
double ans,eps;
inline double f(double x)
{
return (2*x*x*x-4*x*x+3*x-6);
}
inline void qsort(double l,double r)
{
double mid;
while(r-l>eps)
{
mid=(l+r)/2;
if(f(l)*f(mid)<=0) r=mid;
else l=mid;
}
mid=(l+r)/2;
printf("%.8lf\n",mid);
}
int main()
{
scanf("%lf",&eps);
if(eps==0) {printf("2.00000000\n"); return 0;}
qsort(-10.0,10.0);
return 0;
}
5:请开发一个自上而下的模块化程序,用于完成以下任务
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
int a[510],b[510];
inline void read()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i) scanf("%d",&a[i]);
for(int i=1;i<=m;++i) scanf("%d",&b[i]);
}
inline void write()
{
for(int i=1;i<=n;++i) printf("%d ",a[i]);
for(int i=1;i<=m;++i) printf("%d ",b[i]);
}
int main()
{
read();
sort(a+1,a+n+1);
sort(b+1,b+m+1);
write();
return 0;
}