编程练习 15.11.16~15.11.23

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

    • Simple_Array
      • 读题
      • my answer
      • the standard answer
      • 反馈
    • Matrix addition
      • 读题
      • my answer
      • the standard answer
      • 反馈
    • How long will they wait
      • 读题
      • my answer
      • the standard answer
      • 反馈
    • Fibonacci
      • 读题
      • my answer
      • the standard answer
      • 反馈
    • Catch fish
      • 读题
      • my answer
      • the standard answer
      • 反馈
    • 3n 1数链问题
      • 读题
      • my answer
      • the standard answer
      • 反馈
    • 积木分发
      • 读题
      • my answer
      • the standard answer
      • 反馈


Simple_Array

输入10个整数,并放入一个一维数组中,然后将其前5个元素与后5个元素对换,即:第1个元素与第6个元素互换,第2个元素与第7个元素互换……第5个元素与第10个元素互换。分别输出数组原来各元素的值和对换后各元素的值。

输入:10个整数

输出:两行输出,第一行为原数组的值,第二行为转换后数组的值,数组中每个元素用空格隔开,最后一个元素后也有一个空格。

Sample:

input:

1 2 3 4 5 6 7 8 9 0

output:

1 2 3 4 5 6 7 8 9 0

6 7 8 9 0 1 2 3 4 5

注意:每个数字后都有一个空格,最后一行有换行。

读题

my answer

#include<stdio.h>2.int main() {3.    int a[12] = {0};4.    int i;5.    for (i = 0; i < 10; i++)6.        scanf("%d", &a[i]);7.    for (i = 0; i < 10; i++)8.        printf("%d ", a[i]);9.    printf("\n");10.    for (i = 5; i < 10; i++)11.        printf("%d ", a[i]);12.    for (i = 0; i < 5; i++)13.        printf("%d ", a[i]);14.    printf("\n");15.    return 0;16.}

the standard answer

#include <stdio.h>2.#define N 103. 4.int main() {5.    int i, a[N + 1];6.    for (i = 1; i <= N; i++) {7.        scanf("%d", &a[i]);8.    }9.    for (i = 1; i <=N; i++) {10.        printf("%d ", a[i]);11.    }12.    printf("\n");13.    for (i = 1; i <= N/2; i++) {14.        a[0] = a[i];15.        a[i] = a[N/2 + i];16.        a[N/2 + i] = a[0];17.    }18.    for (i = 1; i <=N; i++) {19.        printf("%d ", a[i]);20.    }21.    printf("\n");22.}

反馈

Matrix addition

There are two n*n(2 <= n <= 10) matrixes A and B. (0 <= The digit of matrix <= 10000)

Please calculate matrix C (C = A + B).And then output the digits on matrix C’s diagonal line.

Input format:

the size of matrix

n*n digits in matrix A

n*n digits in matrix B

output format:(the digits on matrix C’s diagonal line)

1.behind every digit, there will be a ’ ‘(space).

2.no ‘\n’.

For example:


[Input]

3
1 2 3
4 5 6
7 8 9
7 8 9
4 5 6
1 2 3

[Output]

8 10 12


tips:

1.After A + B

8 10 128 10 128 10 12

2.output means that:

8+’ ‘+10+’ ‘+12+’ ‘

No ‘\n’

读题

my answer

#include<stdio.h>2.int main() {3.    int a[12][12] = {0};4.    int n, i, j, temp;5.    scanf("%d", &n);6.    for(i = 0; i < n; i++)7.        for (j = 0; j < n; j++)8.            scanf("%d", &a[i][j]);9.    for (i = 0; i < n; i++)10.        for (j = 0; j < n; j++) {11.            scanf("%d", &temp);12.            a[i][j] += temp;13.        }14.    for(i = 0; i < n; i++)15.        printf("%d ", a[i][i]);16.    return 0;17.}

the standard answer

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

反馈

How long will they wait

n(2 <= n <= 50) people line up and wait to get money from ATM.

0 <= the time of using AMT to get money <= 300

Please calculate the wating time of them and output the sum of their wating time.

Input format:

The number of people(n)

time1 time2 time3 …. timen (the time of using AMT to get money)

Output format:

The sum of their wating time + ‘\n’

For example:


[Input]

5

1 3 2 4 5

[Output]

21


tips:

the waiting time of the poeple

first: 0

second: 1

third: 1 + 3 = 4

fourth: 1 + 3 + 2 = 6

