编程练习 期中考试

来源:互联网 发布:校园网络广播系统 编辑:程序博客网 时间:2024/05/29 18:57

    • 2015_MidExam_single_choice
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    • 闰年判断
      • 读题
      • my answer
      • the standard answer
      • 反馈
    • tolower
      • 读题
      • my answer
      • the standard answer
      • 反馈
    • 换硬币for midterm
      • 读题
      • my answer1
      • my answer2
      • the standard answer
      • 反馈
    • Maximal Discount
      • 读题
      • my answer
      • the standard answer
      • 反馈
    • proper fraction
      • 读题
      • my answer
      • the standard answer
      • 反馈
    • rectangle
      • 读题
      • my answer
      • the standard answer
      • 反馈


2015_MidExam_single_choice

1

Which of the following is not a valid variable name declaration?

Aint _a3;
Bint a_3;
Cint 3_a;
Dint _3a;

Your Answer: C

The standard answer is C

标识符开头只可以是字母或者下划线

2

Which data type is most suitable for storing a number 65000 in a 32-bit system?

A signed short
B unsigned short
C long
D int

Your Answer: D

The standard answer is B

3

for c = 2, value of c after c <<= 1;

A c = 1;
B c = 2;
C c = 3;
D c = 4;

Your Answer: D
The standard answer is D

a<<=n等价于a=a<<na<<n表示a左移n位(二进制)等价于a乘以2的n次方 

4

Which of the following is an invalid method for input?

A scanf(“%d%d%d”,&a, &b, &c);
B scanf(“%d %d %d”, &a, &b, &c);
C scanf(“Three values are %d %d %d”,&a,&b,&c);
D None of the mentioned

Your Answer: C

The standard answer is D
c也是有效的

5

What does this statement printf(“%10s”, state); means?
Choices:

A 10 spaces before the string state is printed
B Print empty spaces if the string state is less than 10 characters
C Print the last 10 characters of the string
D None of the mentioned

Your Answer: B
The standard answer is B

s前的10表示输出的占10个字符位,所以如果字符串不足10位,用空格补齐

6

The number of digits present after decimal in float is________.

A1
B3
C6
D16

Your Answer: D

The standard answer is C

这是在考..浮点数小数点后有几位小数..6位显然..但是我没看懂题目

7

What is the size of an int data type?

A 4 Bytes
B 8 Bytes
C Depends on the system/compiler
D Cannot be determined

Your Answer: C
The standard answer is C

8

if (a == 1||b == 2){} can be written as:

A

if (a == 1)

if (b == 2){}

B

if (a == 1){}

if (b == 2){}

C

if (a == 1){}

else if (b == 2){}

D

None of the mentioned

Your Answer: B

The standard answer is D

9

Which for loop has range of similar indexes of ‘i’ used in for (i = 0;i < n; i++)?

A

for (i = n; i>0; i–)

B

for (i = n; i >= 0; i–)

C

for (i = n-1; i>0; i–)

D

for (i = n-1; i>-1; i–)

Your Answer: D

The examination has ended. You can no longer submit your answer.

The standard answer is D

10

What is the output of this C code?

A

true

B

false

C

Depends on the compiler

D

No print statement

Your Answer: D

The standard answer is D

闰年判断

Description

小明的爸爸听说小明最近学会了怎么判断闰年,就随机出了很多个年份让小明判断。请写一个程序帮帮苦逼的小明。

Input

第1行包含一个整数n(0 <= n <= 10),表示年份的数量;第2~n+1行每行包含1个整数year(0 <= year <= 65535),表示爸爸的提问。

Output

对每一次提问,若为闰年,输出”Yes”,否则输出”No”。

Sample Input
4
1
4
100
400

Sample Output
No
Yes
No
Yes

读题

闰年就是1.整百年要整除400 2.其他的整除4

my answer

