在问题中将会有一个从用户那里获取的号码。直到该数字,如果该数字的数字总和为奇数,我想找到所有数字的总和。\n例如,如果输入为 13,则 ;\n1+3+5+7+9+10+12 = 47应该是结果。\n为了防止超出时间限制,问题显示“由于答案可能太大,请以模 10^9+7 打印答案”
\n约束\n1 \xe2\x89\xa4 n < 10^17(n 为输入)
\n这是我的代码:
\nMOD = 1000000007\ndef getSum(number):\n total = 0\n while number > 0:\n total += number % 10\n number //= 10\n return (total % MOD) % 2\n\ndef sticker(number):\n stickerNeed = 0\n for oneDigit in range(1, number):\n if (getSum(oneDigit % MOD) == 1):\n stickerNeed += oneDigit\n return stickerNeed % MOD\n\n\nnumber = int(input())\nresult = sticker(number)\nprint(result % MOD)\nRun Code Online (Sandbox Code Playgroud)\n但它仍然无法处理真正大的数字并给出时间限制。你能帮我吗?我的代码有什么问题吗?
\n我有一个数据集,如下所示:
sample1 <-
structure(list(ID = c( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
L = c(1.31, 1.62, 1.93, 2.24, 2.55, 2.86, 3.17, 3.48, 3.79, 4.10)),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L))
Run Code Online (Sandbox Code Playgroud)
我需要将“L”列变量四舍五入到最接近的 0.00、0.25、0.5 和 0.75。我非常感谢您提供的任何帮助。
Python中有没有一种方法可以初始化一个变量,以便它可以与+= "a string"或一起工作+= 100使用?\n例如:
a = <what to put here>\na += 100 # a = 100\nb = <same what to put here from above> \nb += "a string" # b = "a string"\nRun Code Online (Sandbox Code Playgroud)\n如果我使用:
\na = ""\na += "a string" # this will work fine\nb = ""\nb += 0 # -> TypeError\nRun Code Online (Sandbox Code Playgroud)\n一旦定义了变量的类型,TypeError如果我尝试+=不同的类型,之后就可以得到 a 。例如:
a = <what to put here>\na += "a string" # a = …Run Code Online (Sandbox Code Playgroud) 我试图了解内存分配以及如何减少它们。
我创建了以下长列表进行测试
var list = new List<int>(Enumerable.Range(0, 1_000_000).ToArray());
Run Code Online (Sandbox Code Playgroud)
然后我循环它并打印一个像这样的字符串
for (var i = 0; i < list.Count; i++)
{
Console.WriteLine("Item # " + list[i]);
}
Run Code Online (Sandbox Code Playgroud)
上一个循环生成了超过 200 万次分配。我相信该行"Item # " + list[i]产生了 200 万个分配。
**问题1**为什么在这种情况下会分配200万个字符串?是否list[i]必须作为字符串存储在堆中,这是 1 次分配,然后是另一个分配中的组合字符串,因此每个循环 2 次分配?
下一步我考虑使用字符串生成器来减少分配
var builder = new StringBuilder();
for (var i = 0; i < list.Count; i++)
{
builder.Append("Item # ");
builder.Append(list[i]);
Console.WriteLine(builder.ToString());
builder.Clear();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码有 100 万次分配,是之前分配的一半。
但是,仍然有很多分配。
我知道字符串是 C# 中的不可变对象,每个字符串在堆中都有自己的分配。但是,有没有一种方法可以重用内存分配,以便我们可以创建 1 个字符串,然后在该循环内一遍又一遍地重用相同的分配?就我而言,一旦打印了字符串,我就不再需要它了。对我来说,重用相同的分配并更新它的值是安全的。
**问题2** 是否可以重复使用内存分配来减少分配量?
** 问题 3** …
我需要将类的实例写入变量,并能够访问它以获取其值并访问其方法。我可以以某种方式实现这个吗?
例如:
class A
def initialize(value)
@value = value
end
def preview
puts "Class preview: #{@value}"
end
def something(param)
puts "Something method: #{@value * param}"
end
end
class B
attr_reader :obj
def set_object(obj)
@obj = obj
end
end
b = B.new
b.set_object(A.new(5))
b.obj # ==> 5
10 + b.obj # ==> 15
b.obj.preview # ==> "Class preview: 5"
b.obj.something(3) # ==> "Something method: 15"
Run Code Online (Sandbox Code Playgroud) 在 Apple Silicon Mac 上运行以下 shell 脚本(在配备 M1 的 MacBook Air 2020 和配备 M3 Max 的 MacBook Pro 16" 上尝试;两者都运行 macOS Sonoma,配备 Apple clang 15.0):
#!/bin/bash
rm -rf NTRU
git clone https://github.com/vector-polymul-ntru-ntrup/NTRU.git
cd NTRU/ntruhps2048677/aarch64_tmvp
cat << EOF > speed_polymul.c
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "api.h"
#include "params.h"
#include "poly.h"
#include "tmvp.h"
#include "batch_multiplication.h"
#define NTESTS 1000
uint64_t time0, time1;
uint64_t cycles[NTESTS];
#ifdef __APPLE__
#include "m1cycles.h"
#define __AVERAGE__
#define SETUP_COUNTER() {(void)cycles; setup_rdtsc();}
#define CYCLE_TYPE "%lld"
#define …Run Code Online (Sandbox Code Playgroud) 我有以下测试:
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
public class Program
{
public static void Main()
{
IDictionary<string, string> dictionary = new ConcurrentDictionary<string, string>(); // FAILS sometimes
// ConcurrentDictionary<string, string> dictionary = new ConcurrentDictionary<string, string>(); // WORKS always
Parallel.For(0, 10, i => dictionary.TryAdd("x1", "y1"));
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在 .NET Fiddle 中运行它,有时会出现以下异常:
未处理的异常。System.AggregateException:发生一个或多个错误。(该键已存在于字典中)
System.ArgumentException:该键已存在于字典中。在 System.Collections.Concurrent.ConcurrentDictionary`2.System.Collections.Generic.IDictionary<TKey,TValue>.Add(TKey key, TValue value)
在 System.Collections.Generic.CollectionExtensions.TryAdd[TKey,TValue](IDictionary` 2 字典,TKey 键,TValue 值)
在 Program.<>c__DisplayClass0_0.b__0(Int32 i)
在 System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.b__1(RangeWorker& currentWorker, Int64 timeout, Boolean&replicationDelegateYieldedBeforeCompletion)
---之前位置的堆栈跟踪结束 ---
在 System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.b__1(RangeWorker& currentWorker, Int64 timeout, Boolean&replicationDelegateYieldedBeforeCompletion)
在 …
我正在尝试学习 AVR C,并且正在修补 atmega328p 微控制器。我正在 Linux Mint 上使用 avr-gcc 工具链来编译我的代码并将其烧写到 Arduino 板上。
所以我尝试使用函数调用使两个 LED 灯闪烁。我制作了两个函数 - 一个用于连接到 Arduino 板的引脚 7 的闪烁 LED,另一个用于连接到 Arduino 板的引脚 13 的闪烁 LED。
这些功能正在运行,但是当我修改代码时,我注意到一个奇怪的行为。这些函数按照它们定义的顺序在 main() 之外被调用和执行;而不是按照它们从 内部调用的顺序int main()。
这是我的代码
#include <avr/io.h>
#include <util/delay.h>
void blinkPin13() {
while(1) {
DDRB = DDRB | (1 << DDB5);
PORTB = PORTB | (1 << PORTB5);
_delay_ms(5000);
PORTB = PORTB & ~(1 << PORTB5);
_delay_ms(500);
}
}
void blinkPin7() {
while(1) {
DDRD = DDRD | (1 << …Run Code Online (Sandbox Code Playgroud) 我有一个沉浸式空间,我想向其中添加手势识别器。我希望能够在沉浸式空间中的任何位置检测捏/点击手势。
ImmersiveSpace(id: "FlappyImmersiveSpace") {
FlappySpace()
}
Run Code Online (Sandbox Code Playgroud)
我尝试过这个,但没有成功。
ImmersiveSpace(id: "FlappyImmersiveSpace") {
FlappySpace()
.gesture(
TapGesture()
.onEnded({ _ in
print("TAPPED")
})
)
}
Run Code Online (Sandbox Code Playgroud)
这也不行。
struct FlappySpace: View {
var body: some View {
RealityView { content in
// Logic
}
.gesture(
TapGesture()
.onEnded({ _ in
print("TAPPED")
})
)
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何检测沉浸式空间中的手势,手势不能特定于空间中的实体,而是整个空间中的任何位置。特别想检测点击手势(在 VisionOS 中,这是捏)。
考虑声明一个不受约束的ns::operator*. using namespace ns在块作用域中并调用函数之后foo<T>,clangns::operator*在读取内部基于范围的循环的迭代器时使用foo。没有其他类型ns涉及其他类型,因此 ADL 应该不会产生任何候选者。
在以下示例中,static_assert失败并显示消息:
错误:由于要求 'std::is_same_v<const int &, const custom_type &>',静态断言失败
汇编代码显示这ns::operator*是由 clang 使用的。gcc 和 msvc 的断言都通过了!
namespace ns {
template <typename T>
constexpr auto operator*(T&& /*value*/) {
struct custom_type {};
return custom_type{};
};
} // namespace ns
template <typename T>
constexpr void foo() {
std::vector<T> vec{};
for (const auto& curr : vec) {
static_assert(std::is_same_v<const T&, decltype(curr)>);
}
}
int main() { …Run Code Online (Sandbox Code Playgroud) c# ×2
performance ×2
python ×2
.net ×1
algorithm ×1
atmega ×1
avr ×1
avr-gcc ×1
c ×1
c++ ×1
compiler-bug ×1
concurrency ×1
dataframe ×1
macos ×1
memory ×1
namespaces ×1
r ×1
realitykit ×1
rounding ×1
ruby ×1
swiftui ×1
templates ×1
using ×1
visionos ×1