3566 - 4.cpp

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

已知a数组存放5个四位正整数, 请考生编制一函数void jsVal(int a[5],int b[10],int n),其功能是: 把a数组中的元素个位数字和千位数字重新组成一个新的含有两位数字的数(新数的十位数字是原四位数的个位数字,新数的个位数字是原四位数的千位数字),以及把百位数字和十位数字组成另一个新的含有两位数字的数(新数的十位数字是原四位数的百位数字,新数的个位数字是原四位数的十位数字)。将新组成的10个两位数依次存入数组b中(如a[0]分解的个位和千位组成的数存放到b[0]中,百位和十位组成的数存放到b[1],其他的依此类推),然后将b数组的前n(1<=n<=10)个数按从大到小的顺序排序,最后输出b数组中的元素。

程序中可定义数组: a[5], b[10]
需要编写主函数main()、void jsVal(int a[5],int b[10],int n)函数

</div>

<span style="font-size:12.0pt;line-height:125%;font-family:宋体;color:red;"><span style="color:#000000;"></span><span style="color:#000000;"></span><span style="color:#000000;"></span><span style="color:#000000;"></span></span><span style="font-size:12.0pt;line-height:125%;font-family:宋体;color:red;"><span></span><span></span><span></span><span style="color:#000000;"></span><span style="color:#000000;"></span><span></span></span> 

题目输入

输入n,表示要排序的n个数据(输入1<=n<=10

输入5个整型数据。每个数据用1个空格分隔

<span style="font-size:12.0pt;line-height:150%;font-family:宋体;"> </span>

<span style="font-size:12.0pt;line-height:150%;font-family:宋体;color:red;"><span></span></span> 

<br />

<span style="font-size:12.0pt;line-height:150%;font-family:宋体;"><span></span><span></span><span></span></span> 

题目输出

输出10个数据,每个数据用1个空格隔开。注意最后1个数据后不需要输出空格

<br />

输入/输出样例

输入格式

4
1234 5678 6789 8901 9012
5
9876 8765 7654 6543 5432

输出格式

85 67 41 23 96 78 18 90 29 1
87 76 69 58 47 65 36 54 25 43

C语言解答

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
void jsVal(int a[5],int b[10])
{
	int qw, bw, sw, gw, i, newV1, newV2, j,cnt=0 ;
	for(i = 0 ; i < 5; i++) {
		qw = a[i] / 1000 ;
		gw = a[i] % 10 ;
		bw = a[i]/100%10 ;
		sw = a[i]/10%10;
		newV1 = gw * 10 + qw ;
		newV2 = bw * 10 + sw ;
		b[cnt++] = newV1 ;
		b[cnt++]=newV2;

	}

	for(i = 0 ; i < 4 ; i++)
		for(j = 5 ; j < cnt-1-i ; j++)
			if(b[j] < b[j+1]) {
				newV1 = b[j] ;
				b[j] = b[j+1] ;
				b[j+1] = newV1 ;
			}

}


int main()
{
  // freopen("in","r",stdin);
  //  freopen("out","w",stdout);
   int  i,j,n;
   int a[5],b[10];
  while( scanf("%d",&n)!=EOF){
   for(j=0;j<n;j++)
   {
	   for(i=0;i<5;i++)
		   scanf("%d",&a[i]);
	   jsVal(a,b);
	   for(i=0;i<9;i++)
		   printf("%d ",b[i]);
	   printf("%d\n",b[i]);
   }
  }

  return 0;
}

C++解答

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int cmp(int a,int b)
{
	return a>b;
}
int main()
{
	int a[5],k[10],i,j,n,b,c,d,e;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<5;i++)
			scanf("%d",&a[i]);
		for(i=0,j=0;i<10,j<5;j++,i+=2)
		{
            b=a[j]/1000;
			c=(a[j]-b*1000)/100;
			d=(a[j]-b*1000-c*100)/10;
			e=a[j]%10;
			k[i]=e*10+b;
			k[i+1]=c*10+d;
		}
		sort(k,k+n,cmp);
		for(i=0;i<10;i++)
		{
			printf("%d",k[i]);
			if(i!=9)
                 printf(" ");
		}
		printf("\n");
	}	
	return 0;
}