相关疑难解决方法(0)

如何从浮点数中得到精确的小数部分作为整数?

关于浮点数的十进制和整数提取以及某些特定小数点的输出,有很多问题和答案.但没有人能解决我的问题.如果有人能帮助我解决我的问题,请

我实际上是试图从浮点数中提取精确的小数部分.我试过这个:

float f=254.73;

int integer = (int)f;
float fractional = f-integer;

printf ("The fractional part is: %f", fractional);
Run Code Online (Sandbox Code Playgroud)

但输出为:0.729996.出于这个原因,当我这样做时:

float f=254.73;

int integer = (int)f;
float fractional = f-integer;
int fractional_part_in_integer = ((int)(f*100)%100);

printf ("The value is: %d", fractional_part_in_integer);
Run Code Online (Sandbox Code Playgroud)

它给了我72作为输出.但是,我想从给定的数字254.73中精确地提取73.我已经知道%.2fprintf()函数期间如何使用打印最多两个十进制数字.但在我的代码中,我现在不想打印这个数字.我有一些计算,其中小数部分为整数形式,即73.所以,我的问题是如何从254.73中提取小数部分,以便我可以得到精确的73作为整数来进行更多的计算?

希望,我能让每个人都理解.

c floating-point

6
推荐指数
2
解决办法
7818
查看次数

代码优化子集和

这是我的代码,它打印子集的元素,其总和等于给定的总和,(它仅适用于正数):

#include <bits/stdc++.h>
using namespace std;

void traverse(vector<int> vec) {
    for(int a=0; a < vec.size(); a++)
        cout << vec[a] << " ";
    cout << endl;
}

void possible(vector<int> vec, int sum, vector<int> now) {
    if(sum == 0) {
        traverse(now);
    }
    else if(sum < 0) {
        now.clear();
    }
    else if(sum > 0 && vec.size() > 0) {
        for(int a = 0; a < vec.size(); a++) {
            now.push_back(vec[a]);
            vector<int> vecc(vec.begin() + a + 1, vec.end());
            possible(vecc, sum - vec[a], now);
            now.erase(now.end() - …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm performance subset dynamic-programming

6
推荐指数
1
解决办法
1302
查看次数

“std_lib_facilities.h”显示错误

我正在使用 Codeblocks 17.12 并且已经将编译器设置设置为 C++11 标准。我正在学习 Bjarne Stroustrup 的书“编程 - 使用 C++ 的原则和实践”。在他的书中,他要求包括“std_lib_facilities.h”。我从他的网站复制它并保存在“Mingw”文件夹的“include”文件夹中。之后,我开始制作一个简单的程序:

#include<iostream>
#include "std_lib_facilities.h"
main()
{
    std::cout<<"Hello world";
}
Run Code Online (Sandbox Code Playgroud)

但是编译器显示以下错误和警告:

 warning: This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date.  
 Please use a non-deprecated interface with equivalent functionality instead. 
 For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp]

 error: template-id 'do_get<>' for 'String > 
   std::__cxx11::messages<char>::do_get(std::messages_base::catalog, int, …
Run Code Online (Sandbox Code Playgroud)

c++ header

6
推荐指数
2
解决办法
4979
查看次数

如何修复“错误:调用 'abs' 不明确”

我正在从 HackerRank 运行一个关于指针的简单 C++ 程序,它在网站上运行良好。但是,当我在 MacOS 上运行它时,我得到了error: call to 'abs' is ambiguous并且我不确定到底什么是模棱两可的。

我查看了类似问题的其他答案,但错误消息往往是Ambiguous overload call to abs(double),这不是我遇到的问题,因为我没有使用任何双打。我也试过包括头文件cmathmath.h,但问题仍然存在。

#include <stdio.h>
#include <cmath>

void update(int *a,int *b) {
    int num1 = *a;
    int num2 = *b;
    *a = num1 + num2;
    *b = abs(num1 - num2);
}

int main() {
    int a, b;
    int *pa = &a, *pb = &b;

    scanf("%d %d", &a, &b);
    update(pa, pb);
    printf("%d\n%d", a, b);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题出现在第 8 …

c++ cmath

6
推荐指数
3
解决办法
2万
查看次数

为什么我们实际上需要运行时多态性?

我试图理解多态性,但我不明白如果静态多态性可以很好地调用类的成员,为什么我们需要运行时多态性。

就像,假设这是一个问题。

#include <bits/stdc++.h>
using namespace std;
class base{
    public:
        virtual void fun(){
            cout<<"base called"<<endl;
        }
};
class derived:public base{
    public:
        void fun(){
            cout<<"derived called"<<endl;
        }
};
int main() {

    base b,*b1;
    derived d;
    b1 = &d;
    b1->fun();
    // b.fun();
    // d.fun();
}
Run Code Online (Sandbox Code Playgroud)

假设这是我的代码,我想访问派生类或基类的函数,我可以通过简单地创建该类的对象来做到这一点,所以如果没有问题,那么为什么我们尝试使用引用来调用该对象(运行时多态性)。有人可以解释运行时多态性的实际需要吗?或者如果可能的话,您可以通过使用任何现实生活场景来解释它吗?

c++ oop polymorphism

6
推荐指数
1
解决办法
3877
查看次数

最小化高度之间的最大差异

给定 n 个塔的高度和一个值 k。我们需要将每个塔的高度增加或减少 k(仅一次),其中 k > 0。任务是最小化修改后最长和最短塔的高度之间的差异,并输出该差异。

我得到了解决方案背后的直觉,但我无法评论下面解决方案的正确性。



// C++ program to find the minimum possible 
// difference between maximum and minimum 
// elements when we have to add/subtract 
// every number by k 
#include <bits/stdc++.h> 
using namespace std; 
  
// Modifies the array by subtracting/adding 
// k to every element such that the difference 
// between maximum and minimum is minimized 
int getMinDiff(int arr[], int n, int k) 
{ 
    if (n == 1) 
       return 0; 
  
    // Sort all elements …
Run Code Online (Sandbox Code Playgroud)

c++ arrays optimization greedy

6
推荐指数
3
解决办法
6841
查看次数

这两个表达有什么区别

#include <bits/stdc++.h>
using namespace std;
void test(){
    int a = 100;
    cout << a << endl;
}
int main()
{
    void(*b)() = test;
    (*b)();         //expression one
    b();            //expression two
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

test是一个函数指针,不是吗?(*b)()是正确的形式,因为它相当于函数原型。但为什么删除一个符号是正确的呢*?他们都产生了正确的结果。

c++

6
推荐指数
1
解决办法
210
查看次数

C++ 在同一 shell 实例上运行 shell 命令

正如标题所说,我想在 C++ 中的同一个 shell 进程/实例上运行 shell 命令并与其通信,我该怎么做?我查遍了每个角落,但找不到合适/直截了当的答案。我不是C++霸主,我的回答可能很愚蠢。现在我可以在unix上使用 fork/exec 的组合,但我在 Windows 上。如果有跨平台的解决方案,请在下面提及。提前致谢。

伪代码:

SHELL shell = make_shell();
shell.run("cd desktop");
shell.run("dir");
print(shell.stdout)
Run Code Online (Sandbox Code Playgroud)

c++ shell

6
推荐指数
1
解决办法
1208
查看次数

为什么我无法在 GCC 13.x 版本中使用 #pragma GCC target(...) 声明字符串类型变量

我刚刚将 GCC 编译器版本从 11.4 更新到 13.1。我发现以下代码曾经在我的旧 GCC 11.4 上运行,但不再在 GCC 13.1 上运行。

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")

#include <iostream>

int main() {
    std::string s = "Hello World!";
    std::cout << s << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这是编译器消息:

====================[ Build | run | Debug ]=====================================
"C:\Program Files\JetBrains\CLion 2023.3.2\bin\cmake\win\x64\bin\cmake.exe" --build C:\Users\zyk\CLionProjects\run\cmake-build-debug --target run -j 18
[1/2] Building CXX object CMakeFiles/run.dir/main.cpp.obj
FAILED: CMakeFiles/run.dir/main.cpp.obj 
C:\PROGRA~1\JETBRA~1\CLION2~1.2\bin\mingw\bin\G__~1.EXE   -g -fdiagnostics-color=always -MD -MT CMakeFiles/run.dir/main.cpp.obj -MF CMakeFiles\run.dir\main.cpp.obj.d -o CMakeFiles/run.dir/main.cpp.obj -c C:/Users/zyk/CLionProjects/run/main.cpp
In file included from C:/Program Files/JetBrains/CLion 2023.3.2/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/string:43,
                 from C:/Program Files/JetBrains/CLion …
Run Code Online (Sandbox Code Playgroud)

c++ gcc g++

6
推荐指数
1
解决办法
365
查看次数

从类方法中调用命名空间中名称相同的函数时,重载解析失败

在下面的代码中,很明显,myFn带有两个参数的函数定义应该来自namespace N。但是编译器无法编译它。它是编译器(g ++ 8.3)的限制,还是由C ++标准强加的?

    #include <bits/stdc++.h>

    using namespace std;

    namespace N
    {
      // Same name function exists in class A
      void myFn(int a, int b)
      {
        cout << a << ' ' << b << endl;
      }
    }

    using namespace N;
    class A {
      public:
        void myFn(int a)
        {
    #ifdef FINE
          // Explicitly specify where should myFn definition come from
          N::myFn(a, a);
    #else
          myFn(a, a);
    #endif
        }
    };

    int main()
    {
      A a;
      a.myFn(3);
      return 2; …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces

5
推荐指数
1
解决办法
56
查看次数