第一章 程序入门设计

内容:
1.1算术表达式
1.2变量及其输入
1.3顺序结构程序设计
1.4分支结构程序设计
[注解]:①C语言的概述及发展史:C99、C11及其它 ②数据类型与输入格式

1.1算术表达式

1-1 计算并输出1+2的值

#include<stdio.h>

int main()
{
    printf("%d\n", 1+2);
    return 0;
}

3

修改1-1,输出3-4、5*6、8/4、8/5

#include<stdio.h>

int main()
{
    printf("%d\n", 3-4);
    return 0;
}

-1

#include<stdio.h>

int main()
{
    printf("%d\n", 5*6);
    return 0;
}

30

#include<stdio.h>

int main()
{
    printf("%d\n", 8/4);
    return 0;
}

2

#include<stdio.h>

int main()
{
    printf("%d\n", 8/5);
    return 0;
}

1

#include<stdio.h>

int main()
{
    printf("%d\n", -8/5);
    return 0;
}

-1
对比:

#include<stdio.h>

int main()
{
    printf("%d\n", (-8)/5);
    printf("%d\n", 8/(-5));
    printf("%d\n", (-8)/(-5));
    return 0;
} 

-1
-1
1

引申

#include<stdio.h>

int main()
{
    printf("%d\n", (-8)%5);
    printf("%d\n", 8%(-5));
    printf("%d\n", (-8)%(-5));
    return 0;
} 

-3
3
-3

为什么是1而不是1.6?为什么不是-1.6而是-1?

1-2 计算并输出8/5的值,保留小数点后一位

#include<stdio.h>

int main()
{
    printf("%.1f\n", 8.0/5.0);
    return 0;
}

1.6

修改:%.1f->%.2f或者%f 8./5->8/5 %.1f->%d

#include<stdio.h>

int main()
{
    printf("%.2f\n", 8.0/5.0);
    return 0;
} 

1.60

#include<stdio.h>

int main()
{
    printf("%f\n", 8.0/5.0);
    return 0;
} 

1.600000

#include<stdio.h>

int main()
{
    printf("%.1f\n", 8/5);
    return 0;
} 

0.0

#include<stdio.h>

int main()
{
    printf("%f\n", 8/5);
    return 0;
} 

0.00000

为什么是0.0和0.00000呢?

#include<stdio.h>

int main()
{
    printf("%d\n", 8.0/5.0);
    return 0;
} 

-1717986918

为什么是这个结果呢?

问题①②涉及:整数和浮点数的编码

这个问题可能也跟计算机深层的东西有关,如果有很好的解答,会跟大家分享

这里有个注意点:-1717986918(虽然书里没讲,但是对于这么与众不同的数据,我还是满怀好奇的,对于这个特殊的数,在查阅有关资料之后,再分享出来)
这是一个比较合理的解释,不过可能要学习计算机更深层的东西才能完全理解:http://zhidao.baidu.com/link?url=NWXrjH-GvpPuoTXQlJ-nsj-BnavLCf76fFrVtdZl3nE1askseYm7GDQ3tBMQYKKsrPpYRFiEoz2fYRkTH87R-uddvWwf-na45n-WujF0TC_

1-3 复杂的程序计算(某些数学运算符的表示)

#include<stdio.h>
#include<math.h>

int main()
{
    printf("%.8f\n", 1+2*sqrt(3)/(5-0.1));
    return 0;
}

1.70695951

1.2 变量及其输入

1-4 a+b的问题

#include<stdio.h>

int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", a+b);
    return 0;
}

1 5
6

1-5 圆柱体的表面积
输入底面半径r和高h,求圆柱体表面积(保留三位小数)

#include<stdio.h>
#include<math.h>

int main()
{
    double r, h, area;
    scanf("%lf%lf", &r, &h);
    const double PI = acos(-1);
    area = 2*PI*r*r + 2*PI*r*h;
    printf("Area = %.3f\n", area); 
    return 0;
}

3.5 9
Area = 274.889

这里要注意的点是:%lf%lf—–>如果是%f%f,可以编译一下看看结果

PI求法:
1、一般:#define PI 3.14
2、这里:const double PI = acos(-1);

1-6 三位数反转

#include<stdio.h>

int main()
{
    int num;
    scanf("%d", &num);
    printf("%d%d%d\n", num%10, num/10%10, num/100);
    return 0;
}
#include<stdio.h>

int main()
{
    int num;
    scanf("%d",&num);
    int a = num/100;
    int b = num/10%10;
    int c = num%10;
    int sum = c*100 + b*10 + a;
    printf("%03d\n", sum);
    return 0;
}