fifth: 1 + 3 + 2 + 4 = 10

sum of their wating time: 0 + 1 + 3 + 6 + 10 = 21

output 21 + ‘\n’

读题

my answer

#include<stdio.h>2.int main() {3.    int a[50] = {0};4.    int i, n, time;5.    int sum = 0;6.    scanf("%d", &n);7.    for (i = 1; i < n; i++) {8.        scanf("%d", &time);9.        a[i] = a[i - 1] + time;10.        sum += a[i];11.    }12.    printf("%d\n", sum);13.    return 0;14.}

the standard answer

#include <stdio.h>2. 3.int main() {4.    int n, i, j, sum;5.    int t[51];6. 7.    sum = 0;8.    scanf("%d", &n);9.    for (i = 0; i < n; ++i)10.        scanf("%d", &t[i]);11. 12.    for (i = 1; i < n; ++i)13.        for (j = 0; j < i; ++j)14.            sum += t[j];15. 16.    printf("%d\n", sum);17. 18.    return 0;19.}

反馈

Fibonacci

Fibonacci sequence:

F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2,n∈N*)

give a number t(2 <= t <= 5), it means that there will be t numbers(n) next.

please output every F(n) + ‘\n’

For example

[Input]

5
2 4 5 3 6

[Output]

1
3
5
2
8
for exmaple:

0、1、1、2、3、5、8、13、21、34、……

不用考虑F(0),F(1)的情况。

当n较大时,F(n)很大,所以要用long–%ld

每次都要初始化

读题

my answer

#include<stdio.h>2.int main() {3.    int t, n, i, j;4.    long ans, temp, x, y;5.    scanf("%d", &t);6.    for (i = 0; i < t; i++) {7.        x = 1; y = 0;8.        scanf("%d", &n);9.        for (j = 2; j <= n; j++) {10.            ans = x + y;11.            temp = x;12.            x = x + y;13.            y = temp;14.        }15.        printf("%ld\n", ans);16.    }17.    return 0;18.}

the standard answer

#include <stdio.h>2. 3.int main() {4.    long a = 0;5.    long b = 1;6.    int i, n, time;7.    long temp;8. 9.    scanf("%d", &time);10.    while (time--) {11.        a = 0;12.        b = 1;13.        scanf("%d", &n);14.        for (i = 2; i <= n; ++i) {15.            temp = a + b;16.            a = b;17.            b = temp;18.        }19.        printf("%ld\n", b);20.    }21. 22.    return 0;23.}

反馈

Catch fish

You can use the fishing net to catch fish. If the holes size of net is smaller than or equal to(<=) the fish size, you can catch fish.

Give the hole size(1~100) of net and n(1 <= n <=100) fish size(1~10000)ouput how many fish you can catch.

input format:

hole_size fish_number(n)

fish1 fish2 fish3 … fishn ——–every fish size

output format:

number of fish you can catch + ‘\n’

For example


[Input]

20 10
1 3 5 7 9 11 23 35 47 59

[Output]

4


tips:

23, 35, 47, 59 >= 20. so you can catch 4 fish, output 4 + ‘\n’

读题

my answer

#include<stdio.h>2.int main() {3.    int i, hole, n, fish, count;4.    count = 0;5.    scanf("%d%d", &hole, &n);6.    for (i = 0; i < n; i++) {7.        scanf("%d", &fish);8.        if (hole <= fish)9.        count++;10.    }11.    printf("%d\n", count);12.    return 0;13.}

the standard answer

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

反馈

3*n + 1数链问题

有这样一个问题,如下所示:
1.输入一个正整数n。
2.如果n=1则结束。
3.如果n是奇数则n变为3*n+1,否则n变为n/2。
4.转入第2步。

例如对于正整数22,则数列为:

22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

对于任意一个正整数,经过以上算法最终会推到1。对于给定的正整数n,我们把显示出来的数的个数定义为n的链长,例如22的链长为16。

对于任意一对正整数i和j,求i、j之间每个数(包括i和j这两个边界)其中的最长链长。

输入:

3 7

输出:

17

读题

my answer

#include<stdio.h>2.int myfun(int n);3. 4.int myfun(int n) {5.    int count = 1;6.    while (n != 1) {7.       if (n % 2) {8.          n = 3 * n + 1;9.          count++;10.       } else {11.           n /= 2;12.           count++;13.       }14.    }15.    return count;16.}17. 18.int main() {19.    int i, j, ans, temp;20.    scanf("%d%d", &i, &j);21.    if (i > j) {22.        temp = i;23.        i = j;24.        j = temp;25.    }26.    ans = 1;27.    for (; i <= j; i++) {28.        temp = myfun(i);29.        ans = (temp > ans)? temp: ans;30.    }31.    printf("%d\n", ans);32.    return 0;33.}

