5.25测试错题总结

basibatuo / 2024-08-04 / 原文

D1100 不定方程求解

错误代码(AC了,但是写法有问题)

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
typedef long long ll;
int main()
{
	freopen("as01.in","r",stdin);
	freopen("as01.out","w",stdout); 
	int a,b,c;
	cin>>a>>b>>c;
	int cnt=0;
	for(int i=1;i<=c;i++)
		for(int j=1;j<=c;j++)
			if(a*i+b*j==c||a*j+b*i==c)
				cnt++;
	cout<<cnt;
	return 0;
}

错误点

for (int i = 1; i <= a *b *c; i++)  //应该从0开始,i最大就为c
	for (int j = 1; j <= a *b *c; j++)  //应该从0开始,j最大就为c
		if (a * i + b * j == c || a * j + b * i == c)//i代表x,j代表y,不要再判断一次
			cnt++;

AC代码

#include <iostream>
#include <climits>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <stack>
#include <map>
#include <queue>
using namespace std;
const int N = 1e5 + 2;

int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int a, b, c, cnt = 0;
	cin >> a >> b >> c;
	for (int i = 0; i <= c; i++)
		for (int j = 0; j <= c; j++)
			if (a * i + b * j == c)
				cnt++; //必须压行(doge)
	cout << cnt;
	return 0;
}

A1115 不定方程求解

错误代码

没写

错误点

没写,所以没有错误点。

AC代码(借鉴老师代码(重新打了一遍))

#include <bits/stdc++.h>
using namespace std;

int main() {
	int x, y, w, n, ans;
	cin >> n >> x >> y;
	for (int i = 1; i <= n; i++)
		cout << "(" << x << "," << i << ") ";
	cout << "\n";
	for (int i = 1; i <= n; i++)
		cout << "(" << i << "," << y << ") ";
	cout << "\n";
	w = y - x;
	for (int i = 1; (i <= n); i++)
		if (i + w >= 1 && i + w <= n)
			cout << "(" << i << "," << i + w << ") ";
	cout << "\n";
	ans = x + y;
	for (int i = n; i >= 1; i--)
		if (ans - i >= 1 && ans - i <= n)
			cout << "(" << i << "," << ans - i << ") ";
	return 0;
}