第一个:
#include
#include
#include
#define N 5
main()
{
int a[N];
int i,s1,s2;
s1=0;
s2=0;
printf("请输入%d个数,以-1结束:\n",N);
for(i=0;i{
scanf("%d",&a[i]);
if(a[i]%2==0)
s1+=1;
else
s2+=1;
}
printf("偶数个数为%d个\n",s1);
printf("奇数个数为%d个\n",s2-1);
}
两个题都只需要改变宏定义中的N就可以实现不同多个数据的输入了。答题不易,望采纳。有其他问题请采纳后,向我求助。
第二题:
#include
#include
#include
#define N 5
main()
{
int a[N];
int i,max,t=0;
printf("请输入%d个数:\n",N);
for(i=0;iscanf("%d",&a[i]);
max=a[0];
for(i=0;iif(a[i]>max)
{
max=a[i];
t=i;
}
printf("该数组最大数为:%d,其下标为:%d\n",max,t);
}
1.
#include "stdio.h"
#define NUM 10
short fun(short *a,short k);
main()
{
short a[NUM];
short temp;
short k=0;
short count_ji;
for(;;)
{
scanf("%hd",&temp);
if(temp==-1)
break;
a[k++]=temp;
}
count_ji=fun(a,k);
printf("jishu:%hd\noushu:%hd",count_ji,k-count_ji);
}
short fun(short *a,short k)
{
short i;
short count=0;
for(i=0;iif(a[i]%2)
count++;
return count;
}
2.
#include "stdio.h"
#define N 4
short fun(short *a,short *pmax);
void input(short *a);
main()
{
short a[N];
short max;
short temp;
input(a);
temp=fun(a,&max);
printf("%hd %hd",temp,max);
}
void input(short *a)
{
short i;
for(i=0;iscanf("%hd",a+i);
}
short fun(short *a,short *pmax)
{
short i;
*pmax=a[0];
for(i=0;iif(*pmax *pmax=a[i];
for(i=0;iif(*pmax==a[i])
break;
return i;
}
#include
#define N 10
int main()
{
int max=0,i,a[N],k=0;
printf("请为大小为%d的数组赋值!\n",N);
for(i=0;i
max=a[0];
for(i=1;i
{
max=a[i];
k=i;//记录下标
}
printf("\n");
printf("该数组最大值是%d下标为%d\n",max,k);
return 0;
}
1:用while()实现循环输入知道-1结束 对输入数据进行判断 计数