C语言问题

1. 1-100之间所有奇数存放在一个数组中,并输出该数组。
2. 输入一组数,以-1作为结束标志,再输入一个数,从数组中查找这个数的位置。

第1个回答  2008-01-11
#include <stdio.h>
void main()
{
int i,j,n;
n=0;
for(i=0;i<100;i++)
{
j=2;
while(i%j!=0) // 从2到i之间寻找第一个能被整除的数
j++;
if(i=j) // 如果第一数能被整除的数等于该数本身,则说明该数是素数
{
printf("%4d",i);
n++;
if(n%8==0)
printf("\n"); // 控制每行打⒏个素数
}
}
printf("\n");
}
------------------------------------------------------------------------------------------------------------------------------
int a[20];
main()
{
int i=0,j;
printf("\n 请输入19个整数:");
for(i=0;i<19;i++)
scanf("%d",&a[i]);
printf("\n 请输入结束标志(此例结束标志为-1):");
scanf("%d",&a[19]);
printf("\n 请输入待查找数字:");
scanf("%d",j);
for(i=0;i<20;i++)
{
if(a[i]==j)
{printf("\n 待查找数字位置是"%d"处!",i);
break;}
if(a[i]==-1)
{printf("\n 未找到待查找数字位置");
break;}

}
}
第2个回答  2008-01-11
第一个:
#include "stdio.h"
int main()
{
int a[50];
int i=1,j=0;
for(;i<100;i++)
if(i%2) a[j++] = i; //2不能整除的数就是奇数

for(i=0;i<j;i++)
printf("%d",a[i]);
return 0;
}

第二个:
#include "stdio.h"
#include "stdlib.h"
#define INITIALSIZE 100
#define INCREASESIZE 50
int main()
{
int *a,maxlength,i;
a = (int *)malloc(INTIALSIZE*sizeof(int));
maxlength = INITAILSIZE;
i = 0;
printf("Please enter the number you want!\nType -1 to quit...\n");
do{
scanf("%d",a[i++]);
if(i>maxlength)
{ //越界
a = (int *)realloc(a,INCREASESIZE*sizeof(int));
maxlenght += INCREASESIZE;
}
}while(a[i-1]!=-1)
printf("Please enter the number you want to find!\n");
int tag,j,count = 0;
scanf("%d",&tag);
for(j=0;j<i;j++)
if(tag == a[j]&&count++)
printf("%d ",j+1);
if(count)
printf("place(s) store(s) the target number!\n");
else
printf("The target number doesn't exist!\n");
free(a);
return 0;
}本回答被提问者采纳
第3个回答  2008-01-11
一、
#include<stdio.h>
#include<conio.h>

int main()
{
int a[50],i;
for(i=0;i<=49;i++)
a[i]=2*i+1;
for(i=0;i<=49;i++)
printf("%d ",a[i]);
printf("\n");
getch();
return(0);
}
二、
#include<stdio.h>
#include<conio.h>

int main()
{
int a[80],i=0,t,j,m=0;
printf("please enter some integers, end with -1\n");
scanf("%d",&a[i]);
while(a[i]!=-1)
{
i++;
scanf("%d",&a[i]);
}
printf("please enter an integer for search\n");
scanf("%d",&t);
for(j=0;j<=i-1;j++)
{
if(t==a[j])
{
printf("the position is %d\n",j+1);
m=1;
}

}
if(m==0)
printf("can't found\n");
getch();
return(0);
}