zoukankan      html  css  js  c++  java
  • 经典算法(61~90)

    注:已选择性忽略绘图部分

    【程序61】(利用二维数组
    题目:打印出杨辉三角形(要求打印出10行如下图)
    1.程序分析:
    1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
    1 5 10 10 5 1

     1 #include<stdio.h>
     2 main()
     3 {
     4     int a[10][10];
     5     int i, j, k;
     6     for( i=0; i<9; i++)
     7     {
     8         for( k=0; k<10-i; k++)
     9             printf("  ");
    10         for( j=0; j<=i; j++)
    11         {
    12             if(j==0 || j==i)
    13                 a[i][j]=1;
    14             else
    15                 a[i][j]=a[i-1][j-1]+a[i-1][j];
    16             printf("%d   ",a[i][j]);
    17         }
    18         printf("
    ");
    19     }
    20     system("pause");
    21 }

    ==============================================================
    【程序62】 (略)
    题目:学习putpixel画点。

    ==============================================================
    【程序63】 (略)
    题目:画椭圆ellipse

    ==============================================================
    【程序64】 (略)
    题目:利用ellipse and rectangle 画图。

    ==============================================================
    【程序65】 (略)
    题目:一个最优美的图案。

    ==============================================================
    【程序66】 (回顾指针,实现2个数交换)
    题目:输入3个数a,b,c,按大小顺序输出。

     1 #include<stdio.h>
     2 void swap(int *p1, int *p2)
     3 {
     4     int p;
     5     if(*p1<*p2)
     6     {
     7         p=*p1;    *p1=*p2;    *p2=p;
     8     }
     9 }
    10 main()
    11 {
    12     int a,b;
    13     int *p1, *p2;
    14     p1=&a;
    15     p2=&b;
    16     scanf("%d%d", &a,&b);
    17     if(a<b)
    18         swap(p1,p2);
    19     printf("%d     %d
    ",*p1, *p2);
    20     system("pause");
    21 }

    ==============================================================
    【程序67】(本质冒泡排序) 
    题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

    #include<stdio.h>
    #define N 10
    main()
    {
        int a[N]={1,2,3,4,5,6,7,8,9,10};
        int i, j;
        for( i=0; i<N-1; i++)
        {
            for( j=i+1; j<N; j++)
            {
                if( a[i]<a[j])
                {
                    a[i]=a[i]^a[j];
                    a[j]=a[i]^a[j];
                    a[i]=a[i]^a[j];
                }
            }
            printf("%d  ",a[i]);
        }
        printf("%d", a[N-1]);
        getchar();
    }

    ==============================================================
    【程序68】 (取模运算)
    题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数

     1 #include<stdio.h>
     2 #define N 10
     3 main()
     4 {
     5     int a[N]={1,2,3,4,5,6,7,8,9,10};
     6     int tmp[N];
     7     int m;
     8     int i, j, temp;
     9     scanf("%d", &m);
    10     for( j=0; j<m; j++)
    11         {
    12             tmp[j]=a[j];
    13             //printf("temp[%d]=%d
    ", j, tmp[j]);
    14         }
    15     for( i=0; i<N; i++)
    16     {
    17         if( (i+m)>=N)
    18             a[i]=tmp[(i+m)%N];            //考虑溢出,取余循环
    19         else
    20             a[i]=a[i+m];                    //简单移位
    21         printf("%d  ", a[i]);
    22     }
    23     system("pause");
    24 }

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

     1 #include<stdio.h>
     2 #include<string.h>
     3 #define MAX 100+1
     4 main()
     5 {
     6     int n;
     7     int a[MAX];
     8     int i, temp, count;
     9     for( i=1; i<MAX; i++)
    10         a[i]=1;
    11     scanf("%d", &n);
    12     temp=n;
    13     count=0;
    14     i=1;
    15     for( ; ; )
    16     {
    17         //printf("loop
    ");
    18         if(n==1)
    19             break;
    20         if( i!=0)
    21         {
    22             if( a[i]==1)
    23             {
    24                 count++;
    25                 //printf("i=%d, count=%d
    ", i, count);
    26                 //a[i]=0;
    27                 if(count%3==0)
    28                 {
    29                     count=0;
    30                     a[i]=0;
    31                     n--;
    32                     printf("delete:%d
    n=%d
    ", i, n);
    33                 }
    34             }
    35             i=(i+1)%(temp+1);
    36             //printf("i=%d
    ",i);
    37         }
    38         else
    39             i++;
    40         }
    41     for( i=1; i<=temp; i++)
    42     {
    43         if(a[i]==1)
    44             printf("result=%d
    ", i);
    45     }
    46     system("pause");
    47 }

    ==============================================================
    【程序70】 (字符串处理)
    题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #define MAXN 100+1
     4 int count(char *p);
     5 main()
     6 {
     7     char *p[100];                            //关于此处定义方法不是特别清楚
     8     scanf("%s" , p);                         //输入不能超过100字符
     9     printf("%d
    ", count(p));
    10     system("pause");
    11 }
    12 int count( char *p)
    13 {
    14     int num=0;
    15     while(*p!='')
    16     {
    17         p++;
    18         num++;
    19     }
    20     return num;
    21 }

    【程序71】 (结构体声明的格式及注释)
    题目:编写input()和output()函数输入,输出5个学生的数据记录。

     1 #include<stdio.h>
     2 #define N 2
     3 struct Student
     4 {
     5     char name[10];        //char *name[10]区别
     6     char no[10];
     7     int age;
     8 }stu[N];                    //此处有疑问
     9 
    10 void input()
    11 {
    12     int i;
    13     for( i=0; i<N; i++)
    14     {
    15         printf("Please input the name of student %d: ", i+1);
    16         scanf("%s", stu[i].name);
    17         printf("Please input the number of student %d: ", i+1);
    18         scanf("%s", stu[i].no);
    19         printf("Please input the age of student %d: ", i+1);
    20         scanf("%d", &stu[i].age);
    21     }
    22 };
    23 void output()
    24 {
    25     int i;
    26         printf("Information about student :
    ");
    27         printf("  name    number age
    ");
    28     for( i=0; i<N; i++)
    29         printf("%d: %s    %s    %d
    ", i+1, stu[i].name, stu[i].no, stu[i].age);
    30 };
    31 main()
    32 {
    33     input();
    34     output();
    35     system("pause");
    36 }
    37     
  • 相关阅读:
    HDU 1978 How many ways
    hdu 1966 Pie
    hdu 1966 Pie
    HDU 1896 Stones
    HDU 1896 Stones
    hdu 1278 逃离迷宫
    hdu 1278 逃离迷宫
    HDU 2548 A strange lift
    HDU 2548 A strange lift
    PHP 错误与异常 笔记与总结(10)错误处理器测试
  • 原文地址:https://www.cnblogs.com/anthozoan77/p/4063095.html
Copyright © 2011-2022 走看看