题目描述:
输入10个整数,彼此以空格分隔。重新排序以后输出(也按空格分隔),要求:
1.先输出其中的奇数,并按从大到小排列;
2.然后输出其中的偶数,并按从小到大排列。
输入:
任意排序的10个整数(0~100),彼此以空格分隔。
输出:
可能有多组测试数据,对于每组数据,按照要求排序后输出,由空格分隔。
样例输入:
4 7 3 13 11 12 0 47 34 98
样例输出:
47 13 11 7 3 0 4 12 34 98
提示:
1. 测试数据可能有很多组,请使用while(cin>>a[0]>>a[1]>>...>>a[9])类似的做法来实现;
2. 输入数据随机,有可能相等。
源代码:
#include <stdio.h>
void Sort1(int a[],int n) //(奇数)递减排序
{
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]<a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
void Sort2(int a[],int n) //(偶数)递增排序
{
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
int main()
{
int i,j,k,a[15];
int ji[15],ou[15];
while(scanf("%d",&a[0])!=EOF)
{
for(i=1;i<10;i++)
scanf("%d",&a[i]);
j=0,k=0;
for(i=0;i<10;i++)
{
if(a[i]%2!=0)
ji[j++]=a[i];
else
ou[k++]=a[i];
}
Sort1(ji,j);
Sort2(ou,k);
for(i=0;i<j;i++)
a[i]=ji[i];
for(i=j;i<10;i++)
a[i]=ou[i-j];
for(i=0;i<9;i++)
printf("%d ",a[i]);
printf("%d\n",a[9]);
}
return 0;
}
程序截图:
2. (2006年上海交通大学计算机研究生机试真题)String Matching
题目描述:
Finding all occurrences of a pattern in a text is a problem that arises frequently in text-editing programs.
Typically,the text is a document being edited,and the pattern searched for is a particular word supplied by the user.
We assume that the text is an array T[1..n] of length n and that the pattern is an array P[1..m] of length m<=n.We further assume that the elements of P and T are all alphabets(∑={a,b...,z}).The character arrays P and T are often called strings of characters.
We say that pattern P occurs with shift s in the text T if 0<=s<=n and T[s+1..s+m] = P[1..m](that is if T[s+j]=P[j],for 1<=j<=m).
If P occurs with shift s in T,then we call s a valid shift;otherwise,we calls a invalid shift.
Your task is to calculate the number of vald shifts for the given text T and p attern P.
输入:
For each case, there are two strings T and P on a line,separated by a single space.You may assume both the length of T and P will not exceed 10^6.
输出:
You should output a number on a separate line,which indicates the number of valid shifts for the given text T and pattern P.
样例输入:
abababab abab
样例输出:
3
【分析】串的模式匹配:求模式串P在主串T中的出现次数
源代码:
#include <stdio.h>
#include <string.h>
char T[1000001],P[1000001];
int KMP(char s[],char t[])
{
int i=0,j=0;
int count=0;
while(s[i]!='\0')
{
if(s[i]==t[j])
{
i++;
j++;
}
else
{
i=i-j+1;
j=0;
}
if(j==strlen(t))
{
count++;
i=i-j+1;
j=0;
}
}
return count;
}
int main()
{
while(scanf("%s %s",T,P)!=EOF)
printf("%d\n",KMP(T,P));
return 0;
}
程序截图:
※3. (2010年北京邮电大学计算机研究生机试真题)C翻转
题目描述:
首先输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作数据为以x y为左上角的那几个数据。
操作类型有四种:
1 2 表示:90度,顺时针,翻转4个数
1 3 表示:90度,顺时针,翻转9个数
2 2 表示:90度,逆时针,翻转4个数
2 3 表示:90度,逆时针,翻转9个数
输入:
输入有多组数据。
每组输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作数据为以x y为左上角的那几个数据。
输出:
输出翻转后的数组。
样例输入:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
1 3 1 1
样例输出:
11 6 1 4 5
12 7 2 9 10
13 8 3 14 15
16 17 18 19 20
21 22 23 24 25
源代码:
#include <stdio.h>
#define maxn 6
#define INF 2100000000
void Rotate_shun_90(int a[][maxn],int b[][maxn],int n,int x,int y)
{
int i,j;
int temp;
for(i=x;i<x+n;i++)
{
for(j=y;j<y+n;j++)
b[j][n-i-1]=a[i][j];
}
}
void Rotate_ni_90(int a[][maxn],int b[][maxn],int n,int x,int y)
{
int i,j;
int temp;
for(i=x;i<x+n;i++)
{
for(j=y;j<y+n;j++)
b[n-j-1][i]=a[i][j];
}
}
void Replace(int a[][maxn],int b[][maxn])
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(b[i][j]!=INF)
a[i][j]=b[i][j];
}
}
}
int main()
{
int a[maxn][maxn],b[maxn][maxn];
int i,j;
int k1,k2,x,y;
while(scanf("%d %d %d %d %d",&a[0][0],&a[0][1],&a[0][2],&a[0][3],&a[0][4])!=EOF)
{
for(i=0;i<5;i++)
for(j=0;j<5;j++)
b[i][j]=INF;
for(i=1;i<5;i++)
{
for(j=0;j<5;j++)
scanf("%d",&a[i][j]);
}
scanf("%d %d %d %d",&k1,&k2,&x,&y);
if(k1==1 && k2==2)
Rotate_shun_90(a,b,2,x-1,y-1);
if(k1==1 && k2==3)
Rotate_shun_90(a,b,3,x-1,y-1);
if(k1==2 && k2==2)
Rotate_ni_90(a,b,2,x-1,y-1);
if(k1==2 && k2==3)
Rotate_ni_90(a,b,3,x-1,y-1);
Replace(a,b);
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
printf("%d ",a[i][j]);
printf("%d\n",a[i][4]);
}
}
return 0;
}
程序截图: