C语言编程求解

如题所述

第1个回答  2016-01-11

5.1

//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
int mymax(int *p,int n){
    int max;
    for(max=p[(--n)--];n>=0;n--)
        if(max<p[n]) max=p[n];
    return max;
}
int main(void){
    int a[10],i;
    printf("Please input 10 integers...\n");
    for(i=0;i<10;scanf("%d",a+i++));
    printf("The MAX is %d\n",mymax(a,10));
    return 0;
}

5.2

//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
int mygcd(int x,int y){
    int r;
    while(y){
        r=x%y;       
        x=y;
        y=r;
    }
    return x;  
}
int mylcm(int a,int b){
    return a/mygcd(a,b)*b;
}
int main(void){
    int a,b;
    while(1){
        printf("Please input a & b(int a,b>0 0 end)...\n");
        if(scanf("%d%d",&a,&b),a>0 && b>0){
            printf("The GCD is %d\n",mygcd(a,b));
            printf("The LCM is %d\n\n",mylcm(a,b));
        }
        else if(a==0 || b==0)
            break;
        else printf("Error, redo: ");
    }
    return 0;
}

本回答被网友采纳
第2个回答  2016-01-11
# include <stdio.h>

int main(void)
{
int a[10];
int b;
int c = 0; //这个数记录的是最大值。给赋值为0也是个bug。应该赋值输入十个数的第一个数。
printf("输入十个数\n");

for(b=0;b<10;b++)
{
scanf("%d",&a[b]);

if(c<a[b]) c=a[b]; //把c和每个输入的数字进行比较,如果大于c,让c记录它的值,最后记录下来的值就是最大值了。
}

printf("最大值%d\n",c);

return 0;

}本回答被提问者采纳