UVA - 138 - Street Numbers (简单数论)


题目传送: UVA - 138



思路:题意好难懂,看了半天,还是搜了题解才搞懂题意,真是给英语跪啦


m在家的位置,而n是最后一个位置,直接输出前10组满足1~m-1的和 == m+1 ~ n的和,这是题意;


然后通过题意可以得到n*(n+1)/2 - m - m*(m-1)/2 = m*(m-1)/2;

化简可得2*m*m = n*(n+1);

然后可以通过枚举n来求得m,看m是否为整数(可由double到int判断),是整数则满足情况


打表代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int main() {
	freopen("out.txt", "w", stdout);
	for(int n = 6; n < INF / 10; n++) {
		double m = sqrt((double)n * (n + 1) / 2);
		int M = m;
		if(fabs((double)M - m) < 1e-8) {
			printf("printf(\"%%10d%%10d\\n\", %d, %d);\n", M, n);
		}
	}
	return 0;
}


AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int main() {
	printf("%10d%10d\n", 6, 8);
	printf("%10d%10d\n", 35, 49);
	printf("%10d%10d\n", 204, 288);
	printf("%10d%10d\n", 1189, 1681);
	printf("%10d%10d\n", 6930, 9800);
	printf("%10d%10d\n", 40391, 57121);
	printf("%10d%10d\n", 235416, 332928);
	printf("%10d%10d\n", 1372105, 1940449);
	printf("%10d%10d\n", 7997214, 11309768);
	printf("%10d%10d\n", 46611179, 65918161);
	//printf("%10d%10d\n", 85225144, 120526554);
	//printf("%10d%10d\n", 139833537, 197754484);
	return 0;
}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值