小编Kar*_*eem的帖子

为什么我没有得到一个指向int的指针的警告?

下面的代码提供了一个警告,用于从指向int的指针进行强制转换

int foo[] = {1, 2, 3};
#define bar(x) foo[(int)(x) % 3]
char *baz = "quux";
bar(baz);
Run Code Online (Sandbox Code Playgroud)

另一方面,以下代码编译时没有任何警告(无论它是否可能导致运行时错误)

#include <ctype.h>
// some code
char *baz = "quux";
isalpha(baz);
Run Code Online (Sandbox Code Playgroud)

当我打开ctype.h来查看时isalpha,我发现它是一个使用其他几个宏的宏

#  define _ISbit(bit)   (1 << (bit))

enum
{
    // some identifiers
    _ISalpha = _ISbit (2),
    // some identifiers
};

extern const unsigned short int **__ctype_b_loc (void)
 __THROW __attribute__ ((__const__));

# define __isctype(c, type) \
((*__ctype_b_loc ())[(int) (c)] & (unsigned short int) type)

# define isalpha(c) __isctype((c), _ISalpha)
Run Code Online (Sandbox Code Playgroud)

正如你可能会看到,扩大的结果,isalpha …

c int pointers casting

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

八皇后算法

我之前问过一个关于使用Java解决八个皇后问题的问题.我有一个回溯算法来解决这个问题.

我尝试使用这个算法,但我不知道我的代码有什么问题.它最多只能放置7个皇后.

这是女王级:

    public class Queen {
    //Number of rows or columns
    public static final int BOARD_SIZE = 8;

    boolean[][] board;
    //Indicate an empty square
    public static final boolean EMPTY = false;
    //Indicate a square which containing a queen
    public static final boolean QUEEN = true;
    //Number of moves
    public static final int MOVES = 4;
    //Horizontal moves
    int[] horizontal;
    //Vertical moves
    int[] vertical;

    public int queens = 0;

    public Queen() {
        //Constructor creates an empty board
        board = new …
Run Code Online (Sandbox Code Playgroud)

java backtracking

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

INSERT ... ON DUPLICATE KEY UPDATE 究竟是如何原子的?

我做了一些搜索,但无法找到明确的答案。从插入、更新或抛出错误的意义上来说,它是原子的吗?如果是,这与使用 TRANSACTION 有什么不同吗?或者,如果我从自己的应用程序访问数据库,则使用某种条件以及某种查询函数/方法?

mysql atomic

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

八皇后启发式

我正在开发一种启发式方法,将8个皇后放在8x8棋盘上.每个方格都有自己的消除编号(表示如果在该方格中放置一个女王,则"消除"空棋盘的平方数.)并且每个女王应放置在具有最低消除编号的方格中.

我的问题是我不知道如何继续减少其适当方格的特定消除数量,所以如果你帮助我,我将感激不尽.另一个问题,我觉得我的代码非常复杂,所以任何笔记都会让它变得更简单?

这是我的代码

public class Queen {
private final int SIZE = 8;
private int board[][] = new int[SIZE][SIZE]; // 8x8 board
private int hor[] = new int[SIZE]; // horizontal moves
private int ver[] = new int[SIZE]; // vertical moves
private int lowestValue = 22;
private int[][] queens = new int[SIZE][2]; // 8 queens 

public Queen () {
    // initialize each square with its own elimination number
    for (int row = 0; row < board.length; row++) {
        for (int col …
Run Code Online (Sandbox Code Playgroud)

java artificial-intelligence heuristics n-queens

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

如何确定最大的斐波那契数

我被要求确定可以在我的系统上显示的最大的斐波纳契数,我想知道如何做到这一点..

这是我的简单应用程序,它决定了第n个斐波纳契数

import java.util.Scanner;

public class FibonacciTest
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.printf("please enter the nth fibonacci number: ");

        int n = input.nextInt();
        System.out.printf("%d\n", fibonacci(n));

    }// end main

    public static int fibonacci(int n)
    {
        // determines nth fibonacci number
        int fib = 1;
        int temp = 0;

        if (n == 1)
            return 0;

        else
        {
            for (int i = 2; i < n; i++)
            {
                int last = fib;
                fib += temp;
                temp …
Run Code Online (Sandbox Code Playgroud)

java fibonacci

4
推荐指数
2
解决办法
7309
查看次数

NumberFormatException:无限或NaN

我有一个方法,取n并返回第n个斐波纳契数.在方法实现中,我使用BigDecimal获取第n个Fibonacci数,然后我使用方法toBigInteger()将数字作为BigInteger对象获取,这肯定是因为我在我的应用程序中处理大量数字.

我一直得到正确的结果,直到我通过1475作为我的方法的参数.NumberFormatException: Infinite or NaN在没有任何明确理由的情况下,我遇到了这种情况.

你能解释一下我为什么会得到这个例外吗?

这是我的方法:

BigInteger getFib(int n){
     double phi = (1 + Math.sqrt(5))/2;
     double squareRoot = (Math.sqrt(5)) + (1/2);
     BigDecimal bd = new BigDecimal(Math.floor(Math.pow(phi, n)/(squareRoot)));
     return bd.toBigInteger();
}
Run Code Online (Sandbox Code Playgroud)

java fibonacci numberformatexception

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

当一个声明被称为单入/单出退出时,它不是?

我不确定我是否能很好地使用它,例如,Java中的if语句被称为单入口/单出口语句.在条件为真的情况下,这被认为是单入口点,如果错误则认为是单出口点?

if(someCondition)
   doSomething();
Run Code Online (Sandbox Code Playgroud)

什么是非(单项/单项退出)声明的例子?

java

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

使用FILE变量的地址和C中的FILE*有什么区别?

给出以下简短的例子

FILE *p = fopen("foo.txt", "r");
FILE f = *p;

int i;
fscanf(p, "%i", &i); // works just fine
fscanf(&f, "%i", &i); // segmentation fault
Run Code Online (Sandbox Code Playgroud)

我已经阅读了一些FILE,FILE *以及实际的结构类型_IO_FILE,但我不清楚在第二次调用中导致分段错误的原因是什么fscanf.

除了p并&f包含不同的地址之外,除非涉及到这一点(我认为是这样),在这种情况下&f和之间有什么区别p?

c file-io pointers

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

如果非最终字段的值可以更改,那么如何在匿名类类中使用它们?

我之前问过这个问题,但我得不到合适的答案.

如果非最终字段的值可以更改,那么如何在匿名类类中使用它们?

class Foo{
    private int i;
    void bar(){
        i = 10
        Runnable runnable = new Runnable (){
            public void run (){
                System.out.println(i); //works fine
            }//end method run
        }//end Runnable
    }//end method bar
}//end class Foo 
Run Code Online (Sandbox Code Playgroud)

如果在匿名类中使用的局部变量必须final使编译器能够在匿名类代码中内联它们的值,如下所示:

之前:

public class Access1 {
  public void f() {
    final int i = 3;
    Runnable runnable = new Runnable() {
        public void run() {
            System.out.println(i);
        }//end method run
    };//end anonymous class
  }//end method f
}//end class Access1
Run Code Online (Sandbox Code Playgroud)

后:

public class Access1 …
Run Code Online (Sandbox Code Playgroud)

java final field local-variables anonymous-class

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

为什么字节参数不被识别为整数?

我有以下枚举:

public enum Months {
    JAN(31),
    FEB(28),
    MAR(31),
    APR(30),
    MAY(31),
    JUN(30),
    JUL(31),
    AUG(31),
    SEP(30),
    OCT(31),
    NOV(30),
    DEC(31);

    private final byte DAYS; //days in the month

    private Months(byte numberOfDays){
        this.DAYS = numberOfDays;
    }//end constructor

    public byte getDays(){
        return this.Days;
    }//end method getDays
}//end enum Months
Run Code Online (Sandbox Code Playgroud)

它给了我一个错误,说"构造函数Months(int)是未定义的",虽然我传递了一个有效的字节参数.我究竟做错了什么?

java enums byte constructor arguments

0
推荐指数
2
解决办法
87
查看次数