#include<stdio.h>2.int main() {3.    int year, i, n;4.    scanf("%d", &n);5.    for (i = 0; i < n; i ++) {6.        scanf("%d", &year);7.        if (year % 100 == 0) {8.           if (year % 400 == 0) {9.            printf("Yes\n");10.        } else {11.            printf("No\n");12.        }13.        } else {14.            if (year % 4 == 0) {15.            printf("Yes\n");16.        } else {17.            printf("No\n");18.        }19.        }20.    }21.    return 0;22.}

the standard answer

#include <stdio.h>2. 3.int main(void) {4.    int n, year;5.    scanf("%d", &n);6.    while (n--) {7.        scanf("%d", &year);8.        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))9.            puts("Yes");10.        else11.            puts("No");12.    }13.    return 0;14.}

反馈

一开始不知道闰年还要分400这种..所以浪费了一点时间

tolower

Input a string which contains (a,b,c,…,z,A,B,C,…Z,\t and space ’ ‘) and end of ‘\n’,Please convert all the character into lower case and delete all the space and \t.(1 <= string length <= 20)

Output format:

no ‘\n’

For example:


[Input]

I L o Ve YoU

[Output]

iloveyou


The input “I L o Ve YoU” means that:

I+’ ‘+L+’ ‘+o+’ ‘+’ ‘+V+e+’\t’+Y+o+U+’\n’

读题

因为有空格啊,所以不能用scanf读取字符串,要用getchar()
再将里面大写的转化成小写
最后输出数组里面是字母的部分就好了

my answer

#include<stdio.h>2.#include<string.h>3.int main() {4.    char a[25] = {};5.    int i = 0;6.    for (a[i] = getchar(); a[i] != '\n';a[i] = getchar()) {7.        if (a[i]>= 'A' && a[i] <= 'Z') {8.            a[i] += 32;9.        }10.        i++;11.    }12.  13.    for (i = 0; a[i] != '\n' ;i++) {14.        if (a[i]>= 'a' && a[i] <= 'z') {15.            printf("%c", a[i]);16.        }17.    }18.    return 0;19.}

the standard answer

#include<stdio.h>2.int main() {3.    char ch;4. 5.    while ((ch = getchar()) != '\n') {6.        if (ch == ' ' || ch == '\t')7.            continue;8. 9.        if (ch >= 65 && ch <= 90)10.            ch += 32;11. 12.        printf("%c", ch);13.    }14.    return 0;15.}

反馈

答案的方法更简洁..在一个循环中,判断是否为空格或\t,如果是,用continue跳出该次循环,如果不是,转小写直接输出..

换硬币(for midterm)

Jeremy 放假的时候去了一趟HK,他惊喜的发现由于通货膨胀严重,HK竟然有1元,2元,甚至5元的硬币。Jeremy想把身上携带的n元纸币全部换成硬币,请问他有多少种换成的方式呢(即不同类型的硬币数量有多少种组合方式)?

input:n ( 0 < n < 2016, 用EOF结束)

output: result (result 为组合方式数)

例如

input:

1

2

5

100

output:

1

2

4

541

(注意每次输出结果后均换行)

读题

我用了类似百钱白鸡的穷举法,但是因为嵌套了三层循环,所以超时.

my answer1

#include<stdio.h>2.int main() {3.    int n, i, j, k, result;4.    for (scanf("%d", &n); n != EOF; scanf("%d", &n)) {5.        result = 0;6.        for (i = 0; i <= n/5; i++) {7.            for (j = 0; j <= n/2; j++) {8.                for (k = 0; k <= n; k++) {9.                    if (n == 5 * i + 2 * j + k)10.                    result++;11.                }12.            }13.        }14.        printf ("%d\n", result);15.    }16.    return 0;17.}

my answer2

#include<stdio.h>2.int main() {3.    int n, i, j, k, result;4.    for (scanf("%d", &n); n != EOF; scanf("%d", &n)) {5.        result = 0;6.        int jud = 1;7.        i = 0;j= 0;k = 1;8.        while (jud) {9.            if (n == 5 * i + 2 * j + k) {10.                result++;11.            }12.            if (k < n) {13.                k++;14.            } else if (j < n / 2) {15.                k = 0;16.                j++;17.            } else if (i < n / 5) {18.                k = 0; j = 0;19.                i++;20.            } else {21.                jud = 0;22.            }23.        }24.        printf ("%d\n", result);25.    }26.    return 0;27.}

