alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【403】COMP9024 Exercise

Week 1 Exercises

fiveDigit.c

There is a 5-digit number that satisfies 4 * abcde = edcba, that is,when multiplied by 4 yields the same number read backwards.Write a C-program to find this number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int swap_num(int a)
{
    int result = 0;
 
    while (1)
    {
        int i = a % 10;
        result = result * 10 + i;
        a = a / 10;
        if (a == 0)
            break;
    }
    return result;
}

参考:c语言编程:实现数字的翻转

 

innerProdFun.c

Write a C-function that returns the inner product of two n-dimensional vectorsa and b, encoded as 1-dimensional arrays of n floating point numbers.

Use the function prototype float innerProduct(float a[], float b[], int n).

By the way, the inner product of two vectors is calculated by the sum for i=1..n of ai * bi

1
2
3
4
5
6
7
8
9
10
11
float innerProduct(float a[], float b[], int n)
{
    int i;
    float sep, sum = 0.0;
    for (i = 0; i < n; i++)
    {
        sep = a[i] * b[i];
        sum = sum + sep;
    }
    return sum;
}

matrixProdFun.c

Write a C-function to compute C as the matrix product of matrices A and B.

Use the function prototype void matrixProduct(float a[M][N], float b[N][P], float c[M][P])

You can assume that M, N, P are given as symbolic constants, e.g.

#define M 3
#define N 4
#define P 4

By the way, the product of an m x n matrix A and an n x p matrix B is the m x p matrix C such that Cij is the sum for k=1..n of Aik * Bkj for all i∈{1..m} and j∈{1..p}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void matrixProduct(float a[M][N], float b[N][P], float c[M][P])
{
    float innerProduct(float a[], float b[], int n);
 
    int i, j, k;
    float sum;
    float rr[N], cc[N];
 
    for (i = 0; i < M; i++)
    {
        for (j = 0; j < N; j++)
            rr[j] = a[i][j];
 
        for (k = 0; k < P; k++)
        {
            for (j = 0; j < N; j++)
                cc[j] = b[j][k];
 
            sum = innerProduct(rr, cc, N);
            c[i][k] = sum;
        }
    }
}

数据调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
 
#define M 2
#define N 3
#define P 2
 
int main()
{
    void matrixProduct(float a[M][N], float b[N][P], float c[M][P]);
    float innerProduct(float a[], float b[], int n);
 
    float a[2][3] = { 1, 2, 3, 4, 5, 6 };
    float b[3][2] = { 1, 2, 3, 4, 5, 6 };
 
    float c[2][2];
    matrixProduct(a, b, c);
 
    int i, j;
    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 2; j++)
        {
            printf("%0.0f  ", c[i][j]);
        }
        printf("\n");
    }
 
    return 0;
}

able.c

Write a C-program that outputs, in alphabetical order, all strings that use each of the characters 'a', 'b', 'l', 'e' exactly once.

How many strings are there actually?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void able()
{
    char a = 'a';
    char b = 'b';
    char l = 'l';
    char e = 'e';
    char z = 'z';
 
    char rr[26];
    char ss[26];
 
    int i, j, tmp, flag;
 
    for (i = (int)a; i <= (int)z; i++)
    {
        flag = 0;
 
        if (i == (int)a || i == (int)b || i == (int)l || i == (int)e)
            flag++;
 
        *(rr) = (char)i;
 
        if (flag == 1)
            printf("%c\n", i);
 
        for (j = i + 1; j <= (int)z; j++)
        {
            if (j == (int)a || j == (int)b || j == (int)l || j == (int)e)
                flag++;
 
            if (flag > 1)
            {
                break;
                *(rr + j - i + 1) = '\0';
            }
 
            *(rr + j - i) = (char)j;
 
            if (flag == 1)
            {
                *(rr + j - i + 1) = '\0';
                printf("%s\n", rr);
            }
        }
    }
}

※ 在字符串赋值的过程中,最后需要添加 '\0',否则会乱码。

 

collatzeFib.c

  1. Write a C-function that takes a positive integer n as argument and prints a series of numbers generated by the following algorithm, until 1 is reached:

    • if n is even, then nn/2

    • if n is odd, then n ← 3*n+1

    (Before you start programming, calculate yourself the series corresponding to n=3.)

  2. The Fibonacci numbers are defined as follows:
    • Fib(1) = 1
    • Fib(2) = 1
    • Fib(n) = Fib(n-1)+Fib(n-2) for n≥3

    Write a C program that calls the function in Part a. with the first 10 Fibonacci numbers. The program should print the Fibonacci number followed by its corresponding series. The first 4 lines of the output is as follows:
     Fib[1] = 1: 
     Fib[2] = 1: 
     Fib[3] = 2: 1 
     Fib[4] = 3: 10 5 16 8 4 2 1 

a - code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void even_odd(int n)
{
    if (n < 0)
        printf("Please input a positive integer!!!\n");
    else
        printf("%d\n", n);
 
    while (n != 1)
    {
        if (n % 2 == 0)
            n = n / 2;
        else
            n = 3 * n + 1;
        printf("%d\n", n);
    }
}

b - code

1
2
3
4
5
6
7
8
9
10
int* Fib()
{
    static int ff[10];
    ff[0] = 1;
    ff[1] = 1;
    int i;
    for (i = 2; i < 10; i++)
        ff[i] = ff[i - 2] + ff[i - 1];
    return ff;
}

※ 注意返回数组的方法,另外需要通过 static 关键字来定义数组。

调用数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
int main()
{
    void even_odd(int n);
    int* Fib();
 
    int i;
    int* fib_arr;
    fib_arr = Fib();
 
    for (i = 0; i < 10; i++)
    {
        printf("Fib[%d] = %d:", i + 1, fib_arr[i]);
        even_odd(fib_arr[i]);
    }
 
    return 0;
}

参考:C 从函数返回数组

 

fastMax.c

Write a C-function that takes 3 integers as arguments and returns the largest of them. The following restrictions apply:

  • You are permitted to only use assignment statements, a return statement and Boolean expressions
  • You are not permitted to use if-statements, loops (e.g. a while-statement), function calls or any data or control structures

1
2
3
4
5
6
7
int largest(int a, int b, int c)
{
    int max;
    max = (a > b) ? a: b;
    max = (max > c) ? max : c;
    return max;
}

 

posted on   McDelfino  阅读(449)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2012-06-13 【050】World Flag Database
点击右上角即可分享
微信分享提示