用C语言编写一个程序,求n个数中的最大值和最小值,要求用函数完成。 求c语言程序:用一个函数求N个数的最大值和最小值。。。
\u7528C\u8bed\u8a00\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u6c42n\u4e2a\u6570\u4e2d\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\uff0c\u8981\u6c42\u7528\u51fd\u6570\u5b8c\u6210\u3002\u4ee3\u7801\u5982\u4e0b\uff08\u7528\u5192\u6ce1\u6392\u5e8f\u6cd5\uff09\uff1a
#include void bubble(int a[], int n){ int i, j, temp; for (j = 0; j a[i + 1]) { temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } }}int main(void){ int a[10]; for(int i = 0; i < 10; i++) scanf("%d", &a[i]); bubble(a, 10); printf("Max: %d, min: %d", a[9], a[0]); return 0;}
#includeint max(int n){int i,x,f; scanf("%d",&f); for(i=1;if)f=x; } return f;}int main(){int n; printf("\u6709\u51e0\u4e2a\u6570:"); scanf("%d",&n); printf("\u5176\u4e2d\u6700\u5927\u7684\u6570\u662f\uff1a%d\n",max(n)); return 0;}
#include
int min;
int maxmin(int n)
{int i,x,f;
scanf("%d",&f);
min=f;
for(i=1;i<n;i++)
{scanf("%d",&x);
if(x>f)f=x;
else if(x<min)min=x;
}
return f;
}
int main()
{int n,mm;
printf("\u6709\u51e0\u4e2a\u6570:");
scanf("%d",&n);
mm=maxmin(n);
printf("\u5176\u4e2d\u6700\u5927\u7684\u6570\u662f\uff1a%d\n\u6700\u5c0f\u7684\u6570\u662f\uff1a%d\n",mm,min);
return 0;
}
思路:首先设置最大最小的值都是数组第一个数,然后遍历数组,出现比当前最大值大的,修改最大值为这个数。出现比最小值小的,修改最小值为这个数。
代码
#include <stdio.h>
//为了方便返回,使用结构体存储最大值和最小值
typedef struct {
int Max;
int Min;
}MaxMin;
//函数参数array:数组,length:数组长度。
//这里其实可以把最大值最小值作为指针传进去的。
MaxMin GetMaxMin(int *array, int length)
{
int i;
MaxMin maxMin;
if (length <= 0)
return maxMin;
//初始化最大值和最小值为数组第一个数
maxMin.Max = array[0];
maxMin.Min = array[0];
for (i = 0; i < length; i++) {
if (array[i] >= maxMin.Max)
maxMin.Max = array[i]; //比当前最大值大,修改最大值
if (array[i] <= maxMin.Min)
maxMin.Min = array[i]; //比当前最小值小,修改最小值
}
return maxMin;
}
//主函数测试
int main() {
int length;
int i;
int *array;
printf("input length of array:");
scanf("%d", &length);
array = (int *)malloc(sizeof(int)*length);
for (i = 0; i < length; i++) {
printf("input data of array(index %d):", i + 1);
scanf("%d", array + i);
}
MaxMin maxMin = GetMaxMin(array, length);
printf("Max:%d,Min:%d", maxMin.Max, maxMin.Min);
return 0;
}
3. 运行结果
#include <stdio.h>
int bubble(char *, int);
int main(int argc, const char * argv[])
{
int a[N] = {.....};
int max = 0, min = 0;
if (!bubble(a, N)) {
min = a[0];
max = a[N-1];
}
return 0;
}
int bubble(char * a, int size)
{
if (NULL == a) return -1;
int i = 0, j = 0, swap = 0;
for (i = 0; i < size-1; i++) {
for (j = 0; j < size-1-i; j++) {
if (a[j] > a[j+1]) {
swap = a[j];
a[j] = a[j+1];
a[j+1] = swap;
}
}
}
return 0;
}
代码如下:
void getMaxMin(int a[],int n,int *iMax,int *iMin)
{
int i;
*iMax=a[0];
*iMin=a[0];
for(i=1;i<n;i++)
{
if (*iMax<a[i])
*iMax=a[i];
if (*iMin>a[i])
*iMax=a[i];
}
}
函数就这样了。测试主函数你自己写一个 调用func就好
void func(int *a, int n, int *max, int *min)
{
int i;
*max = *min = a[0];
for(i = 1; i < n; i ++)
{
if(a[i] > *max) *max = a[i];
if(a[i]<*min) *min = a[i];
}
}
绛旓細1銆侀鍏堥渶瑕佸畾涔変竴涓狹ax鏈灏忓煎彉閲忋2銆佺劧鍚庢妸棣栧湴鍧鐨勫艰祴浜堟渶灏忓笺3銆佹帴涓嬫潵灏卞彲浠ュ紑濮嬭繘琛屼究鍒╂煡鎵俱4銆佸鏋滄湁涓暟澶т簬鏈澶у间究鍙互鑷姩鏇挎崲銆5銆佺劧鍚庡氨鍙互鎶婃渶灏忓艰繘琛岃緭鍑恒6銆佺劧鍚庤繘琛岃繍琛屽悗锛屽氨鍙互鐪嬪埌灞忓箷涓婃樉绀烘渶灏忓笺
绛旓細printf("杈撳叆娴嬭瘯涓暟锛歕n") ;scanf("%d",&k) ;int s; //鏁版嵁 int sum=0; /*鍜岋紝姣涓涓暟灏忎簬1000锛屾渶澶1000涓暟锛sum鏈澶т负1000000锛宨nt灏卞彲浠ヤ簡 */ //int i=0; //鎶婃敞閲婄殑浠g爜鍙栨秷鍙互杈撳嚭鏈夊摢涓浜涚礌鏁 //memset(data,0,sizeof(data));printf("璇疯緭鍏%d涓暟\n",k...
绛旓細include <stdio.h> main(){int a[100],m,i,sum=1;printf("input how many numbers you need:\n");scanf("%d",&m);for(i=0;i<m;i++){scanf("%d",&a[i]);sum*=a[i];} printf("\nthe ans is%d",sum);} 濡傛灉sum瓒呭嚭int鐨勮寖鍥达紝浣犲彲浠ヨ瘯鐫鐢╨ong绫诲瀷銆
绛旓細{ if(n<1) return 1;return n*fact(n-1);} else long fact(int n){ long t=1;for(int i=2;i<=n;i++)t*=i;return t;} endif main(){ long s=0;for(int i=1;i<=10;i++)s+=fact(i);printf("%ld\n",s);} 2銆俰nclude "stdio.h"bool prime(int n){ if(n==1)...
绛旓細浠ヤ笅鏄涓涓娇鐢–璇█鍔ㄦ佹暟缁勫疄鐜扮殑绋嬪簭锛鐢ㄤ簬璇诲彇鎸囧畾鏁伴噺鐨勫弻绮惧害瀹炴暟锛堜粠鏂囦欢data.txt涓級锛屽苟璁$畻瀹冧滑鐨勫钩鍧囧硷細```c include <stdio.h> include <stdlib.h> // 鍖呭惈鍔ㄦ佸垎閰嶅唴瀛樼殑鍑芥暟 int main() { int n;double *arr; // 瀹氫箟涓涓寚鍚戝弻绮惧害鍔ㄦ佹暟缁勭殑鎸囬拡 double sum = 0, avg;//...
绛旓細妤间富锛屾垜鐨绋嬪簭濡備笅銆傚嚱鏁癿ax璇诲叆涓涓猲鍏冪礌鐨勬暟缁刟锛岃繑鍥炲叾涓渶澶у厓绱犵殑鍦板潃銆傝繖鏍凤紝鍦ㄨ皟鐢╩ax鐨勫嚱鏁颁腑锛堣繖閲屾槸main锛夛紝鍙互閫氳繃涓ょ杩愮畻鍒嗗埆寰楀埌鏈澶у厓绱犵殑鍊煎拰瀹冪殑涓嬫爣锛堣main涓敞閲婏級include int max(int a,int n){ int i;int p = a;for (i = 0;i < n;i++ )if (a[i]> p )p ...
绛旓細include<stdio.h> int main(){int n,s=1;for(n=1;s<=400000;)s*=++n;printf("鏈澶х殑n鏄細%d",--n);return 0;}
绛旓細{ int i,m=1;for (i=1;i<=n;i++){ m=i*m;} return m;} void main(){ int a,b,c,sum;printf("please input three numbers:\n");scanf("%d%d%d",&a,&b,&c);sum=jiecheng(a)+jiecheng(b)+jiecheng(c);printf("\nthe sum is %d\n",sum);} 杩欎釜鏄彁绀鸿緭鍏ヤ笁涓暟锛...
绛旓細for(n=1;j<100;n++){ j=j+i*n;if(j>100) break;} printf("n=%d\n",--n); /*"--n"鏄洜涓猴紝鍦╪=3鏃讹紝j=66锛岀粡if(j>100)鍒ゆ柇涔嬪悗锛屼笉婊¤冻锛屽洜姝や笉鎵цbreak,浜庢槸n缁х画鑷姞锛屽綋n=4鏃讹紝j=110,缁廼f(j>100)鍒ゆ柇鍚庯紝婊¤冻锛屾墽琛宐reak锛屼絾姝ゆ椂锛宯=4,鏄涓涓笉婊¤冻鐨刵鍊,...
绛旓細include <stdio.h> int main(){ int n;printf("璇疯緭鍏ヨ鏄剧ず鐨勫钩鏂硅嚜鐒舵暟鐨涓暟锛");scanf("%d", &n);int sum = 0; // 鎬诲拰 for (int i = 1; i <= n; i++){ int square = i * i;sum += square;printf("%d^2=%d\n", i, square);} printf("骞虫柟鑷劧鏁扮殑鍜屼负锛%d\...