代码实现 - 沈航817程序设计

大整数加减法(2014年)

#include <stdio.h>
#include <string.h>

#define LEN 20

void reverse(char *str) {
	int front = 0, tail = strlen(str) - 1;

	while (front < tail) {
		char c = str[front];
		str[front++] = str[tail];
		str[tail--] = c;
	}
}

//大整数 加法
void bigIntAdd(const char *strA, const char *strB, char *ret) {
	char lhs[LEN] = "", rhs[LEN] = "";

	strcpy(lhs, strA);
	strcpy(rhs, strB);

	reverse(lhs);
	reverse(rhs);

	int i = 0, carry[LEN + 1] = {0};
	int maxLen = strlen(strA) > strlen(strB) ? strlen(strA) : strlen(strB);

	for (i = 0; i < maxLen || carry[i] != 0; i++) {	
		int a = lhs[i] >= '0' && lhs[i] <= '9' ? lhs[i] - '0' : 0;
		int b = rhs[i] >= '0' && rhs[i] <= '9' ? rhs[i] - '0' : 0;
		int temp = a + b + carry[i];

		if (temp > 9) {
			temp = temp % 10;
			carry[i + 1] = 1;
		}

		ret[i] = temp + '0';
	}

	reverse(ret);
}

//大整数减法 http://blog.csdn.net/jeiwt/article/details/4974739

int main() {
	char lhs[20] = "", rhs[20] = "", ret[20] = "";

	scanf("%s %s", &lhs, &rhs);
	bigIntAdd(lhs, rhs, ret);
	printf("%s\n", ret);

	memset(ret, '\0', LEN);
	bigIntMinus(lhs, rhs, ret);
	printf("%s\n", ret);

	return 0;
}

解一元二次方程(2014年、2015年)

解题思路:公式法——解一元二次方程

1.化方程为一般式:

2.确定判别式,计算Δ(希腊字母,音译为戴尔塔)。

3.1 若Δ > 0,该方程在实数域内有两个不相等的实数根:

3.2 若Δ = 0,该方程在实数域内有两个相等的实数根:

3.3 若Δ < 0,该方程在实数域内无解

代码如下

#include <stdio.h>
#include <math.h>

int main()
{
	double a, b, c, x1, x2, delt;

	scanf("%lf %lf %lf", &a, &b, &c);

	delt = b*b - 4 * a*c;
	
	if (delt > 0) {
		x1 = (-b - sqrt(delt)) / 2 / a;
		x2 = (-b + sqrt(delt)) / 2 / a;

		printf("x1=%.2lf,x2=%.2lf", x1, x2);
	}
	else if (delt == 0) {
		x1 = -b / 2 / a;

		printf("x1 = x2 = %.2lf", x1);
	}
	else {
		printf("无实数根");
	}

	return 0;
}

输入一个正整数N,利用递归实现求N的阶乘。(2015年)

#include <stdio.h>

#define ELEMENT_TYPE int

ELEMENT_TYPE recursion_factorial(const ELEMENT_TYPE n){
	if(n >0)
		return n * recursion_factorial(n - 1);
	else {
		return 1;
	}
}

int main() {
	printf("%d\n", recursion_factorial(4));
	return 0;
}

两个集合相加(2015年)

#include <stdio.h>
#include <stdlib.h>

#define ElELMENT_TYPE int

struct List{
	struct ListNode *front;
	struct ListNode *tail;
};

struct ListNode {
	ElELMENT_TYPE val;
	struct ListNode *next;
};

struct List* init(){
	struct List *p = (struct List*)malloc(sizeof(struct List));
	p->front = (struct ListNode*)malloc(sizeof(struct ListNode));
	p->tail = p->front;
	p->tail->val = NULL;

	return p;
}

void push_back(struct List* p,const ElELMENT_TYPE val) {
	p->tail->val = val;
	p->tail->next = (struct ListNode*)malloc(sizeof(struct ListNode));
	p->tail = p->tail->next;
}

struct ListNode* find(const List* list, const ElELMENT_TYPE val) {
	struct ListNode *cur;
	for(cur = list->front; cur != list->tail; cur = cur->next) {
		if(val == cur->val){
			return cur;
		}
	}

	return NULL;
}

struct List* list_add(const List *lhs, const List *rhs){
	struct List *list = init();

	struct ListNode *cur;
	for(cur = lhs->front; cur != lhs->tail; cur = cur->next) {
		push_back(list, cur->val);
	}

	for(cur = rhs->front; cur != rhs->tail; cur = cur->next) {
		struct ListNode *p = find(lhs, cur->val);
		if(p == NULL) {
			push_back(list, cur->val);
		}
	}

	return list;
}

int main() {
	struct List *l1 = init();
	push_back(l1, 1);
	push_back(l1, 2);
	push_back(l1, 4);

	struct List *l2 = init();
	push_back(l2, 2);
	push_back(l2, 3);
	push_back(l2, 5);

	struct List *l3 = list_add(l1, l2);

	return 0;
}

冒泡排序(2015年)

#include <stdio.h>

#define ARR_SIZE 5

void PopSort(int arr[], const int size) {
	for(int i = 0; i < size; i++) {
		for( int j = i; j< size; j++) {
			if (arr[j] < arr[i]) {
				int temp = arr[i];
				arr [i]  = arr[j];
				arr[j] = temp;
			}
		}
	}
}

int main() {
	int arr[] = {5,4,3,4,5};
	PopSort(arr, ARR_SIZE);
	for(int i = 0; i < ARR_SIZE; i++) {
		printf("%d ", arr[i]);
	}
	return 0;
}

最大公约数

#include <stdio.h>

int greatestCommonDivisor(const int lhs, const int rhs) {
	int i = lhs < rhs ? lhs : rhs;

	for(; i >0; i--) {
		if(lhs % i == 0 && rhs % i ==0){
			break;
		}
	}

	return i;
}

int main() {
	printf("%d\n", greatestCommonDivisor(222, 407));
	return 0;
}

学生信息管理系统

#include <stdio.h>
#include <string.h>

#define STRLEN 100
#define STUDENT_NUM 2

struct Student {
	char strNo[STRLEN];
	char strName[STRLEN];
};

int main() {
	FILE *fp = fopen("data.dat", "r");

	char strNo[STRLEN] = "", strName[STRLEN] = "";
	Student students[STUDENT_NUM];

	int i = 0; 
	while (fscanf(fp, "%s %s", strNo, strName) != EOF) {
		strcpy(students[i].strNo, strNo);
		strcpy(students[i].strName, strName);

		i++;
	}

	Student temp;

	for (i = 0; i < STUDENT_NUM; i++) {
		for (int j = 0; j < i; j++) {
			if (strcmp(students[j].strNo, students[i].strNo) > 0) {

				strcpy(temp.strNo, students[j].strNo);
				strcpy(temp.strName, students[j].strName);

				strcpy(students[j].strNo, students[i].strNo);
				strcpy(students[j].strName, students[i].strName);

				strcpy(students[i].strNo, temp.strNo);
				strcpy(students[i].strName, temp.strName);
			}
		}
	}

	for (i = 0; i < STUDENT_NUM; i++) {
		printf("%s %s\n", students[i].strNo, students[i].strName);
	}

	return 0;
}

矩阵旋转 90 度

#include <stdio.h>

#define N 2

int main() {
	int a[N][N] = {
		{ 11, 12 },
		{ 21, 22 }
	};

	for (int r = 0; r < N; r++) {
		for (int c = 0; c < N; c++) {
			a[r][c] = a[c][r];
		}
	}

	for (int c = 0; c < N / 2 ; c++) {
		for (int r = 0; r < N; r++) {
			int temp = a[r][c];
			a[r][c] = a[r][N - c - 1];
			a[r][N - c - 1] = temp;
		}
	}

	for (int r = 0; r < N; r++) {
		for (int c = 0; c < N; c++) {
			printf("%d ", a[r][c]);
		}
		printf("\n");
	}

	return 0;
}

递归求最小公倍数

#include <stdio.h>

int minCommon(int lhs, int rhs, int common) {
	if (common % lhs == 0 && common % rhs == 0) {
		return common;
	}
	else {
		minCommon(lhs, rhs, common + 1);
	}
}

void main() {
	printf("%d\n", minCommon(3, 4, 1));
	printf("%d\n", minCommon(30, 45, 1));
}
posted @   健康平安快乐  阅读(1407)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示