【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.
- 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;
- }
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
- 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}
- 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;
- }
- }
- }
数据调用
- #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?
- 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
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 n ← n/2
if n is odd, then n ← 3*n+1
(Before you start programming, calculate yourself the series corresponding to n=3.)
- 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
- 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
- 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 关键字来定义数组。
调用数据:
- #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
- int largest(int a, int b, int c)
- {
- int max;
- max = (a > b) ? a: b;
- max = (max > c) ? max : c;
- return max;
- }
【403】COMP9024 Exercise的更多相关文章
- 【434】COMP9024 Exercises Revision
目录: Week01 Week02 Week03 Week04 Week05 Week06 Week07 Week08 Week09 Week10 01. Week01 数字通过 #define 来定 ...
- 【411】COMP9024 Assignment1 问题汇总
1. 构建 Makefile 文件后运行错误,undefined reference to 'sqrt' 实际上是没有链接math数学库,所以要 $gcc test.c –lm //-lm就是链接到m ...
- 【433】COMP9024 复习
目录: 01. Week01 - Lec02 - Revision and setting the scene 02. Week02 - Lec01 - Data structures - memor ...
- 【423】COMP9024 Revision
目录: array '\0' 与 EOF 二维字符数组(字符串数组) 1. array: 参考:C 数组 参考:C 字符串 参考:C笔记之NULL和字符串结束符'\0'和EOF 总结:[个人理解,可能 ...
- 【AtCoder】【思维】【置换】Rabbit Exercise
题意: 有n只兔子,i号兔子开始的时候在a[i]号位置.每一轮操作都将若干只兔子依次进行操作: 加入操作的是b[i]号兔子,就将b[i]号兔子移动到关于b[i]-1号兔子现在所在的位置对称的地方,或者 ...
- 【AGC006C】Rabbit Exercise 置换
题目描述 有\(n\)只兔子站在数轴上.为了方便,将这些兔子标号为\(1\ldots n\).第\(i\)只兔子的初始位置为\(a_i\). 现在这些兔子会按照下面的规则做若干套体操.每一套体操由\( ...
- 【agc006C】Rabbit Exercise
Portal --> agc006C Solution 啊感觉是好有意思的一道题qwq官方题解里面的说辞也是够皮的哈哈哈..(大概就是说如果你没有意识到那个trick的话这题这辈子都做不出来qw ...
- 【432】COMP9024,Exercise9
eulerianCycle.c What determines whether a graph is Eulerian or not? Write a C program that reads a g ...
- 【UFLDL】Exercise: Convolutional Neural Network
这个exercise需要完成cnn中的forward pass,cost,error和gradient的计算.需要弄清楚每一层的以上四个步骤的原理,并且要充分利用matlab的矩阵运算.大概把过程总结 ...
随机推荐
- 快看,那个学SLAM 的崩溃了!
点"计算机视觉life"关注,置顶更快接收消息! 本文列举了当前优秀SLAM方案,点出了SLAM学习者的困境,最后打算搞点大事 请把此文转发给你认识的SLAM大神,愿你头发浓密,心 ...
- Euler's Sum of Powers Conjecture
转帖:Euler's Sum of Powers Conjecture 存不存在四个大于1的整数的五次幂恰好是另一个整数的五次幂? 暴搜:O(n^4) 用dictionary:O(n^3) impor ...
- MongoDB常用语句大全
原文出处:https://www.cnblogs.com/--smile/p/11055204.html 直接输入mongo进入数据库 查询操作 查看当前数据库版本 db.version() //4. ...
- 从运行时的工作空间获取EMF文件(IFILE)
//EMFFILE_URI为EMF文件的URI String uriString = EMFFILE_URI.trimFragment().toPlatformString(true); if (ur ...
- 设置了msconfig处理器个数和内存开不了机终极解决办法
1.进入 启动修复 的 命令提示符(最好是使用有管理员权限的,不过普通用户我也每试过), 使用 bcdedit 命令来查看. 2.可以查看到你的启动参数. 确认 truncatememory 是否为 ...
- 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 这条边联通的 ...
- HDU 3824/ BZOJ 3963 [WF2011]MachineWorks (斜率优化DP+CDQ分治维护凸包)
题面 BZOJ传送门(中文题面但是权限题) HDU传送门(英文题面) 分析 定义f[i]f[i]f[i]表示在iii时间(离散化之后)卖出手上的机器的最大收益.转移方程式比较好写f[i]=max{f[ ...
- HDU 6154 - CaoHaha's staff | 2017 中国大学生程序设计竞赛 - 网络选拔赛
/* HDU 6154 - CaoHaha's staff [ 构造,贪心 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 整点图,每条线只能连每个方格的边或者对角线 问面积大于n的 ...
- MySQL Group Replication-MGR集群简介
简介 MySQL Group Replication(简称MGR)字面意思是mysql组复制的意思,但其实他是一个高可用的集群架构,暂时只支持mysql5.7和mysql8.0版本. 是MySQL官方 ...
- 用HTML5里的window.postMessage在两个网页间传递数据
说明 window.postMessage()方法可以安全地实现Window对象之间的跨域通信.例如,在一个页面和它生成的弹出窗口之间,或者是页面和嵌入其中的iframe之间. 通常情况下,不同页面上 ...