Skip to content

Instantly share code, notes, and snippets.

@aziis98
Last active December 29, 2017 14:10
Show Gist options
  • Select an option

  • Save aziis98/776d8e57cd5d49468148c747eeda487f to your computer and use it in GitHub Desktop.

Select an option

Save aziis98/776d8e57cd5d49468148c747eeda487f to your computer and use it in GitHub Desktop.

Esercizi Lezione 9

#include <stdio.h>
#include <stdlib.h>
void print_numero(int n) {
char *exp0[] = {
"",
"uno",
"due",
"tre",
"quattro",
"cique",
"sei",
"sette",
"otto",
"nove"
};
char *exp1[] = {
"",
"",
"vent",
"trent",
"quarant",
"cinquant",
"sessant",
"settant",
"ottant",
"novant"
};
char *deci[] = {
"dieci",
"undici",
"dodici",
"tredici",
"quattordici",
"quindici",
"sedici",
"diciassette",
"diciotto",
"diciannove"
};
if (n > 9 && n < 20) {
printf(deci[n % 10]);
return;
}
printf("%s", exp1[(n / 10) % 10]);
if (n % 10 != 1 && n % 10 != 8) {
if ((n / 10) % 10 == 2) {
printf("i");
}
else if ((n / 10) % 10 > 2) {
printf("a");
}
}
printf("%s", exp0[n % 10]);
}
int main() {
int value;
scanf("%d", &value);
print_numero(value);
}
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[40];
int i = 0;
while(i < 40 && str[i - 1] != '\n') {
str[i++] = getchar();
}
char target = getchar();
int occ = 0;
for (int i = 0; i < 40; i++) {
if (str[i] == target)
occ++;
}
printf("%d\n", occ);
}
#include <stdio.h>
#include <math.h>
int fibonacci(int n) {
double sqrt5 = sqrt(5.0);
double phi = (1.0 + sqrt5) * 0.5;
return (int) ((pow(phi, n) - pow(-phi, -n)) / sqrt5);
}
int main() {
int n;
scanf("%d", &n);
printf("%d\n", fibonacci(n));
}
#include <stdio.h>
#include <stdlib.h>
int is_palindrome(char * string, int size) {
if (size > 1) {
if (string[0] == string[size - 1]) {
return is_palindrome(&string[1], size - 2);
}
else {
return 0;
}
}
else {
return 1;
}
}
void read_str(char * str, int * size, int maxSize) {
while(*size < maxSize && str[*size - 1] != '\n') {
str[(*size)++] = getchar();
}
str[--(*size)] = '\0';
}
int main() {
int count;
scanf("%d", &count);
char *strings = malloc(sizeof(char) * 40 * count);
int *sizes = malloc(sizeof(int) * count);
getchar();
for (int i = 0; i < count; i++) {
read_str(strings + i * 40, sizes + i, 40);
}
for (int i = 0; i < count; i++) {
if (is_palindrome(strings + i * 40, *(sizes + i)))
printf("%s\n", strings + i * 40);
}
for (int i = 0; i < count; i++) {
// printf("'%s'\n", *(strings + i * 40));
if (!is_palindrome(strings + i * 40, *(sizes + i)))
printf("%s\n", strings + i * 40);
}
}
#include <stdio.h>
#include <stdlib.h>
void swap(char * a, char * b) {
char tmp = *b;
*b = *a;
*a = tmp;
}
void reverse(char * string, int size) {
if (size > 1) {
swap(&string[0], &string[size - 1]);
reverse(&string[1], size - 2);
}
}
int main() {
char str[40];
int size = 0;
while(size < 40 && str[size - 1] != '\n') {
str[size++] = getchar();
}
str[--size] = '\0';
// printf("'%s', size=%d\n", str, size);
reverse(str, size);
// printf("'%s', size=%d\n", str, size);
printf("%s", str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment