首页 > 试题广场 >

平方根

[编程题]平方根
  • 热度指数:2273 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}给定一个正整数 n,求 \sqrt{n} 的整数部分,即对 \sqrt{n} 向下取整的结果。

\hspace{15pt}例如,\sqrt{5}=2.236\ldots 向下取整后为 2\sqrt{16}=4.000\ldots 向下取整后为 4

输入描述:
\hspace{15pt}在一行中输入一个整数 n \left(1 \leqq n \leqq 10^9\right)


输出描述:
\hspace{15pt}输出一个整数,表示 \sqrt{n} 向下取整后的值。
示例1

输入

5

输出

2

说明

\sqrt{5}\approx2.236,向下取整后为 2
示例2

输入

16

输出

4

说明

\sqrt{16}=4.000,向下取整后为 4
#include <stdio.h>
#include<math.h>
int main() {
    int a,b;
    scanf("%d %d", &a) ;
    b = sqrt(a);
    printf("%d\n", b );
    return 0;
}
发表于 2025-06-13 17:45:03 回复(0)
#include <iostream>
#include<cmath>
using namespace std;

int main() {
 int n;
 cin >> n;
 cout << floor(sqrt(n));
}

发表于 2025-06-10 14:55:19 回复(0)
import java.util.Scanner;
import static java.lang.Math.sqrt;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        System.out.print((int)sqrt(n)); 
    }
}

发表于 2025-06-07 22:30:52 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextFloat()) { // 注意 while 处理多个 case
            float a = in.nextFloat();
            double b = Math.sqrt(a);
            double c = Math.floor(b);
            System.out.println((int)c);
        }
    }
}
发表于 2025-06-06 10:56:31 回复(0)
from math import sqrt
a=int(input())
b=int(sqrt(a))
print(b)

发表于 2025-05-27 19:46:19 回复(0)
求大神解答为什么会超时
int main() {
    int a=0;
    int b = 1;
    scanf("%d", &a);
    while (b * b <= a) {
    
        b++;
    }
    printf("%d", b-1);
    return 0;
}

发表于 2025-05-23 13:10:00 回复(1)