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

所有题目来源:菜鸟教程C 语言经典100例

附上前面的题目:C 语言经典100例(51-60)

目录

C 练习实例61 - 杨辉三角形
C 练习实例62
C 练习实例63
C 练习实例64
C 练习实例65
C 练习实例66
C 练习实例67
C 练习实例68
C 练习实例69
C 练习实例70

C 练习实例61 - 杨辉三角形

题目:打印出杨辉三角形(要求打印出10行)。

程序分析:

结构如下所示:
在这里插入图片描述

实例

//  Created by www.runoob.com on 15/11/9.
//  Copyright © 2015年 菜鸟教程. All rights reserved.
//
 
#include <stdio.h>
 
int main()
{
    int i,j;
    int a[10][10];
    printf("\n");
    for(i=0;i<10;i++) {
        a[i][0]=1;
        a[i][i]=1;
    }
    for(i=2;i<10;i++)
        for(j=1;j<i;j++)
            a[i][j]=a[i-1][j-1]+a[i-1][j];
    for(i=0;i<10;i++) {
        for(j=0;j<=i;j++)
            printf("%5d",a[i][j]);
        printf("\n");
    }
}

以上代码执行输出结果为:

在这里插入图片描述

C 练习实例62

题目:学习putpixel画点,(在TC中实现)。

程序分析:无。

程序源代码:

//  Created by www.runoob.com on 15/11/9.
//  Copyright © 2015年 菜鸟教程. All rights reserved.
//

#include "stdio.h"
#include "graphics.h"
int main()
{
    int i,j,driver=VGA,mode=VGAHI;
    initgraph(&driver,&mode,"");
    setbkcolor(YELLOW);
    for(i=50;i<=230;i+=20)
        for(j=50;j<=230;j++)
            putpixel(i,j,1);
    for(j=50;j<=230;j+=20)
        for(i=50;i<=230;i++)
            putpixel(i,j,1);
}

C 练习实例63

题目:画椭圆ellipse(在TC中实现)。

程序分析:无。

程序源代码:

//  Created by www.runoob.com on 15/11/9.
//  Copyright © 2015年 菜鸟教程. All rights reserved.
//

#include "stdio.h"
#include "graphics.h"
#include "conio.h"
int main()
{
    int x=360,y=160,driver=VGA,mode=VGAHI;
    int num=20,i;
    int top,bottom;
    initgraph(&driver,&mode,"");
    top=y-30;
    bottom=y-30;
    for(i=0;i<num;i++)
    {
        ellipse(250,250,0,360,top,bottom);
        top-=5;
        bottom+=5;
    }
    getch();
}

C 练习实例64

题目:利用ellipse and rectangle 画图(在TC中实现)。

程序分析:无。

程序源代码:

//  Created by www.runoob.com on 15/11/9.
//  Copyright © 2015年 菜鸟教程. All rights reserved.
//

#include "stdio.h"
#include "graphics.h"
#include "conio.h"
main()
{
    int driver=VGA,mode=VGAHI;
    int i,num=15,top=50;
    int left=20,right=50;
    initgraph(&driver,&mode,"");
    for(i=0;i<num;i++)
    {
        ellipse(250,250,0,360,right,left);
        ellipse(250,250,0,360,20,top);
        rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
        right+=5;
        left+=5;
        top+=10;
    }
    getch();
}

C 练习实例65

题目:一个最优美的图案(在TC中实现)。

程序分析:无。

程序源代码:

实例

//  Created by www.runoob.com on 15/11/9.
//  Copyright © 2015年 菜鸟教程. All rights reserved.
//
 
#include "graphics.h"
#include "math.h"
#include "dos.h"
#include "conio.h"
#include "stdlib.h"
#include "stdio.h"
#include "stdarg.h"
#define MAXPTS 15
#define PI 3.1415926
struct PTS {
    int x,y;
};
double AspectRatio=0.85;
void LineToDemo(void)
{
    struct viewporttype vp;
    struct PTS points[MAXPTS];
    int i, j, h, w, xcenter, ycenter;
    int radius, angle, step;
    double rads;
    printf(" MoveTo / LineTo Demonstration" );
    getviewsettings( &vp );
    h = vp.bottom - vp.top;
    w = vp.right - vp.left;
    xcenter = w / 2; /* Determine the center of circle */
    ycenter = h / 2;
    radius = (h - 30) / (AspectRatio * 2);
    step = 360 / MAXPTS; /* Determine # of increments */
    angle = 0; /* Begin at zero degrees */
    for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */
        rads = (double)angle * PI / 180.0; /* Convert angle to radians */
        points[i].x = xcenter + (int)( cos(rads) * radius );
        points[i].y = ycenter - (int)( sin(rads) * radius * AspectRatio );
        angle += step; /* Move to next increment */
    }
    circle( xcenter, ycenter, radius ); /* Draw bounding circle */
    for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
        for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
            moveto(points[i].x, points[i].y); /* Move to beginning of cord */
            lineto(points[j].x, points[j].y); /* Draw the cord */
        }
    }
}
int main()
{
    int driver,mode;
    driver=CGA;mode=CGAC0;
    initgraph(&driver,&mode,"");
    setcolor(3);
    setbkcolor(GREEN);
    LineToDemo();
}

C 练习实例66

题目:输入3个数a,b,c,按大小顺序输出。

程序分析:利用指针方法。

实例

//  Created by www.runoob.com on 15/11/9.
//  Copyright © 2015年 菜鸟教程. All rights reserved.
//
 
# include<stdio.h>
 
