2016-09-10 Problem Solving►UVa UVa 256 - Quirksome Squares Contents 1. Problem2. Solution3. Code Problem中文網址 Solution建表。每次都只取完全平方數,最多只會到八位數 9999,9999。先求出平方後,再依照不同位數來拆解成兩個數字相加的平方,看會不會跟原先完全平方數相等。 CodeUVa 25612345678910111213141516171819202122232425262728293031#include<cstdio>int main(){ int ans[4][10], count[4] = {}; int div[4] = { 10,100,1000,10000 }; for (int i = 0; i < 10000; i++) { int square = i*i; for (int j = 0; j < 4; j++) { if (i < div[j])//總位數就不會是 n 了, 10 * 10 = 100 ,100 為 3 位數。 { int x = square / div[j] + square%div[j]; if (x == i) ans[j][count[j]++] = square; } } } int n; while (scanf("%d", &n) != EOF) { int idx = n / 2 - 1; for (int i = 0; i < count[idx]; i++) printf("%0*d\n", n, ans[idx][i]); } return 0;} Newer UVa 216 - Getting in Line Older UVa 11015 - 05-2 Rendezvous