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. hibernate使用注解生成表,有时无法生成数据表的原因

    待生成表中有字段“desc”或“descripe”等和hibernate关键字,导致和hibernate冲突

  2. cannot find -llapack + -lblas

    问题: cannot find -llapack + -lblas 解决: sudo apt-get install libblas-dev liblapack-dev 转:https://suppo ...

  3. Insufficient space for shared memory file 解决办法

    Java HotSpot(TM) 64-Bit Server VM warning: Insufficient space for shared memory file:   /tmp/hsperfd ...

  4. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  5. alibaba 用360的evpp -》个别项目

  6. linux实操_shell运算符

    1."$((运算式))"或"[运算式]" 2.expr m + n 注意:expr运算符要有空格 3.expr m - n 4.expr \*,/,/% 乘,除 ...

  7. 001-官网安装openstack之-安装前基础环境准备

    0.安装常用软件包(根据个人习惯安装需要的软件包) [root@localhost ~]# yum -y install wget vim ntp net-tools tree openssh 1.配 ...

  8. python自动华 (三)

    Python自动化 [第三篇]:Python基础-集合.文件操作.字符编码与转码.函数 1.        集合 1.1      特性 集合是一个无序的,不重复的数据组合,主要作用如下: 去重,把一 ...

  9. 检查Object是否存在某个属性

    1. in 和 hasOwnProperty in会检查对象和它的整条原型链,hasOwnProperty只会检查对象本身,不会检查原型链 let a = {name: 'rick'} let b = ...

  10. 3-自定义构造方法和description方法

    http://www.cnblogs.com/mjios/archive/2013/04/19/3031412.html -自定义构造方法和description方法 1 默认的构造方法是什么?有什么 ...