题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
程序分析:像这样题型我们只能用数值带入去试,幸好我们有计算机。先把这个限定到100000之内,调用系统函数sqrt()开方,分别求出这两个temp1和temp2,然后在对他们平方比较是否和原来数相等
#include <stdio.h> #include <math.h> int main(int argc, const char * argv[]) { long i,temp1,temp2; for (i=0; i<100000; i++) { temp1 = sqrtf(i+100); temp2 = sqrtf(i+100+168); if ((temp1*temp1 == i+100) && (temp2*temp2 == i+100+168)) { printf("--------> %ld\n",i); } } return 0; }
#include <math.h> 这个头文件包含了一些数学函数,在用的时候不用管他是怎么实现的,只会调用就行
程序运行结果:
转载于:https://blog.51cto.com/duxinfeng/1208737