generated from kenryuS/report-temp
cls03
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
include ../common.mk
|
||||
|
||||
PROJECT_NAME:=cls03
|
||||
|
||||
TGTS:=$(patsubst %.c,%,$(wildcard *.c))
|
||||
|
||||
all: $(TGTS)
|
||||
|
||||
%: %.c
|
||||
@mkdir -p $(BUILD_PATH)/$(PROJECT_NAME)
|
||||
$(CC) $(CFLAGS) -I. $^ -o $(BUILD_PATH)/$(PROJECT_NAME)/$@
|
||||
|
||||
clean:
|
||||
$(RM) -drf $(BUILD_PATH)/$(PROJECT_NAME)
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
|
||||
unsigned int sum_recc(unsigned int n) {
|
||||
if (n == 0) return 0;
|
||||
return n + sum_recc(n - 1);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
unsigned int x = 0;
|
||||
|
||||
printf("Input natural number: ");
|
||||
(void)scanf("%u", &x);
|
||||
|
||||
unsigned int s = sum_recc(x);
|
||||
|
||||
printf("Result: %u\n", s);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void getTwoInts(int* a, int* b) {
|
||||
printf("1st Int: ");
|
||||
(void)scanf("%d", a);
|
||||
printf("2nd Int: ");
|
||||
(void)scanf("%d", b);
|
||||
}
|
||||
|
||||
void printRatio(int r[]) {
|
||||
printf("%d/%d", r[0], r[1]);
|
||||
}
|
||||
|
||||
int gcm(int a, int b);
|
||||
int lcm(int a, int b);
|
||||
void reduce(int x[]);
|
||||
|
||||
int gcm(int a, int b) {
|
||||
int x = a, y = b;
|
||||
if (x < y) {
|
||||
x = b;
|
||||
y = a;
|
||||
}
|
||||
int r = a % b;
|
||||
if (r == 0) return b;
|
||||
return gcm(b, r);
|
||||
}
|
||||
|
||||
int lcm(int a, int b) {
|
||||
int g = gcm(a, b);
|
||||
return b * a / g;
|
||||
}
|
||||
|
||||
void reduce(int x[]) {
|
||||
int a = x[0], b = x[1];
|
||||
int g = gcm(a, b);
|
||||
x[0] = a / g;
|
||||
x[1] = b / g;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int a,b;
|
||||
|
||||
getTwoInts(&a, &b);
|
||||
int g = gcm(a, b);
|
||||
int l = lcm(a, b);
|
||||
|
||||
printf("Result:\ngcm = %d\nlcm = %d\n", g, l);
|
||||
|
||||
int r[2] = {a, b};
|
||||
printf("Before: ");
|
||||
printRatio(r); putchar('\n');
|
||||
reduce(r);
|
||||
printf("After: ");
|
||||
printRatio(r); putchar('\n');
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user