计算智能的课后习题代码

计算智能课(大一下学期)课后习题的所有代码,有兴趣可以康康

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0)
#include<algorithm>
#include<queue>
typedef long long ll;case
ll F(ll t, ll b) {
	ll aa = t, bb = b, t;
	while (t % b) {
		t = t % b;
		t = b;
		b = t;
	}
	return aa * bb / b;
}

int main()
{
	IOS;
	ll t, b;
	int t;
	cin >> t;
	while (t--) {
		cin >> t >> b;
		cout << F(t, b) << endl;
	}
	cout << "group 1 done" << endl;
	while (cin >> t >> b && t && b)cout << F(t, b) << endl;
	cout << "group 2 done" << endl;
	while (cin >> t >> b) cout << F(t, b) << endl;
	cout << "group 3 done" << endl;
}

走迷宫2
typedef struct node {
	int x = 0, y = 0;
	int num = 0;
}T;

int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
void f()
{
	char map[200][200] = { 0 };
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++)cin >> map[i];
	int w;
	int c1[200] = { 0 }, r1[200] = { 0 }, c2[200] = { 0 }, r2[200] = { 0 };
	int sc, sr, ec, er;
	cin >> w;
	for (int i = 0; i < w; i++)cin >> c1[i] >> r1[i] >> c2[i] >> r2[i];
	cin >> sc >> sr >> ec >> er;
	queue<T>q;
	T cur, next;
	cur.x = sc, cur.y = sr;
	q.push(cur);
	map[sc][sr] = '1';
	while (!q.empty()) {
		cur = q.front();
		q.pop();
		if (cur.x == ec && cur.y == er) {
			cout << cur.num << endl;
			return;
		}
		int flag = 1;
		for (int i = 0; i < w; i++) {
			if (cur.x == c1[i] && cur.y == r1[i]) {
				flag = 0;
				next.x = c2[i];
				next.y = r2[i];
				next.num = cur.num + 1;
				map[c2[i]][r2[i]] = '1';
				q.push(next);
				break;
			}
		}
		if (flag) {
			int nx, ny;
			for (int i = 0; i < 4; i++) {
				nx = cur.x + dir[i][0];
				ny = cur.y + dir[i][1];
				if (nx < 0 || nx >= n || ny < 0 || ny >= m)continue;
				if (map[nx][ny] == '0') {
					next.x = nx;
					next.y = ny;
					next.num = cur.num + 1;
					map[nx][ny] = '1';
					q.push(next);
				}
			}
		}
	}
	cout << "die" << endl;
}
int main()
{
	IOS;
	int t;
	cin >> t;
	while (t--) {
		f();
	}
}

走迷宫
typedef struct node {
	int x = 0, y = 0;
	int num = 0;
}T;

int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
void f()
{
	char map[200][200] = { 0 };
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++)cin >> map[i];
	int c1[200] = { 0 }, r1[200] = { 0 }, c2[200] = { 0 }, r2[200] = { 0 };
	int sc, sr, ec, er;
	cin >> sc >> sr >> ec >> er;
	queue<T>q;
	T cur, next;
	cur.x = sc, cur.y = sr;
	q.push(cur);
	map[sc][sr] = '1';
	while (!q.empty()) {
		cur = q.front();
		q.pop();
		if (cur.x == ec && cur.y == er) {
			cout << cur.num << endl;
			return;
		}
		int nx, ny;
		for (int i = 0; i < 4; i++) {
			nx = cur.x + dir[i][0];
			ny = cur.y + dir[i][1];
			if (nx < 0)nx += n;
			if (nx >= n)nx -= n;
			if (ny < 0)ny += m;
			if (ny >= m)ny -= m;
			if (map[nx][ny] == '0') {
				next.x = nx;
				next.y = ny;
				next.num = cur.num + 1;
				map[nx][ny] = '1';
				q.push(next);
			}
		}
	}
	cout << "die" << endl;
}
int main()
{
	IOS;
	int t;
	cin >> t;
	while (t--) {
		f();
	}
}