the standard answer

#include<stdio.h>2. 3.int dealWith(int t);4. 5.int main(void) {6.    int m, n, i, t, max = 0;7.    scanf("%d%d", &m, &n);8.    // swap them if necessary.9.    if (m > n) {10.        t = m;11.        m = n;12.        n = t;13.    }14. 15.    for (i = m; i <= n; i++) {16.        t = dealWith(i);17.        max = (max < t)?t:max;18.    }19.    printf("%d\n", max);20.    return 0;21.}22. 23.int dealWith(int t) {24.    int len = 1;25.    while (t != 1) {26.        len++;27.        if (t%2) t = 3*t + 1;28.        else t /= 2;29.    }30.    return len;31.}

反馈

积木分发

Description

歌手The Pancakes到幼儿园跟小朋友玩耍,她到达的时候小朋友们已经争着积木玩了。小朋友都想要更多的积木砌一个自己喜欢的图形,砌完就可以和The Pancakes合照。同时,The Pancakes手上还有一些积木,她可以把手上的这些积木全部给一个小朋友,然后等该小朋友砌完后就可以收回所发的积木和该小朋友原先手上的积木。但她不知道能否让所有的小朋友都和她合照,聪明的你可以帮助她吗?

Input

输入包含多个数据。

每个数据的第一行是两个正整数n和s,1≤n≤10000,1≤s≤1000000,表示一共有n位小朋友,The Pancakes手上有s块积木。以下有n行,每行有两个正整数,a和b,1≤a,b≤10^9,表示第i个小朋友手上有a块积木,还需要b块积木才能够砌完。

输入n=0时表示结束。

Output

如果可以让所有小朋友都和Pancake合照,就输出YES;否则,输出NO。

Sample Input
2 2
1 4
2 1
2 2
1 4
1 1
0 0

Sample Output
YES
NO

Problem Source

ZSUACM Team Member

读题

my answer

#include<stdio.h>2.int main() {3.    int i, j, temp;4.    long n, s;5.    for (scanf("%d%d", &n, &s); n != 0; scanf("%d%d", &n, &s)) {6.        long have[10001] = {0};7.        long need[10001] = {0};8.        for (i = 0; i < n; i++)9.            scanf("%d%d", &have[i], &need[i]);10.        for (i = n - 1; i > 0; i--) {11.            for (j = 0; j < i; j++) {12.                 if (need[i] < need[j]) {13.                    temp = need[i];14.                    need[i] = need[j];15.                    need[j] = temp;16.                    temp = have[i];17.                    have[i] = have[j];18.                    have[j] = temp;19.                  }20.            }21.        }22. 23.        for (i = 0; i < n; i++) {24.            if (s >= need[i]) {25.                s += have[i];26.            } else {27.                break;28.            }29.        }30.        if (i == n) {31.        printf("YES\n");32.        } else {33.            printf("NO\n");34.        }35.    }36.    return 0;37.}

the standard answer

#include <stdio.h>2. 3.int main() {4.    long n, s, i, temp1, temp2, j, sum;5.    long a[10000], b[10000];6.    while (scanf("%ld", &n) == 1) {7.        if (n == 0) {8.            break;9.        } else {10.            scanf("%ld", &s);11.            for (i = 0; i < n; i++) {12.                scanf("%ld %ld", &a[i], &b[i]);13.            }14.            for (i = 0; i < n - 1; i++) {15.                for (j = 0; j < n - i - 1; j++) {16.                    if (b[j] > b[j + 1]) {17.                        temp1 = b[j];18.                        b[j] = b[j + 1];19.                        b[j + 1] = temp1;20.                        temp2 = a[j];21.                        a[j] = a[j + 1];22.                        a[j + 1] = temp2;23.                    }24.                }25.            }26.            sum = 0;27.            for (i = 0; i < n; i++) {28.                if (s >= b[i]) {29.                    s = s + a[i];30.                    sum++;31.                }32.            }33.            if (sum == n) printf("YES\n");34.            else printf("NO\n");35.        }36.    }37.    return 0;38.}

反馈

0 0
原创粉丝点击