需要有关算法和代码的帮助(如果可以)... ^^ [英] Need help with the algorithm and the code (if you can)...^^
本文介绍了需要有关算法和代码的帮助(如果可以)... ^^的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的第3个问题..:D
请求是:输入4个数字并在屏幕上打印:
1.最大值和最小值
2.剩余号码.
我成功编码了第一个(最大和最小).代码在这里:
My 3rd qestion..:D
The request is: Input 4 numbers and print on screen:
1. Max and Min.
2. The remaning numbers.
I successfully coding the frist (Max and Min). The code''s here:
#include<stdio.h>
int main()
{
int a,b,c,d;
printf("Input a: ");
scanf("%i", &a);
printf("Input b: ");
scanf("%i", &b);
printf("Input c: ");
scanf("%i", &c);
printf("Input d: ");
scanf("%i", &d);
if(a>b&&a>c&&a>d) printf("Max is: %i\n", a);
if(b>a&&b>c&&b>d) printf("Max is: %i\n", b);
if(c>a&&c>b&&c>d) printf("Max is: %i\n", c);
if(d>a&&d>b&&d>c) printf("Max is: %i\n", d);
if(a<b&&a<c&&a<d) printf("Min is: %i\n", a);
if(b<a&&b<c&&b<d) printf("Min is: %i\n", b);
if(c<a&&c<b&&c<d) printf("Min is: %i\n", c);
if(d<a&&d<b&&d<c) printf("Min is: %i\n", d);
system("pause");
return 0;
}
现在我陷入了第二个请求的算法中...您能给我一个解决方案吗? (当然,如果可以的话,还提供代码).谢谢!! ^^
Now i get stuck at the algorithm of the 2nd request...Can you give me a solution? (And of course, the code if you can). Thank you!!^^
推荐答案
/*
Create an integer Array. and store input data into that array. I assume that you know how to store input data into array.
*/
void main(void)
{
int arrMaxMin[4] = {23,43,45,12}; /* you have to store input values into array. I have hard coded value for an example. */
int arrsize = sizeof(arrMaxMin) / sizeof(int);
int max = max_array(arrMaxMin,arrsize );
int min = min_array(arrMaxMin,arrsize );
printf("The max is %d\n", max);
printf("The min is %d\n", min);
printf("\nArray:\n");
print_remainingarray(arrMaxMin, arrsize, max, min);
}
int max_array(int arrMaxMin[], int arrsize)
{
int i, max=arrMaxMin[0];
for (i=0; i<arrsize;> if (arrMaxMin[i]>max)
max=arrMaxMin[i];
return(max);
}
int min_array(int arrMaxMin[], int arrsize)
{
int i, min=arrMaxMin[0];
for (i=0; i<arrsize;> if (arrMaxMin[i]<min)>
min=arrMaxMin[i];
return(min);
}
void print_remainingarray(int arrMaxMin[], int arrsize, int max, int min)
{
int i;
for(i=0; i<arrsize;> if(arrMaxMin[i]!= max & arrMaxMin[i]!= min)
printf("%d ", arrMaxMin[i]);
}
printf("\n");
}
/* Mark it as an answer if it helps you. */
这篇关于需要有关算法和代码的帮助(如果可以)... ^^的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文