丑数(打表)
int d[100000005];
void dir()
{
	int num2 = 0, num3 = 0, num5 = 0;
	int t2, t3, t5;
	d[0] = 1;
	for (int i = 1; i <= 100000000; i++) {
		t2 = d[num2] * 2;
		t3 = d[num3] * 3;
		t5 = d[num5] * 5;
		d[i] = min(t2, min(t3, t5));
		if (t2 == d[i])num2++;
		if (t3 == d[i])num3++;
		if (t5 == d[i])num5++;
	}
}

int main()
{
	IOS;
	dir();
	int t, n;
	cin >> t;
	while (t--) {
		cin >> n;
		int num = 0, i = 0;
		while (num < n) {
			num += d[i + 1] - d[i] - 1;
			i++;
		}
		i--;
		num -= d[i + 1] - d[i] - 1;//得到d[i-1]之前非丑数个数
		cout << d[i] + (n - num) << endl;
	}
}

小明手上的牌
int main()
{
	IOS;
	int n, m, last = 0;
	cin >> n >> m;
	for (int i = 0; i < n; i++)cin >> last;
	if (last <= n - m + 1)cout << last << endl;
	else cout << n - m + 1 << endl;
}

银行叫号顺序
#include<queue>
int t = 99999;
int main()
{
	IOS;
	priority_queue <pair< int, string >> pq;
	int realtime = 0, time, tank;
	string name;
	int n,i,j,k;
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> time >> tank >> name;
		while(1)
		{
			if (time <= realtime) {
				pq.push(make_pair(tank * 100000 + t--, name));
				break;
			}
			if (!pq.empty()) {
				cout << pq.top().second << endl;
				pq.pop();
			}
			realtime += 5;
		}
	}
	while (!pq.empty()) {
		cout << pq.top().second << endl;
		pq.pop();
	}
}

勇者斗恶龙
int head[200005], fight[200005];
int main()
{
	IOS;
	int n, m, i, j, k;
	while (cin >> n >> m && n && m)
	{
		memset(head, 0, sizeof(head));
		memset(fight, 0, sizeof(fight));
		for (i = 0; i < n; i++)cin >> head[i];
		for (j = 0; j < m; j++)cin >> fight[j];
		if (n > m) {
			cout << "Loowater is doomed!" << endl;
			continue;
		}
		sort(head, head + n);
		sort(fight, fight + m);
		ll sum = 0;
		i = 0;
		for (k = 0; k < m; k++) {
			if (fight[k] >= head[i]) {
				sum += fight[k];
				i++;
			}
			if (i >= n)break;
		}
		if (i >= n)cout << sum << endl;
		else cout << "Loowater is doomed!" << endl;
	}
}

校赛排名
struct node{
	int num = 0, time=0;
	string name="";
	int order = 0;
}t[500005];

bool cmp(node t,node b)
{
	if (t.num == b.num) {
		if (t.time == b.time)return t.order < b.order;
		return t.time < b.time;
	}
	return t.num > b.num;
}
int main()
{
	IOS;
	int n,i,j,k;
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> t[i].num >> t[i].time >> t[i].name;
		t[i].order = i;
	}
	sort(t, t + n, cmp);
	for (j = 0; j < n; j++)cout << t[j].name << endl;
}

校赛排名2
struct node {
	int pass = 0;
	int time = 0;
	string name;
	int error[20] = { 0 };
	int right[20] = { 0 };
}t[10005];

bool cmp(node t, node b) {
	if (t.pass != b.pass)return t.pass > b.pass;
	return t.time < b.time;
}

int main()
{
	IOS;
	int time;
	string name;
	char question;
	int result;
	int num = 0;//队伍数量
	int flag;
	int i, j, k;
	while (cin >> time >> name >> question >> result) {
		flag = 0;//0为该队伍前面没出现过
		for (i = 0; i < num; i++) {
			if (name == t[i].name) {
				flag = 1;
				break;
			}
		}
		if (flag == 0) {//没出现过
			t[num++].name = name;
		}
		if (result == 0 && t[i].right[question - 'A'] != 1) {//通过并且之前没有正确提交过
			t[i].pass++;
			t[i].right[question - 'A'] = 1;
			t[i].time += t[i].error[question - 'A'] * 20 + time;
		}
		else if (result)t[i].error[question - 'A']++;
	}
	sort(t, t + num, cmp);
	for (j = 0; j < num; j++) {
		if (t[j].pass)cout << t[j].name << ' ' << t[j].pass <<' '<< t[j].time << endl;
	}
}

