第2章 循环结构程序设计

2.1 for 循环


2.2 while 循环和 do-while 循环


2.3 循环的代价

  • windows 下的的管道输入

    echon|程序名

    n 是要输入的值

  • Linux 下的管道输入

    echon|./程序名


2.4 算法竞赛中的输入输出框架

  • 结束输入的方式:

    • Windows: Enter, Ctrl+Z, Enter
    • Linux: Ctrl+D
  • 文件比较命令:

    • Windows: fc
    • Linux: diff
  • 输入输出重定向:

    在 main 函数的入口加入下面两条语句(文件名不允许使用绝对或相对路径)

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    
  • 本机使用文件重定向,上交时删除的方法:

    #define LOCAL //提交时删除这一行
    #include <stdio.h>
    int main() {
          #ifdef LOCAL
          //本机执行的语句,如文件重定向
          #endif
          //...其他语句   
    }
    

    也可以在编译选项中定义 LOCAL 符号而不是在代码中,这样更不容易出错。

    方法是在编译选项中加入-DLOCAL

  • 使用文件输入输出,但不使用重定向的方式:

    int main() {
          FILE *fin, *fout;
          fin = fopen("data.in", "rb");   //rb 是二进制读方式打开文件
          fout = fopen("data.out", "wb"); //wb 是二进制写方式打开文件
          //如果想改成标准输入输出,令 fin = stdin, fout = stdout, 且不写 fclose 语句即可
          int x;
          while(fscanf(fin, "%d", &x) == 1) {
              //输入语句使用 fscanf,第一个参数是输入文件,后面的参数和 scanf 相同
              //...
          }
          fprintf(fout, "%d", x);
          fclose(fin);
          fclose(fout);   //最后应当关闭输入输出文件
          return 0;   
    }
    
  • 鲁棒性(robustness)

    可以理解成程序的健壮性或稳定性

  • 编译选项 -Wall 可以打出编译警告


2.5 注解与习题(python 实现)

  1. 水仙花数(三位数)

    for i in range(100, 1000):
        a = int(i / 100)
        b = int(i % 100 / 10)
        c = int(i % 10)
        if a*a*a + b*b*b + c*c*c == i:
            print(i)
    
  2. 韩信点兵:士兵以三人一排,五人一排,七人一排的队形排列

    输入中包含多组数据,每组数据包含三个非负整数 a, b, c,表示每一种队形排尾的人数(a<3, b<5, c<7),输出总人数的最小值或报告无解。已知总人数不小于10,不超过100

    import sys
    
    for line in sys.stdin:
        # 作用等同于 while(cin)
        # split() 函数默认分隔符是空格
        a, b, c = line.split()[:3]
        a = int(a)
        b = int(b)
        c = int(c)
        for x in range(10, 101):
            if int(x % 3) == a and int(x % 5) == b and int(x % 7) == c:
                print(x)
                break
            elif x == 100:
                print('no answer')
    
  3. 倒三角形

    n = int(input())
    
    for i in range(n):
        for blank in range(i):
            print(' ', end="")
        for sharp in range(2*(n-i)-1, 0, -1):
            print('#', end="")
        if i != n-1:
            print("")
    
  4. 子序列的和

    import sys
    
    for line in sys.stdin:
        n, m = line.split()[:2]
        n = int(n)
        m = int(m)
        if n == 0 and m == 0:
            break
        else:
            result = 0.0
            while n <= m:
                result += 1 / (n * n)
                n += 1
            print("%.5f" % result)
    
  5. 分数化小数:输出 a/b 保留 c 位小数的结果

    import sys
    
    for line in sys.stdin:
        a, b, c = line.split()[:3]
        a = int(a)
        b = int(b)
        c = int(c)
        if not a and not b and not c:
            break
        else:
            formatStr = "%%.%df" % c
            print(formatStr % (a/b))
    
  6. 排列:使用1-9组成 abc, def, ghi 三个数并输出,要求三个数的比值为1:2:3,每个数字仅使用一次

hash = [0] * 10
hash[0] = 1

def check(x):
a = int(x / 100)
b = int(x / 10 % 10)
c = int(x % 10)
if a != b and b != c and a != c and not hash[a] and not hash[b] and not hash[c]:
hash[a] = hash[b] = hash[c] = 1
return True
else:
return False

for x in range(123, 330):
hash = [0] * 10
hash[0] = 1
if check(x) and check(x+x) and check(x+x+x):
print(x, x+x, x+x+x)
```

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容