c语言高手帮帮忙,深夜在线等啊

来源:百度知道 编辑:UC知道 时间:2024/06/11 03:32:38
请编制一个程序其功能是:将满足此条件的4位数按从大到小的顺序存入数组b中,并要计算满足上述条件的4位数的个数c。条件是:千位数大于个位数且该数字是奇数。
我的思路是先求出个数,并将符合条件的数放到数组里去,我先作第一步求C的值

#include <stdio.h>
void main()
{int a[100],b[100],i,c,n,d;
printf("enter the number of integers:\n");
scanf("%d",&n);
printf("please enter %d numbers:\n",n);
for(i=0;i<n;i++)
{scanf("%d",&a[i]);
if(a[i]>=1000&&a[i]<10000)
{d=a[i]/1000;
while(d>((a[i]%1000)%100)%10&&a[i]%2==1)
{ b[c]=a[i];
c++;}
}
}
printf("%d",c);
}

哪错了,希望各位大哥大姐能告诉小弟

晕,你这程序咋写的啊。太乱了。
我感觉最好的思路是先判断是不是奇数,如果是奇数,再进行下面的千位和个位的比较。
如果第二个条件也满足了,就放入数组B中。等到所有数据处理完以后,对B进行排序,同时,个数也算出来了。

补上代码:
#include <stdio.h>

int main(void)
{
int b[100];
int count = 0;
int test = 0;
int i,j;
printf("Input the nuber,input 0 over.\n");

do{
scanf("%d", &test);
if((test > 1000)&&(test < 10000)&&(test % 2 != 0))
{
if((test % 1000) > (test % 10))
{
b[count] = test;
count++;
}
}
}while(test != 0);
//排序
for(i=0; i < count-1; i++)
{
for(j=i; j<count-1; j++)
{
if(b[i] < b[j+1])
{
test = b[j+1];
b[j+1] = b[i];
b[i] = test;
}
}
}

}

1.主函数要为整型
2.个位数就是a[i]%10 而不是那么复杂的式子
3.
while(d>((a[i]%1000)%100)%10&&a[i]%2==1)
{ b[c]=a[i];
c++;}
改为