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

参考: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. float innerProduct(float a[], float b[], int n)
  2. {
  3. int i;
  4. float sep, sum = 0.0;
  5. for (i = 0; i < n; i++)
  6. {
  7. sep = a[i] * b[i];
  8. sum = sum + sep;
  9. }
  10. return sum;
  11. }

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.

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

数据调用

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

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

※ 在字符串赋值的过程中,最后需要添加 '\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:

    1. Fib[1] = 1:
    2. Fib[2] = 1:
    3. Fib[3] = 2: 1
    4. Fib[4] = 3: 10 5 16 8 4 2 1

a - code

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

b - code

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

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

调用数据:

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

参考: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. int largest(int a, int b, int c)
  2. {
  3. int max;
  4. max = (a > b) ? a: b;
  5. max = (max > c) ? max : c;
  6. return max;
  7. }

【403】COMP9024 Exercise的更多相关文章

  1. 【434】COMP9024 Exercises Revision

    目录: Week01 Week02 Week03 Week04 Week05 Week06 Week07 Week08 Week09 Week10 01. Week01 数字通过 #define 来定 ...

  2. 【411】COMP9024 Assignment1 问题汇总

    1. 构建 Makefile 文件后运行错误,undefined reference to 'sqrt' 实际上是没有链接math数学库,所以要 $gcc test.c –lm //-lm就是链接到m ...

  3. 【433】COMP9024 复习

    目录: 01. Week01 - Lec02 - Revision and setting the scene 02. Week02 - Lec01 - Data structures - memor ...

  4. 【423】COMP9024 Revision

    目录: array '\0' 与 EOF 二维字符数组(字符串数组) 1. array: 参考:C 数组 参考:C 字符串 参考:C笔记之NULL和字符串结束符'\0'和EOF 总结:[个人理解,可能 ...

  5. 【AtCoder】【思维】【置换】Rabbit Exercise

    题意: 有n只兔子,i号兔子开始的时候在a[i]号位置.每一轮操作都将若干只兔子依次进行操作: 加入操作的是b[i]号兔子,就将b[i]号兔子移动到关于b[i]-1号兔子现在所在的位置对称的地方,或者 ...

  6. 【AGC006C】Rabbit Exercise 置换

    题目描述 有\(n\)只兔子站在数轴上.为了方便,将这些兔子标号为\(1\ldots n\).第\(i\)只兔子的初始位置为\(a_i\). 现在这些兔子会按照下面的规则做若干套体操.每一套体操由\( ...

  7. 【agc006C】Rabbit Exercise

    Portal --> agc006C Solution 啊感觉是好有意思的一道题qwq官方题解里面的说辞也是够皮的哈哈哈..(大概就是说如果你没有意识到那个trick的话这题这辈子都做不出来qw ...

  8. 【432】COMP9024,Exercise9

    eulerianCycle.c What determines whether a graph is Eulerian or not? Write a C program that reads a g ...

  9. 【UFLDL】Exercise: Convolutional Neural Network

    这个exercise需要完成cnn中的forward pass,cost,error和gradient的计算.需要弄清楚每一层的以上四个步骤的原理,并且要充分利用matlab的矩阵运算.大概把过程总结 ...

随机推荐

  1. 快看,那个学SLAM 的崩溃了!

    点"计算机视觉life"关注,置顶更快接收消息! 本文列举了当前优秀SLAM方案,点出了SLAM学习者的困境,最后打算搞点大事 请把此文转发给你认识的SLAM大神,愿你头发浓密,心 ...

  2. Euler's Sum of Powers Conjecture

    转帖:Euler's Sum of Powers Conjecture 存不存在四个大于1的整数的五次幂恰好是另一个整数的五次幂? 暴搜:O(n^4) 用dictionary:O(n^3) impor ...

  3. MongoDB常用语句大全

    原文出处:https://www.cnblogs.com/--smile/p/11055204.html 直接输入mongo进入数据库 查询操作 查看当前数据库版本 db.version() //4. ...

  4. 从运行时的工作空间获取EMF文件(IFILE)

    //EMFFILE_URI为EMF文件的URI String uriString = EMFFILE_URI.trimFragment().toPlatformString(true); if (ur ...

  5. 设置了msconfig处理器个数和内存开不了机终极解决办法

    1.进入 启动修复 的 命令提示符(最好是使用有管理员权限的,不过普通用户我也每试过), 使用 bcdedit 命令来查看. 2.可以查看到你的启动参数. 确认 truncatememory 是否为 ...

  6. BZOJ 3566 概率充电器(树形概率DP)

    题面 题目传送门 分析 定义f(i)f(i)f(i)为iii点不被点亮的概率,p(i)p(i)p(i)为iii自己被点亮的概率,p(i,j)p(i,j)p(i,j)表示i−ji-ji−j 这条边联通的 ...

  7. HDU 3824/ BZOJ 3963 [WF2011]MachineWorks (斜率优化DP+CDQ分治维护凸包)

    题面 BZOJ传送门(中文题面但是权限题) HDU传送门(英文题面) 分析 定义f[i]f[i]f[i]表示在iii时间(离散化之后)卖出手上的机器的最大收益.转移方程式比较好写f[i]=max{f[ ...

  8. HDU 6154 - CaoHaha's staff | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    /* HDU 6154 - CaoHaha's staff [ 构造,贪心 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 整点图,每条线只能连每个方格的边或者对角线 问面积大于n的 ...

  9. MySQL Group Replication-MGR集群简介

    简介 MySQL Group Replication(简称MGR)字面意思是mysql组复制的意思,但其实他是一个高可用的集群架构,暂时只支持mysql5.7和mysql8.0版本. 是MySQL官方 ...

  10. 用HTML5里的window.postMessage在两个网页间传递数据

    说明 window.postMessage()方法可以安全地实现Window对象之间的跨域通信.例如,在一个页面和它生成的弹出窗口之间,或者是页面和嵌入其中的iframe之间. 通常情况下,不同页面上 ...