C語言經典例題100道 第三題
阿新 • • 發佈:2019-02-11
題目
題目:一個整數,它加上100後是一個完全平方數,再加上268又是一個完全平方數,請問該數是多少?
#include<stdio.h>
#include<math.h>
int main()
{
int i,x,y;
for(i=0;i<10000000;i++)
{
x= sqrt(i+100);
y= sqrt(i+268);
if(x*x==i+100&&y*y==i+268)
printf("%d\n",i);
}
}
在使用Geany執行的時候出現瞭如下錯誤
gcc -Wall -o "2" "2.c" (在目錄 /home/hdy/C100 中) 2.c: 在函式‘main’中: 2.c:14:1: 警告: 在有返回值的函式中,控制流程到達函式尾 [-Wreturn-type] /tmp/cc2Nv8dw.o: In function `main': 2.c:(.text+0x33): undefined reference to `sqrt' 2.c:(.text+0x4b): undefined reference to `sqrt' collect2: ld 返回 1 編譯失敗。
百度說
Linux 中使用gcc編譯器 需要加
-lm 作為連結,呼叫數學函式庫math.h
好吧,比較坑 當時沒有加 \n 出現了 212611581 想怎麼這麼大的數咧,丟人了
好吧 到終端輸入命令吧