RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 582638
Accepted
Max
Max
Asked:2020-10-25 22:43:41 +0000 UTC2020-10-25 22:43:41 +0000 UTC 2020-10-25 22:43:41 +0000 UTC

无法弄清楚出了什么问题[关闭]

  • 772
关闭 这个问题是题外话。目前不接受回复。

寻求调试帮助的问题(“为什么这段代码不起作用? ”)应该包括所需的行为、具体问题或错误,以及在问题中正确重现它的最少代码。没有明确描述问题的问题对其他访问者来说是无用的。请参阅如何创建最小、独立且可重现的示例。

5 年前关闭。

改进问题

给定一个整数 N,按升序打印出所有不超过 N 的自然数的平方。输入数据格式 输入自然数。输出格式 输出问题的答案。

示例输入:50

示例输出:1 4 9 16 25 36 49

#include <iostream>
#include <cmath>
using namespace std;

int main() {
  int a, b = 1;
  cin >> a;
  while (b <= a) {
        if (float(sqrt(b)) % 1 == 0) {
            cout << b;
        }
        b++;
    }
  return 0;
}
c++
  • 4 4 个回答
  • 10 Views

4 个回答

  • Voted
  1. Vlad from Moscow
    2020-10-25T22:58:44Z2020-10-25T22:58:44Z

    C++ 没有为浮点数定义余数运算符%。

    因此,这个 if 子句中的条件

    if (float(sqrt(b)) % 1 == 0) {
    

    错误的。

    写出下面的循环会更容易

    unsigned int a;
    
    std::cin >> a;
    
    for ( unsigned int i = 1, b; ( b = i * i ) <= a; i++ ) std::cout << b << ' ';
    std::cout << std::endl;  
    

    无需包含标头cmath并使用其中声明的函数sqrt。

    该程序可以变得更有意义。例如,

    #include <iostream>
    
    int main() 
    {
        while ( true )
        {
            std::cout << "Enter a non-negative number (0 - exit): ";
    
            unsigned int a;
    
            if ( not ( std::cin >> a ) || a == 0 ) break;
    
            for ( unsigned int i = 1, b; ( b = i * i ) <= a; i++ ) std::cout << b << ' ';
            std::cout << std::endl;
        }
    
        return 0;
    }
    

    与程序的对话可能看起来像这样

    Enter a non-negative number (0 - exit): 100
    1 4 9 16 25 36 49 64 81 100 
    Enter a non-negative number (0 - exit): 50
    1 4 9 16 25 36 49 
    Enter a non-negative number (0 - exit): 25
    1 4 9 16 25 
    Enter a non-negative number (0 - exit): 0
    
    • 2
  2. Best Answer
    Anton Shchyrov
    2020-10-25T22:59:15Z2020-10-25T22:59:15Z

    这条线的意思对我来说是个谜。

    if (float(sqrt(b)) % 1 == 0) {

    求一个数除以 1 的平方根后的余数。你想说什么?想检查一个数字是否是整数?事情不是这样的

    if (1 - floor(sqrt(b)) < 1e-6) {
    

    你的任务是这样解决的

    int b = 1;
    int c = 1;
    while (c <= a) {
      cout << c;
      b++;
      c = b * b;
    }
    
    • 1
  3. Harry
    2020-10-25T23:30:36Z2020-10-25T23:30:36Z

    在我看来,最大的错误是通过对所有数字进行排序并找出这个数字是否是正方形来解决你的问题。最好从另一个方向去,一个一个地输出方块,只要它们小于要求a的(代码特别简化,让你看得更清楚):

    #include <iostream>
    using namespace std;
    
    int main() 
    {
        int a,b = 1;
        cin >> a;
        while(b*b <= a)
        {
            cout << b*b << " ";
            ++b;
        }
        cout << endl;
    
    }
    
    • 1
  4. Senior Pomidor
    2020-10-25T23:01:51Z2020-10-25T23:01:51Z
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main() {
      int a,sqr = 1, b = 1;
    
      cin >> a;
      while (sqr <= a) {
        cout << sqr << " ";
        b++;
        sqr = b * b;;
      }
      return 0;
    }
    

    结论:

    50
    1 4 9 16 25 36 49
    500
    1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484
    
    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5