提示2-3 編寫程序時,要特別留意「當前行」的跳轉和變量的改變。函數
例題2-1 aabbspa
輸出全部形如aabb的四位徹底平方數(即前兩位數字相等,後兩位數字也相等)。操作系統
方法1:code
1 #include <stdio.h> 2 #include <math.h> 3 4 int main() 5 { 6 int a, b, n; 7 double m; 8 9 for (a = 1; a <= 9; a++) 10 for (b = 0; b <= 9; b++) 11 { 12 n = a * 1100 + b * 11; 13 m = sqrt(n); 14 if (floor(m + 0.5) == m) 15 printf("%d\n", n); 16 } 17 18 return 0; 19 }
方法2:blog
1 #include <stdio.h> 2 3 int main() 4 { 5 int x, n, high, low; 6 7 for (x = 1; ; x++) 8 { 9 n = x * x; 10 if (n < 1000) continue; 11 if (n > 9999) break; 12 high = n / 100; 13 low = n % 100; 14 if (high/10 == high%10 && low/10 == low%10) 15 printf("%d\n", n); 16 } 17 18 return 0; 19 }
提示2-13: 能夠使用time.h和clock()函數得到程序運行時間。常數CLOCKS_PER_SEC和操做系統相關,請不要直接使用clock()的返回值,而應老是除以CLOCKS_PER_SEC。 io
scanf函數也有返回值,它返回的是成功輸入的變量的個數;當輸入結束時,scanf沒法再次讀取則返回0。class
提示2-15:在Windows下,輸入完畢後先按Enter鍵,再按CTRL+Z鍵,最後再按Enter鍵,便可結束輸入。在Linux下,輸入完畢後按CTRL+D鍵便可結束輸入。變量
提示2-16:變量在未賦值以前的值是不肯定的。特別地,它不必定等於0。程序