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;
}

参考: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

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

  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

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的更多相关文章

  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. PHP通过iconv将字符串从GBK转换为UTF8字符集

    PHP通过iconv将字符串从GBK转换为UTF8字符集的方法,需要的朋友可以参考下. 1. iconv()介绍 iconv函数可以将一种已知的字符集文件转换成另一种已知的字符集文件.例如:从GB23 ...

  2. 使用fiddler进行接口测试

    我们来说说如何使用fiddler做接口测试? 测试准备,抓到相应的接口和入参或者找接口文档,我这里就用聚合数据里面的接口做样例, 接口如下: 测试接口:http://japi.juhe.cn/qqev ...

  3. bcb ole拖拽功能的实现

    最近项目中用到了OLE 拖拽功能 和BCB 一个Form的Drag 不同的是,只有实现了OLE 拖拽才能,从其他程序拖拽数据到Form 下面的代码实现了,同HTML网页拖拽到Form时,Form获得H ...

  4. js-虚拟dom

    问题: vdom是什么?为什么存在vdom? vdom是如何应用的,核心的api是什么? 介绍一下diff算法 1.一些虚拟dom应用了snabbdom.其中的 h函数相当于渲染成了右侧的JS虚拟节点 ...

  5. virtualBox+centOS的一些报错

    step1: 安装系统后进入命令行模式 安装virtualBox:https://www.virtualbox.org/wiki/Downloads 下载centOS7镜像:https://www.c ...

  6. dpkg: error processing package XXX (--configure) 解决方法 (ubuntu右上角红色警告)

    在 Ubuntu 执行 sudo apt-get upgrade 时,出现了如下的报错: Setting up bluez (4.101-0ubuntu13.1) ... reload: Job is ...

  7. 洛谷P1373 小a和uim之大逃离【线性dp】

    题目:https://www.luogu.org/problemnew/show/P1373 题意: 有一个n*m的地图,每个点上有一个数值.两个人在任一点开始任一点结束,只能往右或往下走,轮流收集数 ...

  8. Activation HDU - 4089 (概率DP)

    kuangbin的博客 强 #include <bits/stdc++.h> using namespace std; const int MAXN = 2005; const doubl ...

  9. 7、Spring Boot 2.x 集成 Redis

    1.7 Spring Boot 2.x 集成 Redis 简介 继续上篇的MyBatis操作,详细介绍在Spring Boot中使用RedisCacheManager作为缓存管理器,集成业务于一体. ...

  10. python自动华 (一)

    Python自动化 [第一篇]:Python简介和入门 Python简介: 一.什么是python Python是一门动态解释性的强类型定义语言. pythonde 特点:“优雅”.“明确”.“简单” ...