导弹追踪飞机c语言编程,C程序拦截导弹

该程序读取用户输入的导弹高度数据,并使用C++实现了一个算法,计算能够容纳的最大导弹数量。通过动态内存分配存储输入数据,然后遍历数组找出最高导弹及其之后能容纳的导弹数。最后输出最多能容纳的导弹数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这很简单啦

[CODE]#include

int count(int* pArray,int n, int base)

{

int i;

int iCount = 0;

for (i = 0; i < n; i ++)

{

if(base >= pArray[i])

{

iCount ++;

}

}

return iCount;

}

int main(void)

{

int i, iCount;

int * pArray = NULL;

char szHeightArray[128];

char szHeight[16];

char * sztemp;

int iMax, iMaxCount, itemp;;

printf("Please enter the number of the missile:");

if (scanf("%d",&iCount) != 1)

{

printf("Error number\n");

return -1;

}

printf("Please enter the Height of the missile:");

if (scanf("%s", szHeightArray) != 1)

{

printf("Error input\n");

return -1;

}

pArray = malloc(iCount * sizeof(int));

if (NULL == pArray)

{

printf("alloc memery failed\n");

return -1;

}

sztemp = szHeightArray;

for(i = 0; i < iCount; i ++)

{

if (sscanf(sztemp, "%d,", &pArray[i]) != 1)

{

printf("Error input\n");

free(pArray);

return -1;

}

while(*sztemp != ',')

{

sztemp ++;

}

sztemp ++;

}

iMax = pArray[0];

iMaxCount = count(pArray + 1, iCount - 1, iMax);

for (i = 1; i < iCount; i ++)

{

if (pArray[i] < iMax)

{

continue;

}

itemp = count(&pArray[i], iCount - i, pArray[i]);

if (itemp > iMaxCount)

{

iMax = pArray[i];

iMaxCount = itemp;

}

}

printf("This machine can hold up %d missles\n", iMaxCount);

free(pArray);

system("pause");

return 0;

}[/CODE]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值