小编mkr*_*er1的帖子

为什么在 % 样式打印格式中使用算术表达式会出现类型错误?

我尝试使用两种方法输入浮点数并输出简单的结果:

t = float(input())
print('{:.2f}'.format(1.0 - 0.95 ** t))
print('%.2f' % 1.0 - 0.95 ** t)
Run Code Online (Sandbox Code Playgroud)

第一种方法有效,但第二种方法出现类型错误:

- 不支持的操作数类型:“str”和“float”。

这有什么问题吗?

python string-formatting python-3.x

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

std::unique 不能按预期与结构一起工作

我目前正在开发一款 2D 游戏,其中关卡由边缘定义:

struct Edge
{
    vec2int start;
    vec2int end;
}
Run Code Online (Sandbox Code Playgroud)

该结构vec2int是一个具有 x、y 坐标的向量,并且重载了所有需要的运算符(在本例中operator==)。由于存储网格内部边缘的数据结构,网格内部的不同单元格中可能存在重复的边缘。当将它们重新组合成一个时,std::vector<Edge>我试图像这样摆脱它们:

auto it = std::unique(
    edges.begin(),
    edges.end(),
    [&](const Edge& e1, const Edge& e2)
    {
        return e1.start == e2.start && e1.end == e2.end;
    });

edges.resize(std::distance(edges.begin(), it));
Run Code Online (Sandbox Code Playgroud)

无论出于何种原因,这只会删除一些(或没有)重复边缘。我不知道为什么。我有什么遗漏的吗std::unique

代码:

#include <algorithm>
#include <iostream>
#include <vector>

template<class T>
struct v2d_generic
{
    T x = 0;
    T y = 0;
    
    bool operator==(const v2d_generic& rhs) const
    {
        return (this->x == rhs.x && this->y == …
Run Code Online (Sandbox Code Playgroud)

c++ c++17

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

Java泛型方法无法解析“T”中的方法“getX()”

假设我有几个对象(=类),每个对象都有一个方法getX()

public class A{
   /* some code */
   public float getX(){}
}
public class B{
   /* some code */
   public float getX(){}
}
Run Code Online (Sandbox Code Playgroud)

现在我想编写一个通用静态方法,如下所示:

public static <T> boolean isOverlaps(T obj) {
   if (obj == null || (!obj.getClass().isInstance(A.class) && !obj.getClass().isInstance(B.class)))
       return false;
   return obj.getX() >= 0 && /*some logic*/; // here it falls
}
Run Code Online (Sandbox Code Playgroud)

IDE 说:

无法解析“T”中的方法“getX()”

如何在不进行强制转换的情况下正确解析该方法(因为它是通用方法)?有可能吗?

java

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

为什么在处理二进制字符串的问题中使用整数比使用字符串慢得多?

我通过使用字符串(class )和整数(class )解决了以下leet代码问题https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/。我认为整数解决方案应该更快并且使用更少的内存。但实际上需要更长的时间。当 n=18 和 k=200 时,需要 10.1 秒,而字符串解决方案需要 0.13 秒。SolutionSolution2

import time

class Solution:
    def findKthBit(self, n: int, k: int) -> str:
        i = 0
        Sn = "0"
        Sn = self.findR(Sn, i, n)
        return Sn[k-1]

    def findR(self, Sn, i, n):
        if i == n:
            return Sn
        newSn = self.calcNewSn(Sn, i)
        return self.findR(newSn, i+1, n)

    def calcNewSn(self, Sn, i):
        inverted = ""
        for c in Sn:
            inverted += "1" if c == "0" else "0"
        newSn …
Run Code Online (Sandbox Code Playgroud)

python biginteger

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

*(指针) 和 (*指针) 有什么区别?

请参阅以下代码块。*(sample)你能告诉我和之间的区别吗(*sample)

for(i = 0; i < num_samples ; i++ ) 
    {
        *(sample) &= 0xfff ;
        
        if( (*sample) & 0x800 ) 
            *(sample) |= 0xf000 ;
            *(sample+1) &= 0xfff ;
            
        if( *(sample+1) & 0x800 ) 
            *(sample+1) |= 0xf000 ;
            
        fprintf( my_data->fout, "%d, %d\n", *sample, *(sample+1) );
        
        sample += 2 ;
    }
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

打开 vim 抛出错误“尾随字符”

我的配置文件很小。这是我的完整内容.vimrc

# Enable mouse
set mouse=a

" Enable syntax highlighting
syntax on

# Add numbers on the left side
set number
Run Code Online (Sandbox Code Playgroud)

打开 vim 会抛出错误“尾随字符”。

每次我尝试打开 Vim 时,都会出现这些错误。我不明白这个错误。那么我在这里做错了什么?

错误如下:

截屏:

https://i.stack.imgur.com/Xy2qt.png

文本:

dana@sunyata ~ % vim .vimrc
Error detected while processing /Users/dana/.vimrc:
line    1:
E488: Trailing characters: Enable mouse: # Enable mouse
line    7:
E488: Trailing characters: Add numbers on the left side: # Add numbers on the left side
line   10:
E488: Trailing characters: Ability to copy from Vim …
Run Code Online (Sandbox Code Playgroud)

vim

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

功能中的非详尽模式 - Maybe 类型

在下面的函数定义中,我在函数 tapChar 中得到了非详尽模式。

我缺少什么?

reverseTap :: DaPhone -> Char -> [(Digit, Presses)]
reverseTap phone s 
    | isUpper s  = ('*',1) : tapChar (toLower s) phone
    | otherwise = tapChar s phone
    where tapChar s (DaPhone[buttons]) = map (\x -> case (elemIndex s $ phChar x) of
                                         (Just i)  ->  (digit x , i)
                                         Nothing   ->  (' ', (-1))) [buttons]
          phChar (Button _ chars) = chars
          digit  (Button d _)     = d
Run Code Online (Sandbox Code Playgroud)

haskell pattern-matching where-clause option-type

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

如何使用“with”作为 TypedDict 中的键?

我想在 python 3.10 中用作witha 的键TypedDict

我有:

from typing import TypedDict, Optional

class Operation(TypedDict, total=False):
    uses: str
    with: Optional[ActionCheckout]
Run Code Online (Sandbox Code Playgroud)

但是我的IDE说我不能这样做?

想法抱怨

python dictionary types

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

为什么单个 Python 脚本中的两个主要函数可以工作?

我试图用 Python 做一些愚蠢的事情,并尝试了最愚蠢的事情(见下文)来看看 Python 的反应。令我惊讶的是它执行得非常完美。但我不明白为什么。

Python 如何知道要foo执行哪个?为什么它不执行相同的foo两次?

def main():
    foo()

def foo():
    print('this is foo 1.')

if __name__ == '__main__':
    main()

def main():
    foo()

def foo():
    print('this is foo 2.')

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
2
解决办法
215
查看次数

逻辑 AND OR 运算符和一个简单的 C++ 问题

我正在处理我的工作簿中的一个 C++ 问题,并且我在这个问题(以及我遇到的许多其他问题)中的逻辑运算符的行为方面遇到了困难。

这是代码:

#include <iostream>


using namespace std;



int main()
{
string input1, input2;

cout << "Enter two primary colors to mix. I will tell you which secondary color they make." << endl;
cin >> input1;
cin >> input2;


if ((input1 != "red" && input1 != "blue" && input1 != "yellow")&&(input2 != "red" && input2 != "blue" && input2 != "yellow"))
{
    cout << "Error...";
}

else
{
    if (input1 == "red" && input2 == "blue")
    {
        cout << …
Run Code Online (Sandbox Code Playgroud)

c++ boolean-logic

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