C程序设计 第四版(谭浩强)-学习笔记-第六天

/*  创建时间:20171226
    创建人:fangweijun(773714759@qq.com)
    功能:对10个数组元素依次赋值0,1,2,3,4,5,6,7,8,9,按逆序输出 
    位置:C程序设计(第四版)2010年6月第四版 144页例6.1 
*/
#include<stdio.h>
int main()
{
    int i,a[10];
    for(i=0;i<=9;i++)
        a[i]=i;
    for(i=9;i>=0;i--)
        printf("%d ",a[i]);
    printf("\n"); 
    return 0;
}
/*Dev-c++输出结果: 
    9 8 7 6 5 4 3 2 1 0
*/


/*  创建时间:20171226
    创建人:fangweijun(773714759@qq.com)
    功能:用数组来处理求Fibonacci数列问题 
    位置:C程序设计(第四版)2010年6月第四版 146页例6.2 
*/
#include<stdio.h>
int main()
{
    int i;
    int f[20]={1,1};//对前面两个元素赋值1 
    for(i=2;i<20;i++) 
        f[i]=f[i-2]+f[i-1];//先后求出f[2]~f[19]的值保存到数组中 
    for(i=0;i<20;i++)
    {
        if(i%5==0)//换行 
            printf("\n");
        printf("%12d",f[i]);    
    }
    printf("\n");
    return 0;
}
/*Dev-c++输出结果: 

               1           1           2           3           5
               8          13          21          34          55
              89         144         233         377         610
             987        1597        2584        4181        6765
*/


/*  创建时间:20171226
    创建人:fangweijun(773714759@qq.com)
    功能:有10个地区的面积,要求对它们按由大到小的顺序排列 
    位置:C程序设计(第四版)2010年6月第四版 147页例6.3 
*/
#include<stdio.h>
int main()
{
    int a[10];
    int i,j,t;
    printf("input 10 number:\n");
    for(i=0;i<10;i++)
        scanf("%d",&a[i]);
    printf("\n");
    for(j=0;j<9;j++)//进行9次循环,实现9趟比较 
        for(i=0;i<9-j;i++)//在每一趟中进行9-j次比较 
            if(a[i]>a[i+1])//相邻两个数比较 
            {
                t=a[i];
                a[i]=a[i+1];//小的排前面 
                a[i+1]=t;   
            }
    printf("the sorted numbers:\n");
    for(i=0;i<10;i++)
        printf("%d ",a[i]);
    printf("\n"); 
    return 0;
}
/*Dev-c++输出结果: 
    input 10 number:
    55
    66
    33
    88
    1223
    6565
    878
    565
    3232
    0

    the sorted numbers:
    0 33 55 66 88 565 878 1223 3232 6565
*/


/*  创建时间:20171226
    创建人:fangweijun(773714759@qq.com)
    功能:将一个二维数组行和列的元素互换,存到另一个二维数组中 
    位置:C程序设计(第四版)2010年6月第四版 152页例6.4
*/
#include<stdio.h>
int main()
{
    int a[2][3]={{1,2,3},{4,5,6}};
    int b[3][2],i,j;
    printf("array a:\n");
    for(i=0;i<=1;i++)//a数组的行 
    {
        for(j=0;j<=2;j++)//a数组的列 
        {
            printf("%5d",a[i][j]);
            b[j][i]=a[i][j];
        }
        printf("\n");//每一行都换行 
    }
    printf("array b:\n");
    for(i=0;i<=2;i++)//输出b数组 
    {
        for(j=0;j<=1;j++)
            printf("%5d",b[i][j]);
        printf("\n");//每一行都换行 
    }
    return 0;
}
/*Dev-c++输出结果: 
    array a:
        1    2    3
        4    5    6
    array b:
        1    4
        2    5
        3    6
*/


