问题 问答题

入数组a中,请编制一函数jsVal(),其功能是:如果一个4位数的千位数字大于等于百位数字,百位数字大于等于十位数字,以及十位数字大于等于个位数字,并且此4位数是奇数,则统计出满足此条件的数的个数ent并把这些4位数按从小到大的顺序存人数组b中,最后调用写函数writeDat()把结果cnt及数组b中符合条件的4位数输出到OUT21.DAT文件中。
注意:部分源程序已给出。程序中已定义数组:a[200],b[200],已定义变量:cnt。请勿改动主函数main()、读函数readDat()和写函数writeDat()的内容。
[试题程序]
#include<stdio.h>
#define MAX 200
int a[MAX],b[MAX],cnt=0;
void writeDat();
void jsVal()


void readDat()

int i;
FILE*fp;
fp=fopen("IN21.DAT","r");
for(i=0;i<MAX;i++)
fscanf(fp,"%d",&a[i]);
fclose(fp);

voidmain()

int i;
readDat();
jsVal();
printf("满足条件的数=% d\n",cnt);
for(i=0;i<cnt;i++)
printf("%d",b[i]);
printf("\n");
writeDat();

void writeDat()

FILE*fp;
int i;
fp=fopen("OUT21.DAT","w");
fprintf(fp,"% d\n",cnt);
for(i=0;i<cnt;i++)
fprintf(fp,"% d\n",b[i]);
fclose(fp);

答案

参考答案:

void jsVal()

{

int i,j; /*定义循环控制变量*/

int aA,aB,aC,aD; /*定义变量保存D位数的每位数字*/

int temp; /*定义数据交换时的暂存变量*/

for(i=0;i<B00;i++) /*逐个取每一个D位数*/

{

aD=a[i]/A000; /*求D位数的千位数字*/

aC=a[i]%A000/A00; /*求D位数的百位数字*/

aB=a[i]%A00/A0; /*求D位数的十位数字*/

aA=a[i]%A0; /*求D位数的个位数字*/

if((aD>=aC) && (aC>=aB) && (aB>=aA) && aA%B! =0)

{ /*如果千位数字大于等于百位数字,百位数字大于等于十位数字,十位数字大于等于个位数字,并且此数是奇数*/

b[cnt]=a[i]; /*则将满足条件的数存入数组b中*/

cnt++; /*统计满足条件的数的个数*/

}

}

for(i=0;i<cnt-A;i++) /*将数组b中的数按从小到大的顺序排列*/

for(j=i+l;j<cnt;j++)

if(b[i]>b[j])

{

temp=b[i];

b[i]=b[j];

b[j]=temp;

}

}

解析:

根据题意可知,函数jsVal()要实现两个功能:一是找出满足条件的那些数,并存放在数组b中;二是对数组b中的数进行从小到大的排序。

首先要找出满足条件的数,关键在于判断每个数是否满足“千位数字大于等于百位数字,百位数字大于等于十位数字,十位数字大于等于个位数字,并且该数是奇数”的条件。

其次,将满足条件的数存入数组b中,并用变量cnt来统计数组b中元素的个数。

最后,将所有满足条件的数取出后利用选择法进行排序,即将当前元素依次同它后面的元素进行比较,发现小于该数的数,就进行交换。

单项选择题
单项选择题

Beyond question, Clinton was supposed to close up, stay out of sight, and avoid second- guessing his wife’s boss. After his ego blustered into oncoming traffic during Hillary’s heated primary race (1) Obama, the nation suffered from yet another turn of Clinton fatigue.

(2) this week Bill Clinton showed an instinct for robust, inclusive leadership that the (3) White House occupant could make good use of right for the time being. With President Obama struggling to (4) the political damage from the Gulf oil spill, Clinton not only can’t help himself—he’s worth being listened.

The conventional-wisdom (5) on President Obama’s early reaction to the spill was (6) he didn’t emote enough. He didn’t feel the (7) of all those people in the crisis whose livelihood would be destroyed, whose clean waters and wildlife would be (8) in black gunk.

Clinton considers this is an unfair (9) but offers a different—and more pointed— lesson to his young successor. "I think we ought to (10) in the same boat for a while... Let’s just (11) the problem, and then we can hold everybody responsible and emote or not emote, " Clinton conversed (12) CNN Anchor Wolf Blitzer.

Obama’s first (13) after the oil spill was to "feel the blame" rather than "feel the pain, " which are pretty sarcastic words as it were. No one intends to let British Petroleum (BP) management (14) the hook—for dangerously cutting corners, and for a (15) safety record. However, the President’s (16) focus on scolding BP consumed (17) White House energy while the oil gushed.

Great leaders don’t rush to criticise; Instead they instinctively (18) solutions. Rudy Giuliani, who didn’t stop to blame (19) intelligence for letting it happen, stood out from New York mayor with a girlfriend problem to 9/11 hero when he took control of a crisis and instilled confidence that a ravaged city could (20) beyond a terrorist attack.

18()

A. call for

B. sum up

C. go over

D. look for