输入5个字符串,按从大到小排序后输出,要求用指针数组实现。
例:(括号内为说明)
输入
food
appear
zoo
911
apple
输出
zoo
food
apple
appear
911
#include <stdio.h>
#include <string.h>
void main()
{
int i,j;
char s[5][80],*p[5],temp[80];
for(i=0;i<5;i++){
scanf("%s",s[i]);
p[i]=s[i];
}
for(i=1;i<5;i++)
for(j=0;j<5-i;j++)
if(strcmp(s[j],s[j+1])<0){
strcpy(temp,s[j]);
strcpy(s[j],s[j+1]);
strcpy(s[j+1],temp);
}
for(i=0;i<5;i++)
puts(p[i]);
}
例:(括号内为说明)
输入
food
appear
zoo
911
apple
输出
zoo
food
apple
appear
911
#include <stdio.h>
#include <string.h>
void main()
{
int i,j;
char s[5][80],*p[5],temp[80];
for(i=0;i<5;i++){
scanf("%s",s[i]);
p[i]=s[i];
}
for(i=1;i<5;i++)
for(j=0;j<5-i;j++)
if(strcmp(s[j],s[j+1])<0){
strcpy(temp,s[j]);
strcpy(s[j],s[j+1]);
strcpy(s[j+1],temp);
}
for(i=0;i<5;i++)
puts(p[i]);
}
程序填空,不要改变与输入输出有关的语句。
输入一个正整数n(0 例:括号内是说明
输入:
5 (n=5)
1 2 4 5 7
3 (x=3)
输出:
1 2 3 4 5 7
#include <stdio.h>
void main()
{
int i,j,n,x,a[10];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&x);
for(i=0;i<n;i++)
if(a[i]<x&&a[i+1]>=x)
j=i+1;
for(i=n;i>j;i--)
a[i]=a[i-1];
a[j]=x;
for(i=0;i<=n;i++)
printf("%d ",a[i]);
putchar('\n');
}
输入一个正整数n(0
输入:
5 (n=5)
1 2 4 5 7
3 (x=3)
输出:
1 2 3 4 5 7
#include <stdio.h>
void main()
{
int i,j,n,x,a[10];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&x);
for(i=0;i<n;i++)
if(a[i]<x&&a[i+1]>=x)
j=i+1;
for(i=n;i>j;i--)
a[i]=a[i-1];
a[j]=x;
for(i=0;i<=n;i++)
printf("%d ",a[i]);
putchar('\n');
}
程序填空。用结构类型表示时间内容(时间以时、分、秒表示),输入一个时间数值,再输入一个秒数n(n<60),以h:m:s的格式输出该时间再过n秒后的时间值(超过24点从0点开始计时)。
例:
输入
11:59:40
30
输出
12:0:10
#include <stdio.h>
void main( )
{
int n;
struct{
int h,m,s;
}time;
scanf("%d:%d:%d", &time.h,&time.m,&time.s);
scanf("%d",&n);
if(time.s+n>=60){
time.m++;
time.s=time.s+n-60;
}
else
time.s+=n;
if(time.m==60){
time.m=0;
time.h++;
}
if(time.h>=24)
time.h=0;
printf("%d:%d:%d\n", time.h,time.m,time.s);
}
例:
输入
11:59:40
30
输出
12:0:10
#include <stdio.h>
void main( )
{
int n;
struct{
int h,m,s;
}time;
scanf("%d:%d:%d", &time.h,&time.m,&time.s);
scanf("%d",&n);
if(time.s+n>=60){
time.m++;
time.s=time.s+n-60;
}
else
time.s+=n;
if(time.m==60){
time.m=0;
time.h++;
}
if(time.h>=24)
time.h=0;
printf("%d:%d:%d\n", time.h,time.m,time.s);
}
指针与数组程序设计之选择法排序
| December 4, 2007 20:08 | timmy | Via Original
定义函数void sort(int a[],int n),用选择法对数组a中的元素进行升序排序。自定义main()函数,并在其中调用sort()函数。
例:(括号内为说明)
输入
10
9 0 8 8 7 6 5 4 3 2
输出
After sort:0 2 3 4 5 6 7 8 8 9
#include <stdio.h>
void sort(int a[],int n);
void main()
{
int a[10],n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,n);
printf("After sort:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
void sort(int a[],int n)
{
int i,k,index,t;
for(i=0;i<n;i++){
index=i;
for(k=i+1;k<n;k++)
if(a[k]<a[index]){
t=a[index];
a[index]=a[k];
a[k]=t;
}
}
}
例:(括号内为说明)
输入
10
9 0 8 8 7 6 5 4 3 2
输出
After sort:0 2 3 4 5 6 7 8 8 9
#include <stdio.h>
void sort(int a[],int n);
void main()
{
int a[10],n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,n);
printf("After sort:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
void sort(int a[],int n)
{
int i,k,index,t;
for(i=0;i<n;i++){
index=i;
for(k=i+1;k<n;k++)
if(a[k]<a[index]){
t=a[index];
a[index]=a[k];
a[k]=t;
}
}
}
在数组中查找指定元素
| December 4, 2007 20:08 | timmy | Via Original
输入一个正整数repeat (0 输入一个正整数n (1 要求定义并调用函数search(list, n, x),它的功能是在数组list中查找元素x,若找到则返回相应下标,否则返回-1。
例:括号内是说明
输入
2 (repeat=2)
3 1 2 -6 2
5 12 2 5 4 0 100
输出
index=1
Not found
#include <stdio.h>
int main( )
{
int ri, repeat;
int i, index, n, res, x;
int a[10];
int search(int list[], int n, int x);
scanf("%d", &repeat);
for(ri=1; ri<=repeat; ri++){
scanf("%d", &n);
for(i=0; i<n; i++)
scanf("%d", &a[i]);
scanf("%d", &x);
res=search(a,n,x);
if(res!=-1)
printf("index=%d\n", res);
else
printf("Not found\n");
}
}
int search(int list[], int n, int x)
{
int i,res;
res=-1;
for(i=0;i<n;i++)
if(list[i]==x){
res=i;
break;
}
return res;
}
例:括号内是说明
输入
2 (repeat=2)
3 1 2 -6 2
5 12 2 5 4 0 100
输出
index=1
Not found
#include <stdio.h>
int main( )
{
int ri, repeat;
int i, index, n, res, x;
int a[10];
int search(int list[], int n, int x);
scanf("%d", &repeat);
for(ri=1; ri<=repeat; ri++){
scanf("%d", &n);
for(i=0; i<n; i++)
scanf("%d", &a[i]);
scanf("%d", &x);
res=search(a,n,x);
if(res!=-1)
printf("index=%d\n", res);
else
printf("Not found\n");
}
}
int search(int list[], int n, int x)
{
int i,res;
res=-1;
for(i=0;i<n;i++)
if(list[i]==x){
res=i;
break;
}
return res;
}