/*  创建时间:20171226
    创建人:fangweijun(773714759@qq.com)
    功能:有一个3*4的矩阵,要求编程求出其中值最大的那个元素的值,以及其所在的行号和列号 
    位置:C程序设计(第四版)2010年6月第四版 152页例6.5
*/
#include<stdio.h>
int main()
{
    int i,j,row=0,colum=0,max;
    int a[3][4]={{1,2,3,4},{9,8,7,6},{-10,10,-5,2}};
    max=a[0][0];
    for(i=0;i<=2;i++)
        for(j=0;j<=3;j++)
            if(a[i][j]>max)
            {
                max=a[i][j];
                row=i;
                colum=j;    
            }
    printf("max=%d\nrow=%d\ncolum=%d\n",max,row,colum); 
    return 0;
}
/*Dev-c++输出结果: 
    max=10
    row=2
    colum=1
*/


/*  创建时间:20171226
    创建人:fangweijun(773714759@qq.com)
    功能:输出一个已知的字符串 
    位置:C程序设计(第四版)2010年6月第四版 152页例6.6
*/
#include<stdio.h>
int main()
{
    char c[15]={'I',' ','a','m',' ','a',' ','s','t','u','d','e','n','t','.'};
    int i;
    for(i=0;i<15;i++)
        printf("%c",c[i]);
    printf("\n"); 
    return 0;
}
/*Dev-c++输出结果: 
    I am a student.
*/


/*  创建时间:20171226
    创建人:fangweijun(773714759@qq.com)
    功能:输出一个菱形图 
    位置:C程序设计(第四版)2010年6月第四版 156页例6.7
*/
#include<stdio.h>
int main()
{
    char diamond[][5]={
        {' ',' ','*'},
        {' ','*',' ','*'},
        {'*',' ',' ',' ','*'},
        {' ','*',' ','*'},
        {' ',' ','*'}
    };
    int i,j;
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
            printf("%c",diamond[i][j]);
        printf("\n");
    }
    return 0;
}
/*Dev-c++输出结果: 
      *
     * *
    *   *
     * *
      *
*/


/*  创建时间:20171226
    创建人:fangweijun(773714759@qq.com)
    功能:输入一行字符,统计其中有多少个单词,单词之间用空格隔开 
    位置:C程序设计(第四版)2010年6月第四版 152页例6.8
*/
#include<stdio.h>
int main()
{
    char string[81];
    int i,num=0,word=0;
    char c;
    gets(string);
    for(i=0;(c=string[i])!='\0';i++)//只要不是'\n'就继续输入 
        if(c==' ')
            word=0;
        else if(word==0)// 不是字符 
        {
            word=1;
            num++;
        }
    printf("There are %d words in this line.\n",num);
    return 0;
}
/*Dev-c++输出结果: 
    i am a boy
    There are 4 words in this line.
*/


/*  创建时间:20171226
    创建人:fangweijun(773714759@qq.com)
    功能:有3个字符串,要求找出其中最大者 
    位置:C程序设计(第四版)2010年6月第四版 152页例6.9
*/
#include<stdio.h>
#include<string.h> 
int main()
{
    //定义数组和使用数组不一样
    //字母表中位置越后面的为大(字母大小一样){ASCII中字母表中越后面则ASCII数字越大} 
    char str[3][20];//定义二维数组 
    char string[20];//定义一维数组 
    int i;
    for(i=0;i<3;i++)
        gets(str[i]);//读入三个字符串
    if(strcmp(str[0],str[1])>0)//比较前两个 
        strcpy(string,str[0]);//将大的赋值给数组string 
    else
        strcpy(string,str[1]);
    if(strcmp(str[2],string)>0)//string和a[2]比较,将大的赋值给string 
        strcpy(string,str[2]);
    printf("\nthe largest string is:\n%s\n",string); 
    return 0;
}
/*Dev-c++输出结果: 
    Holland
    China
    America

    the largest string is:
    Holland
*/
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值