the standard answer

#include <stdio.h>2.int main(void) {3.    int n;4.    while (EOF != scanf("%d", &n)) {5.        int count = n / 5 + 1;  // 五元与一元搭配 和全一元的情况6.        int fiveMax = n / 5;7.        for (int i = 0; i <= fiveMax; i++) {8.            count += ((n - i * 5) >> 1); // 五元与二元与一元的情况9.        }10.        printf("%d\n", count);11.    }12.    return 0;13.}

反馈

Maximal Discount

Description:

Linda is a shopaholic. Whenever there is a discount of the kind where you can buy three items and only pay for two, she goes completely mad and feels a need to buy all items in the store. You have given up on curing her for this disease, but try to limit its effect on her wallet. You have realized that the stores coming with these offers are quite selective when it comes to which items you get for free; it is always the cheapest ones. As an example, when your friend comes to the counter with seven items, costing 400, 350, 300, 250, 200, 150, and 100 dollars, she will have to pay 1500 dollars. In this case she got a discount of 250 dollars. You realize that if she goes to the counter three times, she might get a bigger discount. E.g. if she goes with the items that costs 400, 300 and 250, she will get a discount of 250 the first round. The next round she brings the item that costs 150 giving no extra discount, but the third round she takes the last items that costs 350, 200 and 100 giving a discount of an additional 100 dollars, adding up to a total discount of 350. Your job is to find the maximum discount Linda can get.

Input:

The input consists of two lines. The first gives the number of items Linda is buying, 1 ≤ n ≤ 100. The next line gives the prices of these items, 1 ≤ pi ≤ 1000.

Output:

Output one line giving the maximum discount Linda can get by selectively choosing which items she brings to the counter at the same time.

Sample Input:

6

400 100 200 350 300 250

Sample Output:

400

读题

