精彩C语言知识源代码.doc
+经典C语言源代码1、(1)某年某月某日是星期几#include<stdio.h>int main()int year, month, day;while (scanf_s("%d%d%d", &year, &month, &day) != EOF)if (month = 1 | month = 2)/判断month是否为1或2year-;month += 12;int c = year / 100;int y = year - c * 100;int week = (c / 4) - 2 * c + (y + y / 4) + (13 * (month + 1) / 5) + day - 1;while (week<0) week += 7; week %= 7;switch (week)case 1:printf("Mondayn"); break;case 2:printf("Tuesdayn"); break;case 3:printf("Wednesdayn"); break;case 4:printf("Thursdayn"); break;case 5:printf("Fridayn"); break;case 6:printf("Saturdayn"); break;case 0:printf("Sundayn"); break;return 0;1、(2)某年某月某日是第几天(一维数组)#include "stdio.h"void main() int i, flag, year, month, day, dayth;int month_day = 0,31,28,31,30,31,30,31,31,30,31,30,31 ;printf("请输入年/月/日:n");scanf_s("%d/%d/%d", &year, &month, &day);dayth = day;flag = (year % 400 = 0) | (year % 4 = 0 && year % 100 != 0);if (flag)month_day2 = 29;for (i = 1; i < month; i+)dayth = dayth + month_dayi;printf("%d/%d/%d是第%d天n", year, month, day, dayth);2、30个数中找最小的数及其位置#include "stdio.h"# define SIZE 30void main() int i;float dataSIZE;int min;printf("请输入%d个浮点数:n",SIZE);for (i = 0; i < SIZE; i+) /scanf_s("%f", &datai);datai = rand() % 30 + 1;printf("%f、", datai);min = 0;for (i = 1; i < SIZE; i+) if (datai < datamin)min = i;printf("最小值是%5.2f,位置是%5dn", datamin, min);3、30个数从小到大排序(1)#include "stdio.h"# define SIZE 30void main() int i,j;float dataSIZE,temp;int min;printf("请输入%d个整型数:n",SIZE);for (i = 0; i < SIZE; i+) scanf_s("%f", &datai);for (i = 0; i < SIZE; i+) min = i;for (j = i + 1; j < SIZE; j+)if (dataj < datamin)min = j;temp = datamin;datamin = datai;datai = temp;printf("n排序后的结果是:n");for (i = 0; i < SIZE; i+)printf("%5.2f", datai);(2)模块化程序(数组名作为函数参数)#include "stdio.h"# define SIZE 5void accept_array(float a, int size);void sort(float a, int size);void show_array(float a, int size);void main() float scoreSIZE;accept_array(score, SIZE);printf("排序前:");show_array(score, SIZE);sort(score, SIZE);printf("排序后:");show_array(score, SIZE);void accept_array(float a, int size) int i;printf("请输入%d个分数:", size);for (i = 0; i < size; i+)scanf_s("%f", &ai);void show_array(float a, int size) int i;for (i = 0; i < size; i+)printf(" %5.2f", ai);printf("n");void sort(float a,int size) int i, min, j;float temp;for (i = 0; i < SIZE; i+) min = i;for (j = i + 1; j < SIZE; j+)if (aj < amin)min = j;temp = amin;amin = ai;ai = temp;4、(1)指针加减:#include "stdio.h"#define SIZE 10void main() int aSIZE = 1,2,3,4,5,6,7,8,9,10 ;int *pa, i;pa = &a0; /pa=a;printf("n");for (i = 0; i < SIZE; i+) printf(" %d", *pa);/printf(" %d", *(pa+1);pa+;(2)指针比较:#include "stdio.h"#define SIZE 10void main() int aSIZE = 1,2,3,4,5,6,7,8,9,10 ;int *pa, i;int *qa;pa = qa = &a0;printf("请输入%d整型数:",SIZE);for (; pa < qa + SIZE; pa+)scanf_s("%d", pa);for (pa-; qa <= pa; pa-)printf(" %d", *pa);5、两字符串相连:#include "stdio.h"#include "string.h"void str_cat(char str1, char str2);void main() int i, j;char str1160;char str280;printf("请输入第一个字符串:");gets(str1);printf("请输入第二个字符串:");gets(str2);str_cat(str1, str2);puts(str1);void str_cat(char str1, char str2) int i, j;i = 0;while (str1i != 0)i+;j = 0;while (str2j != 0) str1i = str2j;i+; j+;str1i = 0;6、二维数组(a,b转置)#include "stdio.h"void main() int i, j, b23;int a32 = 1,2,3,4,5,6 ;for (i = 0; i < 2; i+) for (j = 0; j < 3; j+)bij = aji;printf("na:n");for (i = 0; i < 3; i+) for (j = 0; j < 2; j+)printf("%5d", aij);printf("n");printf("nb:n");for(i = 0; i < 2; i+) for (j = 0; j < 3; j+)printf("%5d", bij);printf("n");7、输入一个二维数组并输出(指针)#include "stdio.h"void main() int x23;int i, j;for (i = 0; i < 2; i+)for (j = 0; j < 3; j+)scanf_s("%d", *(x + i) + j);putchar(n);for (i = 0; i < 2; i+)for (j = 0; j < 3; j+)printf("%d ", *(*(x + i) + j);putchar(n);8、冒泡法排序一个数组#include "stdio.h"#define size 10void maopao(int a);void main() int a10;int i;printf("请输入10个整数:n");for (i = 0; i < 10; i+)scanf_s("%d", &ai);maopao(a);void maopao(int a) int i, j, temp;for (i = 0; i < 9; i+) /进行9轮排序for (j = 0; j < 9 - i; j+)/每轮进行9-i次交换if (aj > aj + 1)temp = aj;aj = aj + 1;/大的沉底,小的上浮aj + 1 = temp;printf("排序结果:n");for (i = 0; i < 10; i+)printf("%4d", ai);9、 两数组A,B,要求A<B,如A:4,7,9B:1,3,5,8,9变换后A:1,3,5B:4,7,8,9,9#include <stdio.h>void ReArranger(int* A, int* B, int m, int n) /A和B是各有m个和n个整数的非降序数组,本算法将B数组元素逐个插入到A中,使A中各元素均不大于B中各元素,且两数组仍保持非降序排列。int x, j, i;while (Am - 1>B0)x = Am - 1;Am - 1 = B0; /交换Am-1和B0j = 1;while (j<n && Bj<x)Bj - 1 = Bj+; /寻找Am-1的插入位置Bj - 1 = x;x = Am - 1;i = m - 2;while (i >= 0 && Ai>x)Ai + 1 = Ai-; /寻找B0的插入位置Ai + 1 = x;void main() /这里主要介绍算法思想,主函数就简单写了int A3, B5, i;printf("输入第一个数组:");for (i = 0; i<3; i+) scanf_s("%d", &Ai);printf("n输入第二个数组:");for (i = 0; i<5; i+) scanf_s("%d", &Bi);ReArranger(A, B, 3, 5);printf("n输出第一个数组:");for (i = 0; i<3; i+)printf("%d ", Ai);printf("nn");printf("输出第二个数组:");for (i = 0; i<5; i+)printf("%d ", Bi);printf("n");10、符合1+6+3=3+2+5=1+4+5有哪几组 A 1 B C 6 4D E F到 3 2 5#include <stdio.h>void main() int a, b, c, d, e, f;for (a = 1; a <= 6; a+)for (b = 1; b <= 6; b+) if (b = a)continue;for (c = 1; c <= 6; c+) if (c = a) | (c = b)continue;for (d = 1; d <= 6; d+) if (d = a) | (d = b) | (d = c)continue;for (e = 1; e <= 6; e+) if (e = a) | (e = b) | (e = c) | (e = d)continue;f = 21 - (a + b + c + d + e);if (a + b + d = d + e + f) && (a + b + d = a + c + f) printf(" %dn", a);printf("%d %dn", b, c);printf("%d %d %dn", d, e, f);11、输入一串字符,升序排序,折半查找其中一字符#include <stdio.h>void *sortString(char unsort, int length) for (int i = 0; i < length; i+) for (int j = i + 1; j < length; j+) if (unsorti > unsortj) int temp = unsortj;unsortj = unsorti;unsorti = temp;void main() char s150;gets(s1);char value;scanf_s("%c", &value);printf("s1:%sn", s1);printf("value:%cn", value);int length = strlen(s1);printf("length:%dn", length);sortString(s1, length);printf("s1:%sn", s1);int start = 0, end = length - 1;int mid = (end + start) / 2;while (start <= end&&s1mid != value) mid = (end + start) / 2;if (value > s1mid)start = mid+1;elseend = mid-1;if (mid)printf("该字符在已知字符串中,即在第%d个n",mid);elseprintf("该字符不在已知字符串中n");12、100-300和500-700直接素数,m不被2到根号m直接任一整除#include<stdio.h>int isprime(int n)if (n<2) return 0;for (int i = 2; i<n / 2; i+)if (n%i = 0) return 0;return 1;void main()int i, k = 0;for (i = 100; i <= 300; i+)if (isprime(i)printf("%3d ", i);k+;if (k % 10 = 0) printf("n");for (i = 500; i <= 700; i+)if (isprime(i)printf("%3d ", i);k+;if (k % 10 = 0) printf("n");printf("n");13、判断一个数是否是素数#include<stdio.h>#include<math.h>void main()int m, i, k;printf("请输入一个整数:");scanf_s("%d", &m);k = (int)sqrt(m);for (i = 2; i <= k; i+)if (m%i = 0)break;if (i>k)printf("%d 是素数。n", m);elseprintf("%d 不是素数。n", m);14、一个数是否含有数字5#include <stdio.h>#include <stdbool.h>bool is5Num(int num) int temp = num % 10;while (temp != 5 && num > 10) num = num / 10;temp = num % 10;if (temp != 5)return false;elsereturn true;void main() int num;printf("输入一个数:n");scanf_s("%d", &num);if (is5Num(num)printf("含5n");elseprintf("不含5n");15、一个排好序的数组,插入一个数#include <stdio.h>void main() int a11 = 1,2,3,4,5,6,7,8,9,10 ;int num;printf("插入前数组为:n");for (int i = 0; i<10; i+)printf("%3d", ai);printf("n");printf("请输入一个数:n");scanf_s("%d", &num);if (num > a9)a10 = num;else for (int i = 0; i < 10;i+)if (num < ai) for (int j = 9; j >= i; j-)aj + 1 = aj;ai = num;break;printf("插入后数组为:n");for (int i = 0; i <= 10; i+)printf("%3d", ai);printf("n");16、牛顿迭代法:#include <stdio.h>#include <math.h>double func(double x) /函数return x*x*x + 2.0*x*x + 3.0*x + 4.0;double func1(double x) /导函数return 3 * x*x + 4 * x + 3;void Newton(double x0,double precision)/迭代次数double x1;int k;if (func1(x0) = 0.0) /若通过初值,函数返回为0printf("迭代过程中倒数为0!n");return;x1 = x0 - func(x0) / func1(x0);/进行牛顿迭代计算while (!(func1(x1 - x0) < precision | fabs(func(x1) < precision) x0 = x1;/准备下一次迭代if (func1(x0) = 0.0)/若通过初值,函数返回值为0printf("迭代过程中倒数为0!n");x1 = x0 - func(x0) / func1(x0);/进行牛顿迭代计算void main() double x, precision;printf("输入初始迭代值x0:n");scanf_s("%lf", &x);printf("迭代要求的精度:n");scanf_s("%lf", &precision);Newton(x, precision);/若函数返回值为1printf("该值附近的跟为:%lfn", x);getchar();getchar();17、起始时间到终止时间天数#include "stdio.h"void main() int start3, end3;printf("请输入开始日期,如1964.2.19:n");scanf_s("%d.%d.%d", &start0, &start1, &start2);printf("请输入结束日期,如2001.10.20:n");scanf_s("%d.%d.%d", &end0, &end1, &end2);int sum = 0;for (int mid = start0; mid < end0; mid+) if (mid % 400 = 0) | (mid % 4 = 0 && mid % 100 != 0) sum = sum + 366;elsesum = sum + 365;sum = sum - indexday(start0,start1,start2) + indexday(end0,end1,end2);printf("在%d.%d.%d-%d.%d.%d之间有%d天n", start0,start1,start2,end0,end1,end2, sum);int indexday(int year, int month, int day) int i, flag, dayth;int month_day = 0,31,28,31,30,31,30,31,31,30,31,30,31 ;dayth = day;flag = (year % 400 = 0) | (year % 4 = 0 && year % 100 != 0);if (flag)month_day2 = 29;for (i = 1; i < month; i+)dayth = dayth + month_dayi;return dayth;18、递归求1*1+2*2+3*3+n*n#include "stdio.h"long Element(int n) if (n = 1)return 1 * 1;elsereturn Element(n - 1) + n*n;void main() int n;printf("请输入n的值:n");scanf_s("%d", &n);printf("所求值为%dn", Element(n);19、最大公约数(辗转相除)#include<stdio.h> void main() /* 辗转相除法求最大公约数 */int m, n, a, b, t, c;printf("Input two integer numbers:n");scanf_s("%d%d", &a, &b);m = a; n = b;while (b != 0) /* 余数不为0,继续相除,直到余数为0 */c = a%b; a = b; b = c;printf("The largest common divisor:%dn", a);printf("The least common multiple:%dn", m*n / a);20、杨辉三角#include<stdio.h> void main()int i, j, n, k;printf("Enter n:"); scanf_s("%d", &n);for (i = 1; i <= n; i+)k = 1;for (j = 1; j<i; j+)printf("%3d", k);k = k*(i - j) / j;/每次要打印的下一个数等于前一个数乘以其所在行数和列数的差再除以其列数printf("%3d", k);printf("n");21、约瑟夫#include <stdio.h>void main()int n, m, i, s=0; printf ("Enter n: m: "); scanf("%d%d", &n, &m); for (i=2; i<=n; i+) s=(s+m)%i; printf ("The winner is %dn", s+1); 22、斐波拉契#include<stdio.h>void main()long f, f1, f2; int i, n;printf("Enter n : "); scanf_s("%d", &n);f1 = 1; f2 = 1;printf("%10d%10d", f1, f2);for (i = 1; i <= n; i+)f = f1 + f2;printf("%10d", f);f1 = f2; f2 = f;if (i % 10 = 0)printf("n"); 23、海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?#include<stdio.h>main()int i, m, j, k, count;for (i = 4; i<10000; i += 4)count = 0;m = i;for (k = 0; k<5; k+)j = i / 4 * 5 + 1;i = j;if (j % 4 = 0)count+;else break;i = m;if (count = 4) printf("%dn", j);break;24、验证哥德巴赫猜想,即任一个偶数都可以分解为两个素数之和。#include <stdio.h>int isprime(int);void even(int);void main()int a;printf("请输入一个偶数:"); scanf_s("%d", &a);if (a % 2 = 0) even(a);else printf("%d 不是偶数!n");void even(int x)int i;for (i = 2; i <= x / 2; i+)if (isprime(i) && isprime(x - i)printf("%d=%d+%dn", x, i, x - i); return;int isprime(int a)int i;for (i = 2; i <= a / 2; i+)if (a%i = 0)return 0; return 1;25、魔方阵#include <stdio.h>#define N 20void main()int aNN = 0 , i, j, k, n;doprintf("请输入魔幻方的阶数n(n<%d):", N);scanf_s("%d", &n); while (n <= 0 | n >= N | n % 2 = 0);i = n + 1; j = n / 2 + 1;a1j = 1; /将1放在第一行中间一列for (k = 2; k <= n*n; k+)/*从2开始直到n*n各数依次按一下规则存放:每一个数存放的行比前一个数的行数减1,列数加1*/i-; j+;if (i<1 && j>n)i += 2; j-;else if (i < 1) i = n; /如果上一个数的行数为1,则下一个数的行数为nif (j > n) j = 1;/当上一个数的列数为n时,下一个数的列数应为1,行数减去1if (aij = 0)aij = k;/*如果按上面规则确定的位置上已有数,或上一个数是第一行第n列时,则把下一个数放在上一个数的下面。*/else i += 2; j-; aij = k; for (i = 1; i <= n; i+)for (j = 1; j <= n; j+)printf("%-4d", aij);printf("n");26、国际象棋棋盘#include <stdio.h>#include <windows.h>void main() int i, j;SetConsoleOutputCP(437); /显示大于127的asc码for (i = 0; i < 8; i+) for (j = 0; j < 8; j+) if (i + j) % 2 = 0)printf("%c%c", 219, 219);elseprintf(" ");printf("n");