120
021

理解:%03d的用意

上面是对于三位数的,下面是int 数通用的:[也是hdoj中金山的一道题]

#include<stdio.h>

int main()
{
    int num;
    scanf("%d", &num);
    while(num/10){
        printf("%d", num%10);
        num /= 10;
    }
    printf("%d\n", num);
    return 0;
}

1-7 交换两个变量a、b

最简单的就是(keep it simple and stupid:kiss):

#include<stdio.h>

int main()
{
    int a, b, temp;
    scanf("%d%d", &a, &b);
    printf("%d  %d\n", b, a);
    return 0;
}
#include<stdio.h>

int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    printf("a = %d, b = %d\n", a, b);
    int temp = a;
    a = b;
    b = temp;
    printf("a = %d, b = %d\n", a, b);
    return 0;
}
#include<stdio.h>

int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    a = b - a;
    b = b - a;
    a = b + a;
    printf("%d %d\n", a, b);
    return 0;
} 
#include<stdio.h>

int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    a = a + b;
    b = a - b;
    a = a - b;
    printf("%d %d\n", a, b); 
    return 0;
}
#include<stdio.h>

int main()
{
    int a, b, temp;
    scanf("%d%d", &a, &b);
    a = a^b;
    b = a^b;
    a = a^b;
    printf("%d  %d\n", a, b);
    return 0;
}

用指针实现:

#include<stdio.h>

void exchang(int *i, int *j){
    int temp;
    temp = *i;
    *i = *j;
    *j = temp; 
}
int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    exchang(&a, &b);
    printf("%d %d\n", a, b);
    return 0;
} 

1-8 鸡兔同笼
总数:n,总脚数:m

#include<stdio.h>

int main()
{
    int a, b, m, n;
    scanf("%d%d", &n, &m);
    a = (4*n - m)/2;
    b = (m - 2*n)/2;
    if(m%2==1 || a<0 || b<0){
        printf("No answer");
    }
    else{
        printf("%d  %d\n", a, b);
    }
    return 0;
}
#include<stdio.h>

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    if((4*n-m)/2>0 && (m-2*n)/2>0){
        printf("%d %d\n", (4*n-m)/2, (m-2*n)/2);
    } else {
        printf("No answer\n");
    }
    return 0;
}

1-9 三整数排序(从小到大)

#include<stdio.h>

int main()
{
    int a, b, c, temp;
    scanf("%d%d%d", &a, &b, &c);
    if(a>b){
        temp = a;
        a = b;
        b = temp;
    }
    if(a>c){
        temp = a;
        a = c;
        c = temp;
    }
    if(b>c){
        temp = b;
        b = c;
        c = temp;
    }
    printf("%d %d %d\n", a, b, c);
    return 0;
}

下面使用指针实现交换:

#include<stdio.h>

int swap(int *i, int *j)
{
    int temp;
    temp = *i;
    *i = *j;
    *j = temp;
    return 0;
}

int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    int *x = &a;
    int *y = &b;
    int *z = &c;
    if(a>b)
        swap(x, y);
    if(a>c)
        swap(x,z);
    if(b>c)
        swap(y,z);
    printf("%d %d %d\n", a, b, c);
    return 0;
}
#include<stdio.h>

void getChange(int *i, int *j){
    int temp = *i;
    *i = *j;
    *j = temp;
}

int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if(a>b){
        getChange(&a, &b);
    }
    if(a>c){
        getChange(&a, &c);
    }
    if(b>c){
        getChange(&b, &c);
    }
    printf("%d %d %d\n", a, b, c);
    return 0;
}

关于数据类型及格式输入符

#include<stdio.h>

int main()
{
    printf("%d\n", 11111 * 11111);
    return 0;
}

123454321

#include<stdio.h>

int main()
{
    printf("%d\n", 111111 * 111111);
    return 0;
}

[Warning] integer overflow in expression [-Woverflow]
-539247567

修改如下:

#include<stdio.h>

int main()
{
    printf("%ld\n", 111111 * 111111);
    return 0;
}

[Warning] integer overflow in expression [-Woverflow]
-539247567

再次修改:

 #include<stdio.h>

int main()
{
    printf("%lld\n", 111111 * 111111);
    return 0;
}

本章有7道习题,5个问题
·平均数
·温度
·连续和
·正弦余弦
·打折
·三角形
·年份
问题:int、double的范围、运算符的优先性

注:一些个人课后题解题想法在:“ 算法竞赛课后习题解题”(类别)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值