小编tad*_*man的帖子

C++ uint64_t按位并检查偶数

我有以下代码,只是检查uint64_t是否是偶数,我打算使用按位AND操作来检查,但它似乎没有工作.

这是我认为首先工作的代码:

int n;
scanf("%d",&n);
for(int i = 0; i < n; i++){
    uint64_t s,d;
    scanf("%llu %llu",&s,&d);
    //try for x
    uint64_t x;
    bool stop = false;
    x = s + d;
    printf("%llu",x&1ULL); \\ This prints 0 when the number is even but
    if(x&1ULL==0ULL){ \\ This check always returns false
        printf("%llu",x);
        x/= 2;
Run Code Online (Sandbox Code Playgroud)

如果数字是奇数或偶数,则此代码始终打印输出0或1,但if语句始终返回false.我究竟做错了什么?谢谢

c bit-manipulation bitwise-operators uint64 bitwise-and

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

如何拒绝或仅允许Hash中的某些键?

我有这个方法试图从每个hashie :: mash对象中选择某个字段(每个图像是一个hashie :: mash对象)但不是全部.

  def images
        images = object.story.get_spree_product.master.images
        images.map do |image|
          {
            position: image["position"],
            attachment_file_name: image["attachment_file_name"],
            attachment_content_type: image["attachment_content_type"],
            type: image["type"],
            attachment_width: image["attachment_width"],
            attachment_height: image["attachment_height"],
            attachment_updated_at: image["attachment_updated_at"],
            mini_url: image["mini_url"],
            small_url: image["small_url"],
            product_url: image["product_url"],
            large_url: image["large_url"],
            xlarge_url: image["xlarge_url"]
          }
        end
      end
Run Code Online (Sandbox Code Playgroud)

有更简单的方法吗?

images是一个hashie :: mash对象的数组.

object.story.get_spree_product.master.images.first.class
Hashie::Mash < Hashie::Hash
[15] pry(#<Api::V20150315::RecipeToolSerializer>)> object.story.get_spree_product.master.images.count
2
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails spree

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

从Ruby中的数组中删除字符串

假设我有一个看起来像这样的数组:

arr = [ 1, 5, "dog", 6 ]
Run Code Online (Sandbox Code Playgroud)

如何删除字符串,以便我的输出如下所示:

[ 1, 5, 6 ]
Run Code Online (Sandbox Code Playgroud)

ruby

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

如何在c中对数组中的日期进行排序

我正在尝试从数组中排序日期我有以下代码(不包括我正在尝试阅读的数组和文件,另一个包含我正在尝试编写的已排序日期.

int aniomayor=tot[0].anio;
int diamayor=tot[0].dia;
int mesmayor=tot[0].mes;

while (i<nf) {
  if (tot[i].anio > aniomayor) {
    int aniomayor=tot[i].anio;
    int diamayor=tot[i].dia;
    int mesmayor=tot[i].mes;
  }
  else if (tot[i].anio == aniomayor && tot[i].mes > mesmayor) {
    int aniomayor=tot[i].anio;
    int diamayor=tot[i].dia;
    int mesmayor=tot[i].mes;
  }
  else if (tot[i].anio == aniomayor && tot[i].mes == mesmayor &&  tot[i].dia > diamayor) {
    int aniomayor=tot[i].anio;
    int diamayor=tot[i].dia;
    int mesmayor=tot[i].mes;
  }

  i++;
}

fprintf(f, "%s ", diamayor);
fprintf(f, "%s ", mesmayor);
fprintf(f, "%s \n", aniomayor);
Run Code Online (Sandbox Code Playgroud)

我认为它会起作用,但在2,3,4 ..行中它将始终打印相同的日期,我不知道该怎么做才能忽略已经排序的日期.提前致谢.

c

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

SockMerchant挑战Ruby数组#lount算不算?

所以,我正在对HackerHank做一个初学者的挑战,而且红宝石的奇怪行为令人难以置信.

挑战在于:查找并计算阵列中有多少对.(袜子对)

这是我的代码.

n = 100
ar = %w(50 49 38 49 78 36 25 96 10 67 78 58 98 8 53 1 4 7 29 6 59 93 74 3 67 47 12 85 84 40 81 85 89 70 33 66 6 9 13 67 75 42 24 73 49 28 25 5 86 53 10 44 45 35 47 11 81 10 47 16 49 79 52 89 100 36 6 57 96 18 23 71 …
Run Code Online (Sandbox Code Playgroud)

ruby arrays sorting counting

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

何时使用 #define 与 typedef?

所以,我一直在努力减少using namespace std,因为我现在有点明白它会在代码中引起什么冲突,即使我不会很快使用别人的代码,我想训练自己使用良好的做法...

但我仍然不喜欢当你在代码上撒上作用域运算符时代码的样子,我遇到的一个建议是使用typedefs自定义外观,我写的是:

#include <iostream>
#include <fstream>
#include <string>

typedef std::cout std_cout;
typedef std::endl std_endl;
typedef std::string std_string;
typedef std::fstream std_fstream;
typedef std::ios std_ios;
Run Code Online (Sandbox Code Playgroud)

现在,当我输入这个时,一切都很好,除了前两行(在第二个代码块中),它们分别给了我variable "std::cout" is not a type namefunction template "std::endl" is not a type name错误消息。

现在,如果我typedef#defines替换所有s ,问题最初似乎已经解决了,但是我最终会在包含这些定义之一的每一行中出现大量语法错误。(此外我知道这#define也同样有问题,using namespace std所以我也试图避免那个)

更复杂的是,第一行(在同一块中)是这里两条有问题的行之一,与我在网上的众多来源中看到的完全相同,所以我不知道是什么对了……

如何正确定义数据类型?

提前致谢。

c++ typedef namespaces

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

程序不会打印 'sayHi()' 函数

我收到此错误,即未在此范围内声明“sayHi”。我不明白为什么它不起作用,尤其是当我从 C 教程中复制代码时。

#include <stdio.h>

int main()
{
    sayHi();
    return 0;
}

void sayHi()
{
    printf("You made it ha");
    
}
Run Code Online (Sandbox Code Playgroud)

c

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

if(x % y == 0) 导致我的程序不输出任何内容?

目前正在研究 euler 项目的第三个问题,并偶然发现了一个奇怪的问题,这让我用头撞墙试图弄清楚。我将问题隔离到这一小部分代码。

#include <iostream>


int main()
{
    long max = 13195;
    long largestPrimeNumber = 0;

    for(int i = 0; i < max; i++)
    {
        if(max % i == 0)
        {
            std::cout << "test\n";
        }
        else
        {
            std::cout << "other test\n";
        }
    }
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个小测试程序的最终目标是为每个非素数输出“测试”,为每个小于 13195 的素数输出“另一个测试”,如下所示:

test
test
test
test
another test
test
test
another test
Run Code Online (Sandbox Code Playgroud)

我尝试过将其放入函数并在 for 循环中运行该函数的方法。它工作正常,直到我添加条件(max % i == 0);。关于它的一些东西似乎弄乱了代码,但我不知道是什么。

c++

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

数组中多个对象的总和值

这就是我所拥有的:

const arrayA = [{name:'a', amount: 10, serviceId: '23a', test:'SUCCESS'},
                {name:'a', amount: 9, test:'FAIL'}, 
                {name:'b', amount: 15, serviceId: '23b', test:'SUCCESS'}]

 
Run Code Online (Sandbox Code Playgroud)

(请注意,有些对象没有“serviceId”)

我想得到:

 [{name:'a', amount: 19, test:'FAIL'},
  {name:'b', amount: 15, test:'SUCCESS'}]
Run Code Online (Sandbox Code Playgroud)
  • 我想对按名称分组的字段求和amount
  • “测试”字段应该是FAIL是否有任何具有该值的对象FAIL(按名称分组)
  • 我不关心 的值serviceId,也不想将其包含在新对象中。

我已经搜索并尝试了这样的事情:(参考:/sf/answers/3523685231/

const result = Object.values(arrayA.reduce((r, o) => (r[o.name]
  ? (r[o.name].amount += o.amount)
  : (r[o.name] = {...o}), r), {}));
Run Code Online (Sandbox Code Playgroud)

但仍然不确定如何分配test字段。

任何帮助,将不胜感激!

javascript reduce typescript

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

Rack::Chunked 已弃用,并将在 Rack 3.1 中删除

当我启动 Rails 应用程序时,我得到:

/Users/dorianmariefr/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/rack-3.0.7/lib/rack/chunked.rb:6:警告:Rack::Chunked已弃用并将在 Rack 3.1 中删除

不知道如何解决这个警告,有什么想法吗?

ruby rake ruby-on-rails

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