7744问题&&floor()函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39216184/article/details/83141398

为向下取整,和四舍五入不同,floor(x);指从x左边取最大整数

包含在#include<math.h>中

问题:

输出所有形如aabb的4位完全平方数(即前两位数字相等,后两位数字也相等)。

#include<stdio.h>
#include<math.h>
int main() {
  for(int a = 1; a <= 9; a++)
    for(int b = 0; b <= 9; b++)    {
      int n = a*1100 + b*11; 
//这里才开始使用n,因此在这里定义n   
   int m = floor(sqrt(n) + 0.5);  //+0.5是为了防止误差(1变成0.999999999时floor为0)
   if(m*m == n) 
   printf("%d\n", n);  
  } 
 return 0; 
}

枚举:

#include<stdio.h>
int main(){
  for(int x = 1; ; x++){
    int n = x * x;
    if(n < 1000) continue; //跳出本次循环
    if(n > 9999) break;    //截至循环
    int hi = n / 100;    //前两位
    int lo = n % 100;    //后两位
    if(hi/10 == hi%10 && lo/10 == lo%10)
    printf("%d\n", n);
   }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39216184/article/details/83141398
今日推荐