题目废话太多
给你数字n,然后给你n个数
将这些数据排序(以从大到小为例
将排好序的数中 排位是三的倍数 的数找出来相加

my answer

#include<stdio.h>2.int main() {3.    int n, i, j, temp;4.    int discount = 0;5.    int a[105] = {};6.    for (i = 1; i <= n; i++) {7.        scanf("%d", &a[i]);8.    }9.    10.     for (j = 1; j < n; j++)11.        for (i = i; i < n - j; i++)12.        {13.            if(a[i] > a[i + 1])14.            {15.                temp = a[i];16.                a[i] = a[i + 1];17.                a[i + 1] = temp;18.            }19.        }20. 21.    for (i = n-2; i > 0;){22.        discount += a[i];23.        i -= 3;24.    }25.    printf("%d", discount);26.    return 0;27.}

the standard answer

#include<stdio.h>2.#include<stdlib.h>3. 4.void Merge(int *R, int low, int m, int high);5.void MergeSort(int R[], int low, int high);6. 7.int main(void) {8.    int arr[110], num, i, j, res = 0;9. 10.    scanf("%d", &num);11.    for (i = 0; i < num; i++) {12.        scanf("%d", &arr[i]);13.    }14. 15.    // sort the array with Merge Sort.16.    MergeSort(arr, 0, num - 1);17. 18.    for (i = num - 1, j = 0; i >= 0; i--, j++) {19.        if ((j + 1)%3 == 0) {20.            res += arr[i];21.        }22.    }23. 24.    printf("%d\n", res);25.    return 0;26.}27. 28.void Merge(int *R, int low, int m, int high) {29.    int i = low, j = m + 1, p = 0;30.    int *R1;31.    R1 = (int *)malloc((high - low + 1)*sizeof(i));32.    if (!R1) return;33. 34.    while (i <= m && j <= high) {35.        R1[p++] = (R[i] <= R[j])?R[i++]:R[j++];36.    }37. 38.    while (i <= m) {39.        R1[p++] = R[i++];40.    }41. 42.    while (j <= high) {43.        R1[p++] = R[j++];44.    }45. 46.    for (p = 0, i = low; i <= high; p++, i++) {47.        R[i] = R1[p];48.    }49. 50.    free(R1);51.}52. 53.void MergeSort(int R[], int low, int high) {54.    int mid;55.    if (low < high) {56.        mid = (low + high)/2;57.        MergeSort(R, low, mid);58.        MergeSort(R, mid + 1, high);59.        Merge(R, low, mid, high);60.    }61.}

反馈

proper fraction

giving an integer N (2<=N<=20) and a real number M (0.5

读题

给你两个数n和m
输出所有分子分母都小于n并且小于m的最简真分数

my answer

#include<stdio.h>2.int main() {3.    int n, i, j;4.    float max;5.    scanf("%d%f", &n, &max);6.    for (i = 1; i <= n; i++) {7.        for (j = 1; j < i; j++) {8.            float x, y;9.            x = j;10.            y = i;11.            float real = x / y;12.            13.            if (j == 1 && real <= max) {14.                printf("%d/%d\n", j, i);15.            } else if (i % j != 0 && real <= max) {16.                printf("%d/%d\n", j, i);17.            }18.        }19.    }20.    return 0;21.}

the standard answer

#include<stdio.h>2.int main() {3.    int length = 0;4.    int i, j;5.    int x;6.    int flag = 0;7.    int k = 0;8.    double threshold = 0;9.    scanf("%d%lf", &x, &threshold);10.    for (i = 1; i <= x; i++) {11.        for (j = 1; j < i; j++) {12.            flag = 0;13.            for (k = 2; k <= j; k++) {14.                if (j != 1) {15.                    if ((j % k == 0) && (i % k == 0)) {16.                        flag = 1;17.                        break;18.                    }19.                }20.            }21.            if (flag == 0 && 1.0 * j / i <= threshold) {22.                printf("%d/%d\n", j, i);23.            }24.        }25.    }26.    return 0;27.}

反馈

我没有处理好分子分母有公约数的情况

rectangle

Give you N rectangles.If you can pick exactly three pieces of those bones to form a larger rectangle?

input:

There are several testcases.

The first line is an integer T, indicating the number of testcases.

For each testcase:

The first line is a integer N and N is no more than 10.

The second line contains 2*N integers describing N rectangles.Each rectangle is described by 2 integers indicating as width and height.

All these integers in the second line are between [1,10000]

output

If you can do it, print Yes.Otherwise, print No instead.

sample input

2

4

1 1 1 1 1 2 2 2

4

1 1 2 2 10 10 20 20

sample output

Yes

No

Hint:

改编自2015北京区域赛某题

读题

my answer

the standard answer

#include<stdio.h>2.int a[110][2][2];3.int main() {4.    int T, n;5.    int i, j, k, x, y, z;6.    int flag;7.    scanf("%d", &T);8.    while (T--) {9.        scanf("%d", &n);10.        for (i = 1; i <= n; i++) {11.            scanf("%d%d", &a[i][0][0], &a[i][0][1]);12.            a[i][1][0] = a[i][0][1];13.            a[i][1][1] = a[i][0][0];14.        }15.        flag = 0;16.        for (i = 1; i <= n; i++) {17.            for (x = 0; x <= 1; x++)18.            for (j = 1; j <= n; j++) {19.                if (i == j) continue;20.                for (y = 0; y <= 1; y++)21.                for (k = 1; k <= n; k++) {22.                    if (i == k || j == k) continue;23.                    for (z = 0; z <= 1; z++) {24.                        if (a[i][x][0] + a[j][y][0] == a[k][z][0]25.                        && a[i][x][1] == a[j][x][1]) flag = 1;26.                        if (a[i][x][0] == a[j][y][0]27.                        && a[i][x][0] == a[k][z][0]) flag = 1;28.                    }29.                }30.            }31.        }32.        if (flag) {33.            printf("Yes\n");34.        } else {35.            printf("No\n");36.        }37.    }38.}

反馈

0 0
原创粉丝点击