第1章 程序设计入门

  • 记录总结

    程序编译与运行原理与过程

    理解算法竞赛程序三部曲:输入、计算、输出;记住算法竞赛的目标及其对程序的要求;保持简单(Keep It Simple and Stupid, KISS)

    附录A

    黑盒测试

    顺序结构程序设计,分支结构程序设计

    C99,C11(无gets)

    C编译器 gcc、visual c++系列、(无需编译直接执行的c语言解释器 Ch、TCC)

    变量类型、声明、输入输出格式(不匹配造成的结果、%03d不足3位补0)、编码与运算、范围

    查文档printf,输出字符%d

    数学函数使用方法与常用函数 <math.h>

    逻辑表达式、运算符优先级、短路运算符

    Pi的表示方式 <math.h>中的M_PI,  ANSI C(C89)标准,  gcc-ansi编译尝试

    如何理解 a ^= b ^= a ^= b (^为异或)

 

  • 相关问题与代码
    1. 圆柱体表面积 (const 声明常量、pi的表示)

       1 #include <stdio.h>
       2 #include <math.h>
       3 int main()
       4 {
       5     const double pi = acos(-1.0);
       6     double r, h, s1, s2, s;
       7     scanf("%lf%lf", &r, &h);
       8     s1 = pi*r*r;
       9     s2 = 2 * pi*r*h;
      10     s = s1*2.0 + s2;
      11     printf("Area = %.3f\n", s);
      12     return 0;
      13 }
      View Code
    2. 变量交换(三变量法、以下另一种方法不建议使用)
       1 #include <stdio.h>
       2 int main()
       3 {
       4     int a, b;
       5     scanf("%d%d", &a, &b);
       6     a = a + b;//适用与定义了加减法的数据类型
       7     b = a - b;//异或代替加减法实现,简写为 a ^= b ^= a ^= b;
       8     a = a - b;
       9     printf("%d %d\n", a, b);
      10     return 0;
      11 }
      View Code
    3. 数据类型实验

      A1:11111*11111正确;6个1错误,为负数;9个1结果错误,为正数

      A2:11111.0*11111.0正确;6个1正确;9个1结果接近正确,个位错误

      A3:sqrt(x)返回浮点数,当x为负数返回-nan(ind)。

      nan是"not a number"的缩写,即计算结果不是个数。
      例如:32位数实际指数128,数符1或0,指数域二进制1111 1111。尾数域等于非零。
      ind 是 indeterminate 的缩写,即无法确定是什么。
      对负数开平方,对负数取对数,0.0除以0.0,0.0乘无穷大∞, 无穷大∞除以无穷大∞等错误都会得到它。

      A4:1.0 / 0.0, 0.0 / 0.0

      报错error C2124: 被零除或对零求模

    4. 输入格式实验

      B1 B2 B3 在scanf格式中插入空格、tab、回车可行

      B4:在scanf格式输入字符,输出为负数

      查文档printf,输出字符%d

    5. 习题

1-1平均数

1 #include <stdio.h>
2 int main()
3 {
4     int a, b, c;
5     scanf("%d%d%d", &a, &b, &c);
6     printf("%.3f\n", (a + b + c) / 3.0);
7     return 0;
8 }
View Code

1-2温度(摄氏温度c = 5*(华氏温度f-32)/9)

1 #include <stdio.h>
2 int main()
3 {
4     float f;
5     scanf("%f", &f);
6     printf("%.3f", 5 * (f - 32) / 9);//c=5*(f-32)/9
7     return 0;
8 }
View Code

1-3连续和

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int n, sum = 0;
 5     scanf("%d", &n);
 6     //for (int i = 1; i <= n; i++)
 7     //    sum += i;
 8     //printf("%d\n", sum);
 9     printf("%d\n", n*(n + 1) / 2);
10     return 0;
11 }
View Code

1-4正弦和余弦(math,h中的sin、cos、pi)

 1 #include <stdio.h>
 2 #include <math.h>
 3 int main()
 4 {
 5     const double pi = acos(-1.0);
 6     double n;
 7     scanf("%lf", &n);
 8     printf("%lf %lf\n", sin(pi*(n / 180)), cos(pi*(n / 180)));//double角度
 9     return 0;
10 }
View Code

1-5打折

1 #include <stdio.h>
2 int main()
3 {
4     float x;
5     scanf("%f", &x);
6     if (95 * x >= 300.0) printf("%.2f\n", 95 * x*0.85);
7     else printf("%.2f\n", 95 * x);
8     return 0;
9 }
View Code

1-6三角形

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int a[3], book = 1;
 5     for (int i = 0; i < 3; i++)
 6         scanf("%d", &a[i]);
 7     for (int i = 0; i < 3; i++)
 8     {
 9         if (a[i % 3] + a[(i + 1) % 3]<a[(i + 2) % 3] || a[i % 3] - a[(i + 1) % 3]>a[(i + 2) % 3])
10         {
11             book = 0;
12             break;
13         }
14     }
15     if (book) printf("yes\n");
16     else printf("not a triangle\n");
17     return 0;
18 }
View Code

1-7年份(闰年概念)

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int y, book = 0;
 5     scanf("%d", &y);
 6     if (y % 400 == 0) book = 1;
 7     else if (y % 4 == 0 && y % 100 != 0) book = 1;
 8     if (book) printf("yes\n");
 9     else printf("no\n");
10     return;
11 }
View Code

 

转载于:https://www.cnblogs.com/berred/p/7707465.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值