巡逻的士兵
ll f(int n)
{
	if (n < 3)return 0;
	else if (n == 3)return 1;
	if (n % 2 == 0)return 2 * f(n / 2);
	else return f(n / 2) + f((n + 1) / 2);
}
int main()
{
	IOS;
	ll n;
	while (cin >> n && n)
	{
		cout << f(n) << endl;
	}
}

偷懒的士兵1
ll f(int n)
{
	if (n < 3)return 0;
	else if (n == 3)return 1;
	if (n % 2 == 0)return 2 * f(n / 2);
	else return f(n / 2) + f((n + 1) / 2);
}
int main()
{
	IOS;
	ll n;
	while (cin >> n && n)
	{
		cout << f(n) << endl;
	}
}

偷懒的士兵2
int f(int n,int cur,int pos)
{
	if (n < 3)return cur;
	else if (n == 3)return 99999;
	return min(f(n / 2, cur + pos, pos * 2), f((n + 1) / 2, cur, pos * 2));
	
}
int main()
{
	IOS;
	int n;
	while (cin >> n && n)
	{
		int t = f(n, 1, 1);
		if (t == 99999)cout << 0 << endl;
		else cout << t << endl;
	}
}

除法等式
int b, n;
void f()
{
	int a = b * n;
	int xx = a, yy = b;
	int t[12] = { 0 }, i = 0;
	while (xx) {
		t[i++] = xx % 10;
		xx /= 10;
	}
	while (yy) {
		t[i++] = yy % 10;
		yy /= 10;
	}
	sort(t, t + i);
	int flag = 1;
	for (int j = 0; j < i - 1; j++) {
		if (t[j] && t[j] == t[j + 1])
		{
			flag = 0;
			break;
		}
	}
	if (flag)printf("%05d/%05d=%d\n", a, b, n);
}
int main()
{
	IOS;
	while (cin >> n && n) {

		for (b = 1; b <= 50000; b++) {
			if (b * n < 100000)f();
			else break;
		}
	}
}

三角形
int main()
{
	IOS;
	int t,a;
	cin >> t;
	while (t--) {
		cin >> a;
		for (int j = (a * a + 1) / 2; j > sqrt(a * a / 2); j--) {
			if (j != a && (int)sqrt(abs(j * j - a * a)) * (int)sqrt(abs(j * j - a * a)) == abs(j * j - a * a)) {//首先a必定不与n相等(相等的话a不是较大边,b也不是整数),然后判断b是否是整数
				cout << j << ',' << (int)sqrt(abs(j * j - a* a)) << endl;
			}
		}
		cout << endl;
	}
}

龙龙
long long H(int n) {
	long long res = 0;
	int i = 1, l = n / (i + 1), r = n / i;
	while (l < r) {
		res += (r - l) * (n / r);
		r = n / ++i;
		l = n / (i + 1);
	}
	for (int j = l; j >= 1; j--)res += n / j;
	return res;
}

int main()
{
	IOS;
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		cout<<H(n)<<endl;
	}
}

分数拆分
1/k=1/x+1/y+1/z
int main()
{
	//IOS;
	ll k;
	while (cin >> k && k) {
		ll z;
		for (z = k + 1; z <= 3 * k; z++) {
			ll y = k * z / (z - k);
			if (k * z % (z - k) == 0 && y >= z) {//y为整数并且大于等于z
				printf("1/%lld=1/%lld+1/%lld\n", k, y, z);
			}
			ll t = y;
			for (y = t + 1; y <= 2 * t + 1; y++) {
				ll x = k * y * z / (y * z - k * z - k * y);
				if (k * y * z % (y * z - k * z - k * y) == 0 && x >= y && y >= z) {//x为整数并且大于等于y大于等于z
					printf("1/%lld=1/%lld+1/%lld+1/%lld\n", k, x, y, z);
				}
			}
		}
		cout << endl;
	}
}

