题目传送: 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; }
UVA - 138 - Street Numbers (简单数论)
原文地址:http://blog.csdn.net/u014355480/article/details/45279377