UVA12027 Very Big Perfect Squares【数学】

An integer, n, is called a perfect square if there is another integer, m, such that n = m × m. Examples of perfect squares are 1, 4, 9, 16, 25, 36, etc. That is: 1, 2^2, 3^2, 4^2, 5^2, 6^2, etc.
    Given a natural number, A, we want to know how many perfect squares exist between 1 and A, inclusive. For example, between 1 and 1 there is only one perfect square (1), between 1 and 5 there are 2 (1 and 4), between 1 and 40 there are 6 (1, 4, 9, 16, 25 and 36).
    We want to work with very big numbers, so we are not very interested in accuracy. We only need the first digit of the result, and the rest of digits can be 0.
Input
The input can contain different test cases.
    For each test case, there is a line with a natural number A.
    You can assume that A is between 1 y 101000, inclusive.
    The input ends with a line with the value: ‘0’.
Output
For each test case, the output should be a line with a natural number N, indicating how many perfect squares exist between 1 and A, inclusive.
    As we do not need too much precision, you only have to output the most significant digit of the result. You have to complete the rest of the result with zeros. For example, if the result is 59, then you have to output ‘50’; if the result is 12345, then you have to output: ‘10000’.
Sample Input
1
5
40
1000
0
Sample Output
1
2
6
30

问题链接UVA12027 Very Big Perfect Squares
问题简述:(略)
问题分析
    数学题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA12027 Very Big Perfect Squares */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    while(cin >> s && s != "0") {
        int len = s.length();
        long long x = 0;
        if(len & 1) {
            len++;
            x += s[0] - '0';
        } else
            x += (s[0] - '0') * 10 + s[1] - '0';

        printf("%lld", (long long)sqrt(x));
        for(int i = 1; i < len / 2; i++)
            putchar('0');
        putchar('\n');
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值