Created
December 27, 2021 14:15
-
-
Save definev/6f34bd9b8fb49b291e2480220b8fe4eb to your computer and use it in GitHub Desktop.
Mid-term 2021
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#include <string.h> | |
char *cau_10(char mssv[]) | |
{ | |
int n; | |
printf("Nhap tham bien n: "); | |
scanf("%d", &n); | |
int m = n % 1000; | |
printf("Cac cap so thoa man la:\n"); | |
for (int y = 0; y * y <= m; y++) | |
{ | |
printf("(%d, %d)\n", m - y * y, y); | |
if (y != 0) | |
{ | |
printf("(%d, -%d)\n", m - y * y, y); | |
} | |
} | |
return mssv; | |
} | |
int isNext(int stack) | |
{ | |
char next; | |
if (stack == 0) | |
{ | |
scanf("%c", &next); | |
return isNext(stack + 1); | |
} | |
printf("\nBan co muon tiep tuc nua hay khong? (y/n)? "); | |
scanf("%c", &next); | |
if (next == 'y' || next == 'Y') | |
{ | |
return 1; | |
} | |
else if (next == 'n' || next == 'N') | |
{ | |
return 0; | |
} | |
else | |
{ | |
printf("Ban da nhap sai cu phap vui long thu lai!"); | |
return isNext(stack); | |
} | |
} | |
void cau_5(char mssv[]) | |
{ | |
double n; | |
printf("Nhap tham bien n: "); | |
scanf("%lf", &n); | |
int m = (int)round(cbrt(n)); | |
if (m % 2 == 0) { | |
m = m - 1; | |
} | |
int ssh = (m - 1) / 2 + 1; | |
int sum = (m + 1) * ssh / 2; | |
printf("Tong day so tu 1 -> %d la: %d", m, sum); | |
} | |
void cau_1(char hvt[]) | |
{ | |
int bytes[strlen(hvt)]; | |
int sum = 0; | |
hvt[strlen(hvt) - 1] = '\0'; | |
printf("Ho va ten: %s", hvt); | |
for (int i = 0; i < strlen(hvt); i++) | |
{ | |
char charAt = hvt[i]; | |
bytes[i] = hvt[i]; | |
printf("\n"); | |
if (bytes[i] % 2 == 0) | |
{ | |
sum += bytes[i]; | |
} | |
char input[2] = " "; | |
while (input[0] != charAt) | |
{ | |
printf("Nhap lai ki tu %c: ", charAt); | |
scanf("%s", input); | |
if (input[0] != charAt) | |
{ | |
printf("Nhap sai vui long nhap lai!\n"); | |
} | |
} | |
} | |
printf("\nDay so tuong ung voi ho va ten la: "); | |
for (int i = 0; i < strlen(hvt); i++) | |
{ | |
printf("%d ", bytes[i]); | |
} | |
printf("\nTong cua cac so chan la: %d", sum); | |
} | |
int main() | |
{ | |
char hvt[30]; | |
char mssv[20]; | |
printf("Ho ten sinh vien: "); | |
fgets(hvt, 30, stdin); | |
printf("Ma so sinh vien: "); | |
fgets(mssv, 20, stdin); | |
int canContinue = 1; | |
while (canContinue == 1) | |
{ | |
char input; | |
printf("\e[1;1H\e[2J"); | |
printf("Lua chon 1: Bai so 10\n"); | |
printf("Lua chon 2: Bai so 5\n"); | |
printf("Lua chon 3: Bai so 1\n"); | |
printf("Nhap lua chon cua ban: ( 1 | 2 | 3 ): "); | |
scanf("%c", &input); | |
switch (input) | |
{ | |
// Cau 10 | |
case '1': | |
cau_10(mssv); | |
canContinue = isNext(0); | |
break; | |
// Cau 5 | |
case '2': | |
cau_5(mssv); | |
canContinue = isNext(0); | |
break; | |
// Cau 1 | |
case '3': | |
cau_1(hvt); | |
canContinue = isNext(0); | |
break; | |
default: | |
printf("Ban da nhap sai vui long thu lai"); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment