2024-1-4X习题八

6-1 实验5_1_设计函数fun

int fun(int x) {
	if (x < 1) return x;
	else if (x <= 10) return 2 * x - 1;
	else if (x <= 100) return 3 * x - 11;
	else return x * x - 24;
}

6-2 实验5_3_设计函数getDays

int getDays(int year, int month) {
	if (month == 2) {
		if (!(year % 4)) {
			if (!(year % 100) && (year % 400)) return 28;
			else return 29;
		}
		else return 28;
	}
	else {
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) return 31;
		else return 30;
	}
}

7-1 实验4_4_进制转换一

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

int main() {
	int n;
	scanf("%d", &n);
	int ans=0;
	int cnt = 0;
	while (n > 0) {
		if (n & 1) ans+=pow(2,cnt);
		cnt++;
		n /= 10;

	}
	printf("%d", ans);
}

7-2 实验4_8_斐波那契数列

//矩阵快速幂
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
const int d = 101;


int a[2][2] = { {0,1},{1,1} }, ans[2][1] = { 1,1 };
void kuaisumi() {
	int ans2[2][2];
	for(int i=0;i<2;i++)
		for (int j=0; j < 2; j++) {
			int ans1=0;
			for (int l = 0; l < 2; l++) {
				ans1 += (a[i][l] * a[l][j])%101;

			}
			ans2[i][j] = ans1%101;
		}
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 2; j++) {
			a[i][j] = ans2[i][j];
		}
}
void chengfa() {
	int sum[2][1];
	for (int i = 0; i < 2; i++) {
		int ans1 = 0;
		for (int j = 0; j < 2; j++) {
			ans1 += (a[i][j] * ans[j][0])%101;
		}
		sum[i][0] = ans1%101;
	}
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 2; j++) {
			ans[i][j] = sum[i][j];
		}
		
}
 
int main() {
	int n;
	scanf("%d", &n);
	n -= 2;
	while (n > 0) {
		if (n & 1) {
			chengfa();

		}
		kuaisumi();
		n >>=    1;

	}
	printf("%d", ans[1][0]);


}

7-3 实验4_12_遍历搜寻

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
const int d = 10001;

int ans1[10001] = { 0 };
int main() {
	int a, b, c;
	scanf("%d %d %d", &a, &b, &c);
	int cnt = 0;
	for (int i = a; i <= b; i++) {
		int ans = 0;
		for (int j = 1; j <= i / 2; j++) {
			if (!(i % j)) ans += j;
		}
		if (fabs(i - ans) <= c) ans1[cnt++] = i;

	}
	int cnt1 = 0,cnt2=1;
	if (!ans1[0]) printf("There is no proper number in the interval.");
	else{
		while (ans1[cnt1]) {
			int jud = 0;
			if (cnt2 == 5) jud = 1;
			if (!ans1[cnt1 + 1])jud = 1;
			printf("%d%c", ans1[cnt1], jud ? '\n' : '\t');
			cnt1++;
			cnt2++;
			if (cnt2 == 6) cnt2 = 1;

		}
	}
	

}

看到这篇文章的同学:

看到这篇文章的ybk:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值