n皇后问题
int a[15];
int n;
bool rule(int k) {
	int i = 1;
	for (; i < k; i++) {
		if (a[i] == a[k] || abs(a[i] - a[k]) == abs(i - k))return false;
	}
	return true;
}
int queen(int n) {
	int cnt = 0;
	int k = 1;
	a[1] = 0;
	while (k > 0) {
		a[k]++;
		while (a[k] <= n && !rule(k))a[k]++;
		if (a[k] <= n) {
			if (k == n) {
				cnt++;
			}
			else {
				a[++k]=0;
			}
		}
		else {
			k--;
		}
	}
	return cnt;
}
int main()
{
	IOS;
	int t;
	cin >> t;
	while (t--) {
		cin >> n;
		cout << queen(n) << endl;
	}
}

//最小特殊数
ll n, k;
ll a[20], c[20],cnt;
int book[20], flag;
void dfs(int cur) {
	if (n == cur) {
		ll sum = 0;
		for (int i = 0; i < n; i++)sum = sum * 10+c[i];
		if (sum % k == 0) {
			flag = 1;
			cnt = sum;
			return;
		}
	}
	for (int i = 0; i < n&&!flag; i++) {
		if (cur == 0 && a[i] == 0)continue;//首位不为0
		if (!book[i]) {
			c[cur] = a[i];
			book[i] = 1;
			dfs(cur + 1);
			book[i] = 0;
		}
	}
}
int main() {
	IOS;
	cin >> n >> k;
	for (int i = 0; i < n; i++)cin >> a[i];
	if (n == 1 && a[0] == 0) {
		cout << 0 << endl;
		return 0;
	}
	sort(a , a +n);
	dfs(0);
	if (flag)cout << cnt << endl;
	else cout << -1 << endl;
}

万湖之国
struct node {
	double x, y, R;
}lake[100005];
int st[100005];

bool cmp(node a, node b) {
	return a.x + a.R < b.x + b.R;//以圆的右端点排序
}

int Find(int x) {
	return st[x] == x ? x : st[x] = Find(st[x]);
	/*
	if(st[x]==x)return x;
	else return st[x]=Find(st[x]);
	*/
}
int main()
{
	IOS;
	int n;
	cin >> n;
	int num = n;
	for (int i = 0; i < n; i++)cin >> lake[i].x >> lake[i].y >> lake[i].R;
	sort(lake, lake + n, cmp);
	for (int i = 0; i < n; i++)st[i] = i;//并查集初始化
	for (int i = 0; i < n; i++) {
		for (int j = i - 1; j >= 0; j--) {
			if (lake[j].x + lake[j].R <= lake[i].x - lake[i].R)break;//第i个圆的左端点横坐标值大于等于前面的第j个圆右端点横坐标值则跳过
			if ((lake[i].x - lake[j].x) * (lake[i].x - lake[j].x) + (lake[i].y - lake[j].y) * (lake[i].y - lake[j].y) < (lake[i].R + lake[j].R) * (lake[i].R + lake[j].R)) {
				int x = Find(i);
				int y = Find(j);
				if (x != y) {
					st[x] = y;
					num--;
				}
			}
		}
	}
	cout << num << endl;
	return 0;
}

繁忙的公路
树状数组
ll a[1000005];
ll n;
ll lowbit(ll x) {
	return x & (-x);
}

void add(ll x, ll y) {
	while (x <= n) {
		a[x] += y;
		x += lowbit(x);
	}
}

ll getsum(ll n) {
	ll sum = 0;
	while (n) {
		sum += a[n];
		n -= lowbit(n);
	}
	return sum;
}

int main()
{
	IOS;
	ll m, x, y;
	cin >> n;
	cin >> m;
	char type;
	while (m--) {
		cin >> type >> x >> y;
		if (type == 'H') {
			add(x, y);
		}
		else {
			ll sum1 = getsum(x - 1);
			ll sum2 = getsum(y);
			cout << sum2 - sum1 << endl;
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值