void swap(int *, int *);
int main(void)
{
    int a, b, c;
    int *p1, *p2, *p3;
    printf("输入 a, b ,c:\n");
    scanf("%d %d %d", &a, &b, &c);
    p1 = &a;
    p2 = &b;
    p3 = &c;
    if(a>b)
        swap(p1, p2);
    if(a>c)
        swap(p1, p3);
    if(b>c)
        swap(p2, p3);
    printf("%d %d %d\n", a, b, c);
}
void swap(int *s1, int *s2)
{
    int t;
    t = *s1; *s1 = *s2; *s2 = t;
}

以上程序执行输出结果为:

输入 a, b ,c:
1 3 2
1 2 3

C 练习实例67

题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

程序分析:无

实例

#include<stdio.h>
#include<stdlib.h>
 
void fun(int *s,int n)
{
    int i;
    int max=s[0];
    int a=0;
    for(i=0;i<n;i++)
    {
        if(s[i]>max)
        {
            max=s[i];
            a=i;
        }
    }
    s[a]=s[0];
    s[0]=max;
    int j;
    int min=s[n-1];
    int b=n-1;
    for(j=0;j<n;j++)
    {
        if(s[j]<min)
        {
            min=s[j];
            b=j;
        }
    }
    s[b]=s[n-1];
    s[n-1]=min;
}
 
void printf_s(int *s,int n)
{
    int i;
    for(i=0;i<n;i++)
        printf("%d ",s[i]);
    printf("\n");
}
 
int main()
{
    int s[20];
    int i,n;
    printf("设置数组长度(<20):");
    scanf("%d",&n);
    printf("输入 %d 个元素:\n",n);
    for(i=0;i<n;i++)
        scanf("%d",&s[i]);
    fun(s,n);
    printf_s(s,n);
    return 0;
}

输出结果未:

设置数组长度(<20):5
输入 5 个元素:
12 123 4 65 21
123 12 21 65 4

C 练习实例68

题目:有 n个整数,使其前面各数顺序向后移 m 个位置,最后m个数变成最前面的 m 个数。

程序分析:无。

实例

//  Created by www.runoob.com on 15/11/9.
//  Copyright © 2015年 菜鸟教程. All rights reserved.
//
 
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int arr[20];
    int i,n,offset;
    //输入数组大小和数组内容
    printf("Total numbers?\n");
    scanf("%d",&n);
    printf("Input %d numbers.\n",n);
    for(i=0;i<n;i++)
        scanf("%d",&arr[i]);
    //输入滚动偏移量
    printf("Set your offset.\n");
    scanf("%d",&offset);
    printf("Offset is %d.\n",offset);
    //打印滚动前数组
    print_arr(arr,n);
    //滚动数组并打印
    move(arr,n,offset);
    print_arr(arr,n);
}
 
//打印数组
void print_arr(int array[],int n)
{
    int i;
    for(i=0;i<n;++i)
        printf("%4d",array[i]);
    printf("\n");
}
//滚动数组
void move(int array[],int n,int offset)
{
    int *p,*arr_end;
    arr_end=array+n;      //数组最后一个元素的下一个位置
    int last;
    
    //滚动直到偏移量为0
    while(offset)
    {
        last=*(arr_end-1);
        for(p=arr_end-1;p!=array;--p)   //向右滚动一位
            *p=*(p-1);
        *array=last;
        --offset;
    }
}

C 练习实例69

题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

程序分析:无。

实例

//  Created by www.runoob.com on 15/11/9.
//  Copyright © 2015年 菜鸟教程. All rights reserved.
//
 
#include <stdio.h>
void main()
{
    int num[50],n,*p,j,loop,i,m,k;
    printf("请输入这一圈人的数量:\n");
    scanf("%d",&n);
    p=num;
    //开始给这些人编号
    for (j=0;j<n;j++)
    {
        *(p+j)=j+1;
    }
    i=0;//i用于计数,即让指针后移
    m=0;//m记录退出圈子的人数
    k=0;//k报数1,2,3
    while(m<n-1)//当退出的人数不大于总人数时,即留下的人数至少是一个人
        //这句不能写成m<n,因为假设有8人,当退出了6人时,此时还是进行人数退出,即m++,
        //这时是7<8,剩下的一个人自己喊1,2,3那么他也就退出了,将不会有输出
    {
        if (*(p+i)!=0)//如果这个人的头上编号不是0就开始报数加1,这里采用的方法是报数为3的人头上编号重置为0
        {
            k++;
        }
        if (k==3)
        {    k=0;    //报数清零,即下一个人从1开始报数
            *(p+i)=0;//将报数为3的人编号重置为0
            m++;    //退出人数加1
        }
        i++;      //指针后移
        if (i==n)//这句很关键,如果到了队尾,就要使指针重新指向对头
            //并且它只能放在i++后面,因为只有i++了才有可能i==n
        {
            i=0;
        }
        
        
    }
    printf("现在剩下的人是:");
    for (loop=0;loop<n;loop++)
    {
        if (num[loop]!=0)
        {
            printf("%2d号\n",num[loop]);
        }
    }
    
}

以上程序执行输出结果为:

请输入这一圈人的数量:
8
现在剩下的人是: 7号

C 练习实例70

题目:写一个函数,求一个字符串的长度,在 main 函数中输入字符串,并输出其长度。

程序分析:无。

实例

//  Created by www.runoob.com on 15/11/9.
//  Copyright © 2015年 菜鸟教程. All rights reserved.
//
 
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int len;
    char str[20];
    printf("请输入字符串:\n");
    scanf("%s",str);
    len=length(str);
    printf("字符串有 %d 个字符。",len);
}
//求字符串长度  
int length(char *s)  
{  
    int i=0;
    while(*s!='\0')
    {  
        i++;   
        s++;  
    }  
    return i;  
}

以上程序执行输出结果为:

请输入字符串:
www.runoob.com
字符串有 14 个字符。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小朱不猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值