C语言经典100例(1-20)

此文为本人在练习 菜鸟教程C语言经典100例 时所写的部分代码记录(1~20)

#include
int main(void)
{
    int i,j,k;
    for(i = 1;i < 5;i++)
    {
        for(j = 1;j < 5;j++)
        {
            if(i == j)
                continue;
            for(k = 1;k < 5;k++)
            {
                if(i == k || j == k)
                    continue;
                printf("%d,%d,%d\n",i,j,k);
            }
        }
    }
    return 0;
}
#include
#include
int main(void)
{
    int flag = 0;
    long n = -10000;
    while(n < 10000)
    {
        if(n + 100 == (int)sqrt(n + 100) * sqrt(n + 100) && n + 268 ==  (int)sqrt(n + 268) * sqrt(n + 268))
        {
            printf("%ld\n",n);
        }
        n++;
    }
    return 0;
}
#include
int IsLeap(int year)
{
    if((year%4==0&&year%100!=0)||(year%400==0))
        return 1;
    else
        return 0;
}
int main(void)
{
    int monthday[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},
                            {0,31,29,31,30,31,30,31,31,30,31,30,31}};
    int year,month,day;
    int sum = 0;
    printf("请输入年、月、日,格式为:年,月,日(2015,12,10)\n");
    scanf("%d,%d,%d",&year,&month,&day);
    if(IsLeap(year))
    {
        for(int i = 0; i < month; i++)
        {
            sum += monthday[1][i];
        }
    }
    else
    {
          for(int i = 0; i < month; i++)
        {
            sum += monthday[0][i];
        }
    }
    sum += day;
    printf("这是这一年的第 %d 天。",sum);
    return 0;
}

#include
int main(void)
{
    int x,y,z,temp,min,max;
    printf("请输入三个数字:\n");
    scanf("%d%d%d",&x,&y,&z);
    temp = x < y? x : y;
    min = temp < z? temp : z;
    temp = x < y? y : x;
    max = temp < z? z : temp;
    printf("从小到大排序:");
    printf("%d%3d%3d",min,x + y + z - min - max,max);
    return 0;
}
#include
int main(void)
{
    int i, j;
    for(i = 1;i < 10; i++)
    {
        for(j = 1;j <= i; j++)
        {
            printf("%d*%d=%d  ",i,j,i*j);
        }
        printf("\n");
    }
    return 0;
}
#include
int main(void)
{
    int i;
    int f1 = 1;
    int f2 = 1;
    for(i = 1; i <=40; i++)
    {
        if(i == 1)
            printf("%12d",f1);
        else
        {
            f2 = f1 + f2;
            f1 = f2 - f1;
            printf("%12d",f1);
        }

        if(i%4 == 0)
            printf("\n");
    }
    return 0;
}
#include
#include
int IsPrime(int n)
{
    int i;
    for(i = 2; i <= sqrt(n); i++)
    {
        if(n%i == 0)
            return 0;
    }
    return 1;
}
int main(void)
{
    int i, j = 0;
    for(i = 101; i <= 200; i++)
    {
        if(IsPrime(i))
        {
            printf("%d ",i);
            j++;
            if(j%5 == 0)
                printf("\n");
        }
    }
    return 0;
}
#include
int IsSXH(int n)
{
    int a,b,c,num;
    num = n;
    c = n%10;
    n = n/10;
    b = n%10;
    a = n/10;
    if(num == a*a*a + b*b*b + c*c*c)
        return 1;
    else
        return 0;
}
int main(void)
{
    int i;
    for(i = 100; i < 1000; i++)
    {
        if(IsSXH(i))
            printf("%d\n",i);
    }
    return 0;
}
#include
int main(void)
{
    int n,count = 1,num,i = 2;
    printf("请输入整数:");
    scanf("%d",&n);
    num = n;
    while(i < num)
    {
        if(n%i == 0)
        {
            if(count == 1)
                printf("%d=%d",n,i);
            else
                printf("*%d",i);
            count++;
            n = n/i;
        }
        else
            i++;
    }
    return 0;
}
#include
int gcd(int m,int n);
int min(int m,int n,int g);
int main(void)
{
    int m,n,g1,g2;
    printf("请输入两个数字:\n");
    scanf("%d%d",&m,&n);
    g1 = gcd(m,n);
    g2 = min(m,n,g1);
    printf("这两个数的最大公约数是%d,最小公倍数是%d",g1,g2);
    return 0;
}

int gcd(int m,int n)
{
    int r;
    while(n!=0)
    {
        r = m % n;
        m = n;
        n = r;
    }
    return m;
}

int min(int m,int n,int g)
{
    return m*n/g;
}
#include
int main(void)
{
    char ch;
    int alpha,num,space,others;
    alpha = num = space = others = 0;
    printf("请输入一些字母:\n");
    while((ch = getchar()) != '\n')
    {
        if((ch >= 'a' && ch <= 'z')||(ch >='A' && ch <='Z'))
            alpha++;
        else if(ch >= '0' && ch <= '9')
            num++;
        else if(ch == ' ')
            space++;
        else
            others++;
    }
    printf("字母=%d,数字=%d,空格=%d,其他=%d",alpha,num,space,others);
    return 0;
}
#include
long N_A(int a,int n);
int main(void)
{
    int a,n;
    printf("请输入 a 和 n:\n");
    scanf("%d%d",&a,&n);
    printf("a+aa+...=%ld",N_A(a,n));
    return 0;
}

long N_A(int a,int n)
{
    long sum = 0;
    long temp = a;
    while(n)
    {
        sum += temp;
        temp = temp*10 + a;
        n--;
    }
    return sum;
}
#include
int Perfect(int n);
int main(void)
{
    int i,j,count;
    for(i = 1; i <= 1000; i++)
    {
        if(Perfect(i))
        {
            count = 1;
            printf("%d=",i);
            for(j = 1; j < i; j++)
            {
                if(i % j == 0 && count == 1)
                {
                    printf("%d",j);
                    count++;
                }
                else if(i % j == 0 && count != 1)
                {
                    printf("+%d",j);
                    count++;
                }

            }

            printf("\n");
        }
    }
    return 0;
}

int Perfect(int n)
{
    int sum = 0;
    for(int i = 1; i < n; i++)
    {
        if(n%i == 0)
            sum += i;
    }
    if(n == sum)
        return 1;
    else
        return 0;
}
#include
int main(void)
{
    float h = 100,sum = 0;
    int count = 0;
    while(count < 10)
    {
        if(count == 0)
        {
            sum += h;
            h = h / 2;
        }
        else
        {
            sum += h*2;
            h = h / 2;
        }
        count++;
    }
    printf("第10次落地时,共经过%.6f米,第10次反弹高%.6f米\n",sum,h);
    return 0;
}

你可能感兴趣的:(C语言经典100例(1-20))