最大公约数
方法一:暴力求解
//穷举法 #include
int gcd(int a, int b) { if (a == 0) return b; else if (b == 0) return a; else if(a == b) return a;//或者b int gcd_ret = a > b ? b : a; while (gcd_ret > 1) { if ((a % gcd_ret == 0) && (b % gcd_ret == 0)) break; gcd_ret--; } return gcd_ret; } int main() { int a, b; printf("请输入两个整数:"); scanf("%d%d", &a, &b); int gcd_ret = gcd(a, b); printf("最大公约数为:%d\n", gcd_ret); return 0; } 方法二:更相减损法
//相减法 int gcd(int a, int b) { if (a == 0) return b; else if (b == 0) return a; else if(a == b) return a;//或者b int gcd_ret = 0; while (a != b) { gcd_ret = a > b ? (a -= b) : (b -= a); } return gcd_ret; } int main() { int a, b; printf("请输入两个整数:"); scanf("%d%d", &a, &b); int gcd_ret = gcd(a, b); printf("最大公约数为:%d\n", gcd_ret); return 0; }
方法三:辗转相除法
//辗转相除法 int gcd(int a, int b) { if (a == 0) return b; else if (b == 0) return a; else if (a == b) return a;//或者b int mod = a % b; while (mod) { a = b; b = mod; mod = a % b; } return b; } int main() { int a, b; printf("请输入两个整数:"); scanf("%d%d", &a, &b); int gcd_ret = gcd(a, b); printf("最大公约数为:%d\n", gcd_ret); return 0; }
方法四:递归写法
//辗转相除法---递归实现 int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } int main() { int a, b; printf("请输入两个整数:"); scanf("%d%d", &a, &b); int gcd_ret = gcd(a, b); printf("最大公约数为:%d\n", gcd_ret); return 0; }
最小公倍数
方法一:暴力求解
//穷举法 int lcm(int a, int b) { if (a * b == 0) return 0; int lcm_ret = a > b ? a : b; while (1) { if ((lcm_ret % a == 0) && (lcm_ret % b == 0)) break; lcm_ret++; } return lcm_ret; } int main() { int a, b; printf("请输入两个整数:"); scanf("%d%d", &a, &b); int lcm_ret = lcm(a, b); printf("最小公倍数为:%d\n", lcm_ret); return 0; }
方法二:公式法
//辗转相除法---递归实现 int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } //公式法 lcm = a * b / gcd(a,b) int lcm(int a, int b) { if (a * b == 0) return 0; return a * b / gcd(a, b); } int main() { int a, b; printf("请输入两个整数:"); scanf("%d%d", &a, &b); int gcd_ret = gcd(a, b); printf("最大公约数为:%d\n", gcd_ret); int lcm_ret = lcm(a, b); printf("最小公倍数为:%d\n", lcm_ret); return 0; }
#include
#include float disc;//判别式:disc = b^2-4ac float x1, x2;//两个根 float p, q;//两个虚根 void greater_than_zero(int a, int b) { //x1 = -b + sqrt(disc) / 2a //x2 = -b - sqrt(disc) / 2a x1 = -b + sqrt(disc) / (2 * a); x2 = -b - sqrt(disc) / (2 * a); } void equal_to_zero(int a, int b) { //x1 = x2 = -b / 2a x1 = x2 = -b / (2 * a); } void less_than_zero(int a, int b) { //p = -b / 2a //q = sqrt(-disc) / 2a p = -b / 2 * a; q = sqrt(-disc) / (2 * a); } int main() { int a, b, c; printf("请输入a,b,c:"); scanf("%d%d%d", &a, &b, &c); disc = b * b - 4 * a * c;//计算判别式 if (disc > 0) { greater_than_zero(a, b); printf("disc>0的两个根为:x1=%f,x2=%f\n", x1, x2); } else if (disc == 0) { equal_to_zero(a, b); printf("disc=0的两个根为:x1=%f,x2=%f\n", x1, x2); } else//disc < 0 { less_than_zero(a, b); printf("disc<0的两个根为:x1=%f,x2=%f\n", p+q, p-q); } return 0; }
#include
#include #include bool IsPrime(value) { for (int i = 2; i <= sqrt(value); ++i) { if (value % i == 0) return false; } return true; } int main() { bool flag; int value; printf("请输入 value: "); scanf("%d", &value); flag = IsPrime(value); if (flag) printf("%d 是素数\n", value); else printf("%d 不是素数\n", value); return 0; }
#include
void PrintArray(int arr[3][3]) { for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { printf("%d ", arr[i][j]); } printf("\n"); } } void ReverseArray(int arr[3][3]) { for (int i = 0; i < 3; ++i) { for (int j = 0; j < i; ++j) { int tmp = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = tmp; } } } int main() { int arr[3][3] = { 1,2,3,4,5,6,7,8,9 }; PrintArray(arr); printf("\n"); ReverseArray(arr); PrintArray(arr); return 0; }
#include
#include void ReverseString(char str[]) { int start = 0; int end = strlen(str) - 1; while (start < end) { char tmp = str[start]; str[start] = str[end]; str[end] = tmp; start++; end--; } } int main() { char str[100] = { 0 }; printf("请输入字符串:>"); gets(str); printf("原始字符串为:%s\n", str); ReverseString(str); printf("反序字符串为:%s\n", str); return 0; }
#include
void ConcatString(char dest[], char str1[], char str2[]) { int i = 0; while (str1[i] != '\0') { dest[i] = str1[i]; i++; } int j = 0; while (str2[j] != '\0') { dest[i++] = str2[j++]; } dest[i] = '\0'; } int main() { char str1[100] = { 0 }; char str2[100] = { 0 }; char str[200] = { 0 }; printf("请输入第一个字符串:>"); gets(str1); printf("请输入第二个字符串:>"); gets(str2); ConcatString(str, str1, str2); printf("新字符串为:%s\n", str); return 0; }
#include
void Copy(char dest[], char str[]) { int i = 0; int j = 0; while (str[i] != '\0') { if (str[i] == 'a' || str[i] == 'A' || str[i] == 'o' || str[i] == 'O' || str[i] == 'e' || str[i] == 'E' || str[i] == 'i' || str[i] == 'I' || str[i] == 'u' || str[i] == 'U') { dest[j++] = str[i]; } i++; } dest[j] = '\0'; } int main() { char str[100] = { 0 }; char dest[100] = { 0 }; printf("请输入一个字符串:>"); gets(str); printf("原始字符串为:%s\n", str); Copy(dest, str); printf("元音字符串为:%s\n", dest); return 0; }
#include
void OutString(char digits[]) { int i = 0; while (digits[i] != '\0') { printf("%c", digits[i]); if (digits[i + 1] == '\0') break; printf(" "); i++; } } int main() { char digits[5] = { 0 }; printf("请输入一个4位数字符串:>"); scanf("%s", digits); OutString(digits); printf("\n"); return 0; }
#include
int letter, digit, space, others; void CountString(char str[]) { int i = 0; while (str[i] != '\0') { //统计字母 if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z') letter++; else if (str[i] >= '0' && str[i] <= '9')//统计数字 digit++; else if (str[i] == ' ')//统计空格 space++; else//统计其它字符 others++; i++; } } int main() { char str[256] = { 0 }; printf("请输入一个字符串:>"); gets(str); CountString(str); printf("字母个数:%d\n数字个数:%d\n空格个数:%d\n其它字符个数:%d\n", letter, digit, space, others); return 0; }
#include
#include void FindLongWord(char str[], char word[]) { int i = 0, j = 0, len = 0; while (str[i] != '\0') { j = i; while (str[j] != ' ' && str[j] != '\0') { j++; } len = j - i; if (len > strlen(word)) { strncpy(word, str + i, len); } j++; i = j; } } int main() { char str[256] = "A gentleman who speaks and recites is as gentle as jade"; char word[256] = { 0 }; FindLongWord(str, word); printf("最长单词为:%s\n", word); return 0; }
#include
#include void BubbleSort(char str[]) { int n = strlen(str); for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (str[j] > str[j + 1]) { char tmp = str[j]; str[j] = str[j + 1]; str[j + 1] = tmp; } } } } int main() { char str[11] = { 0 }; printf("请输入10个字符:"); for (int i = 0; i < 10; i++) { scanf("%c", &str[i]); } printf("原始字符序列为:%s\n", str); BubbleSort(str); printf("排序后字符序列为:%s\n", str); return 0; }
#include
#include double Root(int a, int b, int c, int d, double x) { double x0; double f, f1; do { x0 = x; f = a * pow(x0, 3) + b * pow(x0, 2) + c * x0 + d; f1 = 3 * a * pow(x0, 2) + 2 * b * x0 + c; x = x0 - f / f1; } while (fabs(x - x0) >= 1e-3); return x; } int main() { int a, b, c, d; double x; printf("请输入系数a b c d x:>"); scanf("%d%d%d%d%lf", &a, &b, &c, &d, &x); double res = Root(a, b, c, d, x); printf("root = %lf\n", res); return 0; }
#include
float Ploy(int n, int x) { if (n == 0) return 1; else if (n == 1) return x; return ((2 * n - 1) * x - Ploy(n - 1, x) - (n - 1) * Ploy(n - 2, x)) / n; } int main() { int n, x; printf("请输入n和x的值:>"); scanf("%d%d", &n, &x); float ret = Ploy(n, x); printf("%d阶勒让德多项式的值为:%f", n, ret); return 0; }
#include
#define M 10 //学生人数 #define N 5 //课程数 float avg_stu[M];//每个学生的平均分 float avg_course[N];//每门课程的平均分 float hight;//最高分 int stu_index, course_index;//最高分的学生和课程成绩下标 float variance;//方差 //计算每个学生的平均分 void avg_score_stu(float score[M][N]) { float sum = 0.0; for (int i = 0; i < M; i++) { sum = 0.0; for (int j = 0; j < N; j++) { sum += score[i][j]; } avg_stu[i] = sum / N; } } //计算每门课程的平均分 void avg_score_course(float score[M][N]) { float sum = 0.0; for (int i = 0; i < N; i++) { sum = 0.0; for (int j = 0; j < M; j++) { sum += score[j][i]; } avg_course[i] = sum / N; } } //所有分数中最高的分数所对应的学生和课程 float hight_course(float score[M][N]) { float hight = 0.0; for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { if (score[i][j] > hight) { hight = score[i][j]; stu_index = i; course_index = j; } } } return hight; } //计算平均分方差 float variance_avg_score() { float sum_avg_square = 0.0;//平均分平方和 float sum_avg_score = 0.0;//平均分和 for (int i = 0; i < M; i++) { sum_avg_square += avg_stu[i] * avg_stu[i]; sum_avg_score += avg_stu[i]; } return (sum_avg_square / M - (sum_avg_score / M) * (sum_avg_score / M)); } //打印 void print_result(float score[M][N]) { printf("N0 course1 course2 course3 course4 course5 average\n"); for (int i = 0; i < M; i++) { printf("N%-5d", i + 1); for (int j = 0; j < N; j++) { printf("%-10.1f", score[i][j]); } //显示每个学生的平均分 printf("%-10.1f\n", avg_stu[i]); } printf("%-5s ", "avg"); for (int i = 0; i < N; i++) { //显示每一门课的平均分 printf("%-10.1f", avg_course[i]); } printf("\n"); printf("最高分为:%.1f分,是第%d位同学的第%d门课程。\n", hight, stu_index+1, course_index+1); printf("平均分的方差为:%.1f\n", variance); } int main() { float score[M][N] = { {1,2,3,4,5}, {2,3,4,5,6}, {3,4,5,6,7}, {4,5,6,7,8}, {5,6,7,8,9}, {6,7,8,3,1}, {7,8,8,1,2}, {8,8,1,2,3}, {7,1,2,3,4}, {4,3,2,1,8} }; //计算每个学生的平均分 avg_score_stu(score); //计算每门课程的平均分 avg_score_course(score); //所有分数中最高的分数所对应的学生和课程 hight = hight_course(score); //计算平均分方差 variance = variance_avg_score(score); //打印 print_result(score); return 0; }
#include
#include #define N 10 #define NAME_SIZE 10 //输入10个职工的姓名和职工号 void Input(int id[], char name[][NAME_SIZE]) { for (int i = 0; i < N; i++) { printf("输入职工号:"); scanf("%d", &id[i]); getchar(); //忽略输入的回车 printf("输入职工的姓名:"); gets(name[i]); } } //在屏幕上输出 void Output(int id[], char name[][NAME_SIZE]) { for (int i = 0; i < N; i++) { printf("[%d] : [%s]\n", id[i], name[i]); } } //排序 void Sort(int id[], char name[][NAME_SIZE]) { char tmp_name[NAME_SIZE]; for (int i = 0; i < N - 1; i++) { for (int j = 0; j < N - 1 - i; j++) { if (id[j] > id[j + 1]) { int tmp_id = id[j]; strcpy(tmp_name, name[j]); id[j] = id[j + 1]; strcpy(name[j], name[j + 1]); id[j + 1] = tmp_id; strcpy(name[j + 1], tmp_name); } } } } //折半法查找职工号 void Search(int id[], char name[][NAME_SIZE], int key) { int low = 0; int high = N - 1; int mid; while (low <= high) { mid = (low + high) / 2; if (key == id[mid]) break; if (key < id[mid]) high = mid - 1; else low = mid + 1; } if (low <= high) printf("职工号为%d的职工姓名为:%s\n", key, name[mid]); else printf("要查找的职工号为%d的职工不存在。\n", key); } int main() { int id[N];//职工号 char name[N][NAME_SIZE];//职工号对应的职工姓名 Input(id, name); Output(id, name); Sort(id, name); printf("\n"); Output(id, name); int no; while (1) { printf("请输入要查找的职工号:"); scanf("%d", &no); Search(id, name, no); } return 0; }
#include
unsigned int HextoDec(char hex[]) { unsigned int ret = 0; int i = 0; while (hex[i] != '\0') { if (hex[i] >= 'a' && hex[i] <= 'z') ret = 16 * ret + hex[i] - 'a' + 10; else if (hex[i] >= 'A' && hex[i] <= 'Z') ret = 16 * ret + hex[i] - 'A' + 10; else ret = 16 * ret + hex[i] - '0'; i++; } return ret; } int main() { char hex[9] = { 0 };// FF FF FF FF printf("请输入一个十六进制数:>"); scanf("%s", hex); unsigned int ret = HextoDec(hex); printf("0x%s ==> %u", hex, ret); return 0; }
#include
void Convert(int n) { if (n / 10 != 0) Convert(n / 10); printf("%c", n % 10 + '0'); } int main() { int num = 0; printf("请输入一个整数:>"); scanf("%d", &num); printf("整数%d转换成字符串为: ", num); Convert(num); printf("\n"); return 0; }
#include
#include bool IsLeap(int year) { return ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)); } int GetDayByYM(int year, int month) { //1 2 3 4 5 6 7 8 9 10 11 12 int days[13] = { 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if ((month == 2) && IsLeap(year)) return days[0]; return days[month]; } int GetDayByYMD(int year, int month, int day) { int days = 0; for (int i = 1; i < month; i++) { days += GetDayByYM(year, i); } return days + day; } int main() { int year, month, day; printf("请输入年 月 日:>"); scanf("%d%d%d", &year, &month, &day); int days = GetDayByYMD(year, month, day); printf("%d年%d月%d日是该年的第%d天\n", year, month, day, days); return 0; }