C语言经典100例(61-80)

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

网址为: http://www.runoob.com/cprogramming/c-100-examples.html

#include
int main(void)
{
    int i, j;
    int a[10][10];
    for(i = 0; i < 10; i++)
    {
        a[i][i] = 1;
        a[i][0] = 1;
    }

    for(i = 2; i < 10; i++)
    {
        for(j = 1;j < i; j++)
        {
            a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
        }
    }

    for(i = 0; i < 10; i++)
    {
        for(j = 0; j <= i; j++)
        {
            printf("%-4d",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}
#include
void sort(int *a,int *b,int *c)
{
    int max,min,temp;
    max = *a > *b ? (*a > *c ? *a : *c) : (*b > *c ? *b : *c);
    min = *a < *b ? (*a < *c ? *a : *c) : (*b < *c ? *b : *c);
    temp = *a + *b + *c - max - min;
    printf("%d %d %d",min,temp,max);
}

int main(void)
{
    int a,b,c;
    printf("输入 a, b ,c:\n");
    scanf("%d%d%d",&a,&b,&c);
    sort(&a,&b,&c);
    return 0;
}
#include
#define N 80
void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
int main(void)
{
    int a[N], n, maxpos = 0, minpos = 0;
    int max, min;
    printf("请输入数组元素个数:");
    scanf("%d",&n);
    printf("请输入数组元素:\n");
    for(int i = 0; i < n; i++)
        scanf("%d",&a[i]);
    max = min = a[0];
    for(int i = 0; i < n; i++)
    {
        if(max < a[i])
        {
            max = a[i];
            maxpos = i;
        }
        if(min > a[i])
        {
            min = a[i];
            minpos = i;
        }
    }

    swap(&a[0],&a[maxpos]);
    swap(&a[n - 1],&a[minpos]);

    for(int i = 0; i < n; i++)
        printf("%d ",a[i]);
    return 0;
}
#include
#define N 80
void reverse(int a[],int low,int high)
{
    int i = low;
    int j = high;
    while(i < j)
    {
        if(i < j)
        {
            int temp;
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
        i++;
        j--;
    }
}

int main(void)
{
    int n, m, a[N];
    printf("需要输入几个数?\n");
    scanf("%d",&n);
    printf("请输入%d个数:\n",n);
    for(int i = 0; i < n; i++)
    {
        scanf("%d",&a[i]);
    }
    printf("请输入移动位置大小:\n");
    scanf("%d",&m);
    reverse(a,0,n - 1);
    reverse(a,0,m - 1);
    reverse(a,m,n - 1);
    for(int i = 0; i < n; i++)
    {
        printf("%d ",a[i]);
    }
    return 0;
}

#include

int main()
{
    int a[50],n;
    printf("输入一圈人数:\n");
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        a[i]=i+1;
    }
    int coun=0,k=0,l=0;
    while(coun<n-1){
        if(a[l]!=0){
            k++;
        }
        if(k==3){
            a[l]=0;
            coun++;
            k=0;
        }

        l++;
        if(l==n){
            l=0;
        }
    }

    printf("剩余的人是");
    for(int i=0;i<n;i++){
        if(a[i]!=0){
            printf("%d号\n",a[i]);
        }
    }
    return 0;
}
#include
int str_len(char *str)
{
    int i = 0;
    while(str[i] != '\0')
        i++;
    return i;
}

int main(void)
{
    char str[80] = {'\0'};
    int ret;
    printf("请输入字符串:\n");
    gets(str);
    ret = str_len(str);
    printf("字符串有 %d 个字符。\n",ret);
    return 0;
}
#include
#include
#include
typedef struct node
{
    int data;
    struct node *next;
}node,*Linklist;

Linklist CreateList(int n)
{
    int i = 1;
    Linklist L,rear,s;
    L = (node*)malloc(sizeof(node));
    if(!L)
        return 0;
    L->next = NULL;
    rear = L;
    while(n)
    {
        s = (Linklist)malloc(sizeof(node));
        printf("请输入第%d个数:",i++);
        scanf("%d",&(s->data));
        rear->next = s;
        rear = s;
        n--;
    }
    rear->next = NULL;
    return L;
}
void Print(Linklist L)
{
    Linklist p = L->next;
    printf("所创建的链表为:\n");
    while(p)
    {
        if(p->next != NULL)
            printf("%d --> ",p->data);
        else
            printf("%d",p->data);
        p = p->next;
    }
}

int main(void)
{
    Linklist L = NULL;
    int n;
    printf("请输入数据个数:\n");
    scanf("%d",&n);
    L = CreateList(n);
    Print(L);
    return 0;
}
Linklist reverse(Linklist L)
{
    Linklist pre,p=L->next,r=p->next;
    p->next=NULL;//处理第一个结点
    while(r!=NULL)//r为空,则说明p为最后一个结点
    {
        pre=p;//依次遍历
        p=r;
        r=r->next;
        p->next=pre;//指针反转
    }
    L->next=p;//处理最后一个结点
    return L;
}
#include
void reverse(long n)
{
    while(n)
    {
        int i = n%10;
        printf("%d",i);
        n = n/10;
    }
}

int main(void)
{
    long n;
    printf("输入一个整数: ");
    scanf("%ld",&n);
    printf("反转后的整数: ");
    reverse(n);
    return 0;
}
#include
#include
float fun(int n)
{
    float sum = 0;
    if(n%2 == 0)
    {
        int i = 2;
        sum += 1.0/i;
        while(i != n)
        {
            i += 2;
            sum += 1.0/i;
        }
        return sum;
    }
    else
    {
        int i = 1;
        sum += 1.0/i;
        while(i != n)
        {
            i += 2;
            sum += 1.0/i;
        }
        return sum;
    }
}

int main()
{
    int n;
    float ret;
    printf("请输入一个数字:");
    scanf("%d",&n);
    ret = fun(n);
    printf("%f",ret);
    return 0;
}
#include
#include
struct man{
    char name[20];
    int  age;
}person[3]={{"li",18},{"wang",25},{"sun",22}};

int main()
{
    struct man *q,*p;
    int i;
    p = person;
    q = person;
    for(i=0;i<3;i++)
    {
        if(q->age < p->age)
        {
            q = p;
        }
        p++;
    }
    printf("%s %d\n",q->name,q->age);
    return 0;
}
#include
#include
int main()
{
    int x, i=0, j=1;
    while(i<5){
        x=4*j;
        for(i=0;i<5;i++)
        {
            if(x%4!=0){break;}
            x=(x/4)*5+1;
        }
        j++;
    }
    printf("%d\n",x);
    
    return 0;
}

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