第4周

4.1 break语句和continue语句

在多重循环的情况下,break语句只能跳出直接包含它的那一重循环,continue只对直接包含它的那重循环起作用。

4.2 OJ输入数据的处理

scanf(...)表达式的值为int,表示成功读入的变量个数,匹配不成功时直接停止扫描,值为EOF(即-1)时说明输入数据已经结束。

4.3 用freopen重定向输入

freopen函数以指定模式重新指定到另一个文件,模式用于指定新文件的访问方式。

FILE *freopen(const char * restrict filename, const char * restrict mode, FILE * restrict stream);

filename:文件名或文件路径,mode:文件访问权限字符串("r"只读,“w”只写,“a”追加写入),steam:需要被重定向的文件流。

eg:freopen("c:\tmp\test.txt", "r", stdin);

 

作业

1.角谷猜想

Description:

所谓角谷猜想,是指对于任意一个正整数,如果是奇数,则乘3加1,如果是偶数,则除以2,得到的结果再按照上述规则重复处理,最终总能够得到1。如,假定初始整数为5,计算过程分别为16、8、4、2、1。 
程序要求输入一个整数,将经过处理得到1的过程输出来。

Input:一个正整数N(N <= 2,000,000)

Output:从输入整数到1的步骤,每一步为一行,每一部中描述计算过程。最后一行输出"End"。如果输入为1,直接输出"End"。

Sample Input:5

Sample Output:

5*3+1=16

16/2=8

8/2=4

4/2=2

2/2=1

End

 1 #include <cstdio>
 2 
 3 int main()
 4 {
 5     long long N;
 6     scanf("%lld", &N);
 7 
 8     while(N != 1) {
 9         long long t;
10         if(N % 2) {
11             t = N*3+1;
12             printf("%lld*3+1=%lld
", N, t);
13         }
14         else {
15             t = N/2;
16             printf("%lld/2=%lld
", N, t);
17         }
18         N = t;
19     }
20     printf("End
");
21 
22     return 0;
23 }

2.正常血压

Description:

监护室每小时测量一次病人的血压,若收缩压在90 - 140之间并且舒张压在60 - 90之间(包含端点值)则称之为正常,现给出某病人若干次测量的血压值,计算病人保持正常血压的最长小时数。

Input:

第一行为一个正整数n,n < 100
其后有n行,每行2个正整数,分别为一次测量的收缩压和舒张压,中间以一个空格分隔。

OutPut:输出仅一行,血压连续正常的最长小时数。

Sample Input:

4

100 80

90 50

120 60

140 90

Sample Output:2

 1 #include <cstdio>
 2 
 3 int main()
 4 {
 5     //freopen("D:\temp\test\tmp.txt", "r", stdin);
 6 
 7     int n, a, b;
 8     int sum = 0, maxsum = 0;
 9     scanf("%d", &n);
10     for(int i=0; i<n; i++) {
11         scanf("%d %d", &a, &b);
12         if(a>=90 && a<=140 && b>=60 && b<=90)
13             sum++;
14         else
15             sum = 0;
16         if(sum > maxsum)
17             maxsum = sum;
18     }
19 
20     printf("%d
", maxsum);
21 
22     return 0;
23 }

3.数字反转

Description:

给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2)。

Input:输入共 1 行,一个整数N。-1,000,000,000 ≤ N≤ 1,000,000,000。

Output:输出共 1 行,一个整数,表示反转后的新数。

Sample Input:样例 #1:123  样例 #2:-380

Sample Output:样例 #1:321  样例 #2:-83

 1 #include <cstdio>
 2 
 3 int main()
 4 {
 5     //freopen("D:\temp\test\tmp.txt", "r", stdin);
 6 
 7     int n, r = 0;
 8     scanf("%d", &n);
 9 
10     while(n) {
11         r = r*10+(n%10);
12         n /=10;
13     }
14 
15     printf("%d
", r);
16 
17     return 0;
18 }

4.求特殊自然数

Description:一个十进制自然数,它的七进制与九进制表示都是三位数,且七进制与九进制的三位数码表示顺序正好相反。编程求此自然数,并输出显示。

Input:无。

Output:三行。第一行是此自然数的十进制表示;第二行是此自然数的七进制表示;第三行是此自然数的九进制表示。

Sample Input:无。

Sample Output:(不提供)

 1 #include <cstdio>
 2 
 3 int main()
 4 {
 5     //freopen("D:\temp\test\tmp.txt", "r", stdin);
 6 
 7     printf("248
503
305
");
 8 
 9     return 0;
10 }

5.雇佣兵

Description:

雇佣兵的体力最大值为M,初始体力值为0、战斗力为N、拥有X个能量元素。

当雇佣兵的体力值恰好为M时,才可以参加一个为期M天的战斗期,战斗期结束体力值将为0。在同一个战斗期内,雇佣兵每连续战斗n天,战斗力就会上升1点,n为当前战斗期开始时的战斗力。

一个战斗期结束后,雇佣兵需要用若干个能量元素使其体力恢复到最大值M,从而参加下一个战斗期。每个能量元素恢复的体力值不超过当前的战斗力。每个能量元素只能使用一次。

请问:雇佣兵的战斗力最大可以到达多少。

Input:一行包括三个整数M、N、X,相邻两个整数之间用单个空格隔开。M、N、X均为不超过10000的正整数。

Output:输出一个整数,为雇佣兵的最大战斗力。

Sample Input:5 2 10

Sample Output:6

 1 #include <cstdio>
 2 
 3 int main()
 4 {
 5     //freopen("D:\temp\test\tmp.txt", "r", stdin);
 6 
 7     int m, n, x;
 8     scanf("%d %d %d", &m, &n, &x);
 9 
10     while(x > 0) {
11         int t = m/n;
12         if(m % n)
13             t++;
14         if(x < t)
15             break;
16         x -= t;
17         t = m/n;
18         n += t;
19     }
20 
21     printf("%d
", n);
22 
23     return 0;
24 }

6.数字统计

Description:

请统计某个给定范围[L, R]的所有整数中,数字2出现的次数。

比如给定范围[2, 22],数字2在数2中出现了1次,在数12中出现1次,在数20中出现1次,在数21中出现1次,在数22中出现2次,所以数字2在该范围内一共出现了6次。

Input:输入共 1 行,为两个正整数 L 和 R,之间用一个空格隔开。

Output:输出共 1 行,表示数字 2 出现的次数。

Sample Input:样例 #1:2 22  样例 #2:2 100

Sample Output:样例 #1:6  样例 #2:20

 1 #include <cstdio>
 2 
 3 int main()
 4 {
 5     //freopen("D:\temp\test\tmp.txt", "r", stdin);
 6 
 7     int L, R;
 8     int counter = 0;
 9     scanf("%d %d", &L, &R);
10 
11     for(int i=L; i<=R; i++) {
12         int j = i;
13         while(j) {
14             if(j%10 == 2)
15                 counter++;
16             j /= 10;
17         }
18     }
19 
20     printf("%d
", counter);
21 
22     return 0;
23 }
原文地址:https://www.cnblogs.com/VincentValentine/p/5658580.html