小编Nay*_*uki的帖子

Squaring的指数(项目Euler 99)关于我的解决方案的提示

这是我在谈论的问题 http://projecteuler.net/index.php?section=problems&id=99

我的代码将编译并正确运行.我猜测计算是搞乱的地方.它告诉我,行号633是最大的(欧拉表示的项目不正确).

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

using namespace std;

int poww(int base, int exp);
int main()
{
    //ignore messy/unused variables. I am desperate 
    int lineNumber = 0;
    string line; 
    int answerLine = 0;
    int max =0;
    int lineNum = 0;
    int answer =0;
    ifstream inFile;
    size_t location;
    string temp1,temp2;
    int tempMax = 0;
    int base,exp = 0;
    inFile.open("C:\\Users\\myYser\\Desktop\\base_exp.txt");
    while(getline(inFile,line))
    {
        lineNumber++;
        location = line.find(",");
        temp1 = line.substr(0,(int(location)));
        temp2 = line.substr((int(location)+1),line.length());
        //cout << temp1 << " " << …
Run Code Online (Sandbox Code Playgroud)

c++ computation

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

Java Math.pow问题

我正在研究一个需要多维数字的程序.长话短说,我需要将数字与字符串进行比较,所以当转换为字符串时,我需要摆脱double给出的小数位.为此,我使用Math.round并保存为long.这适用于相对正常的数字,但数字可以达到999,999.

我使用275393(给定的测试编号,所以我认为它必须对我正在处理的问题是正确的)并且计算器和计算机似乎都没有得到正确的答案.正确答案应该在结果的某处包含123457,但计算器有12346(我认为它只是四舍五入,因为它在此之后停止列出数字)并且计算机有123456(计算机在此点之后停止列出数字).是四舍五入给它的问题(它不应该因为我很确定它只会到十分之一,但谁知道)?或者是别的什么?

java floating-point floating-accuracy

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

初学者试图编写一个简单的计算器

我刚开始使用Java并试图编写一个简单的计算器:

我尝试了以下方法:

    double x, y; //inputs
    Scanner sc = new Scanner(System.in);
    System.out.println("x?");
    x = sc.nextDouble();
    System.out.println("y?");
    y = sc.nextDouble();

    double answer = 0;

    System.out.println("Operation type: (+,-,*,/)");


    String opStr= sc.nextLine();
    char op = opStr.charAt(0);


    switch(op)
    {
        case '+':
            answer=x+y;
            break;

        case '-':
            answer=x-y;
            break;

        case '*':
            answer=x*y;
            break;

        case '/':

            if (y==0)
            {
                System.out.println("Error, can't divide by zero");
            }
            else
            {
                answer=x/y;
            }

            break;
        default: System.out.println("Unkown operation");
    }  // switch end

    System.out.println("answer = " +x+op+y+"=" + answer);
Run Code Online (Sandbox Code Playgroud)

当我尝试运行它时,我得到以下内容:(我可以输入x和y,但之后我收到错误消息.

Operation type: (+,-,*,/) …
Run Code Online (Sandbox Code Playgroud)

java exception

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

无法使PrintWriter工作

我正在为游戏创建一个保存游戏文件,但我PrintWriter只是覆盖了文件而不是我需要的数字.

这是代码:

public void fileWriter() throws FileNotFoundException {
    PrintWriter print = new PrintWriter(new File("C:\\Users\\john\\Desktop\\stats.txt"));
    print.print(String.valueOf(this.swordNumber) + " ");
    print.print(String.valueOf(this.shieldNumber) + " ");
    print.print(String.valueOf(this.monstersDefeated) + " ");
    print.print(String.valueOf(this.damageDealt));
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了一切来打印这些变量,String.valueOf但它不起作用.

java io printwriter

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

为什么要使用Math.random()及其功能

我在这里有一段代码:

if (Math.random() < 0.80) {
    var img = $('#img');
}

$(document).mousemove(function(event) {
    var mouse_x = event.pageX;
    var mouse_y = event.pageY;
    $(img).css({
        'top': mouse_y+'px', 
        'left': mouse_x+'px',
        'display' : 'block',
        'position' : 'absolute'
    }); 
});
Run Code Online (Sandbox Code Playgroud)

在这个脚本中,我不明白该if (Math.random() < 0.80)行在做什么.又是如何Math.random()得到它的价值,来自哪里?

javascript random

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

SHA-256相同的字符串返回不同的byte []?

我需要SHA-256作为AES-256的密钥.但我的示例SHA-256是:

MessageDigest messageDigest;
messageDigest = MessageDigest.getInstance("SHA-256");

String input = new String("ALIBABA");
messageDigest.update(input.getBytes(Charset.forName("UTF-8")));
byte[] hash = messageDigest.digest();
String hash1s = new String(hash,StandardCharsets.UTF_8);
System.out.println("HASH 1 is "+hash1s);
System.out.println("HASH 1 is "+hash);

String input2 = new String("ALIBABA");
messageDigest.update(input2.getBytes(Charset.forName("UTF-8")));
byte[] hash2 = messageDigest.digest();
String hash2s = new String(hash2,StandardCharsets.UTF_8);
System.out.println("HASH 2 is "+hash2s);
System.out.println("HASH 2 is "+hash2);
Run Code Online (Sandbox Code Playgroud)

返回不是相同的值byte []:

HASH 1是V% % P 9 P v// e\BF} $]

HASH 1是[B @ 629f0666

HASH 2是V% % P 9 P v// e\BF} $]

HASH 2是[B @ 1bc6a36e …

java hash

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

连接到java中列表中的最后一个元素

如何连接到列表中的最后一个元素?

List<String> x = {cow, cat, dog}; //explanation purpose
Run Code Online (Sandbox Code Playgroud)

如果if语句被触发,我想将"dog"连接成"dog".

java list concatenation

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

无法编译的源代码 - 错误的树类型错误和找不到符号错误

我收到“无法编译的源代码 - 错误的树类型”错误和“找不到符号”错误我关闭了保存时编译,现在找不到符号。似乎破坏它的区域是我在 AsteroidFields 生成方法中初始化 Asteroid() 的地方,所以我觉得我的初始化不正确,但我无法弄清楚如何实现。

package asteroidfield;
import java.util.TreeSet;
import blobzx.BlobGUI;
import blobzx.SandBox;
import blobzx.SandBoxMode;



public class AsteroidField implements BlobGUI {

    SandBox ast;

    public static void main (String [] Args){
        new AsteroidField();       
    }

    public AsteroidField (){
        ast = new SandBox();

        ast.setSandBoxMode(SandBoxMode.FLOW);   
        ast.setFrameRate(15);
        ast.init(this);

    }

    @Override
    public void generate() {
       // This is the line that is breaking the code.   
         Asteroid asteroid = new Asteroid();

    }

}








    package AsteroidField;

    import blobzx.BlobUtils;
    import blobzx.PolyBlob;
    import java.awt.Point;


    import java.util.Random;


    public class …
Run Code Online (Sandbox Code Playgroud)

java

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

Python 将整数转换为 16 字节字节

我正在尝试在我的代码中使用 AES-CTR-128。我使用Python 2.7并利用该cryptography模块进行加密。

我需要设置特定的计数器值,例如 counter_iv = 112。当我尝试时

import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
backend = default_backend()
key = os.urandom(32)
counter_iv = 112
cipher = Cipher(algorithms.AES(key), modes.CTR(counter_iv), backend=backend)
encryptor = cipher.encryptor()
ciphertext = encryptor.update(b"a secret message") + encryptor.finalize()
Run Code Online (Sandbox Code Playgroud)

这给了我一条错误消息:

Traceback (most recent call last):
File "aestest.py", line 14, in <module>
  cipher = Cipher(algorithms.AES(key), modes.CTR(counter_iv), backend=backend)
File "/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/ciphers/modes.py", line 139, in __init__
  raise TypeError("nonce must be bytes")
TypeError: nonce must be bytes
Run Code Online (Sandbox Code Playgroud)

我认为它告诉我 counter_iv …

python cryptography type-conversion data-conversion

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

切片的 Python 类型注释?

我有这个通用功能:

def slice(seq):
    return seq[1:3]

assert slice("abcde") == "bc"
assert slice(b"xyz") == b"yz"
assert slice([2,7,1,8]) == [7,1]
Run Code Online (Sandbox Code Playgroud)

我们可以看到slice有类型str -> str, bytes -> bytes, List[int] -> List[int]

如何为函数编写类型注释?这是一个错误的尝试(TypeVar文档):

from typing import *

E = TypeVar("E")
T = TypeVar("T", bytes, str, Sequence[E])

def slice(seq: T) -> T:
    return seq[1:3]
Run Code Online (Sandbox Code Playgroud)

它产生以下错误消息:

error: Type variable "mymodule.E" is unbound
note: (Hint: Use "Generic[E]" or "Protocol[E]" base class to bind "E" inside a class
note: (Hint: Use …
Run Code Online (Sandbox Code Playgroud)

python type-hinting

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

当a是数字时,while(!a)是什么意思?

试图破译这段C代码:

int WaitForPacket(uint16 milliseconds, Dexcom_packet* pkt, uint8 channel) {
uint32 start = getMs();
uint8 * packet = 0;
uint32 i = 0;
uint32 seven_minutes = 420000;
int nRet = 0;
swap_channel(nChannels[channel], fOffset[channel]);

while (!milliseconds || (getMs() - start) < milliseconds) {
    i++;
    if(!(i % 60000)) {
        strobe_radio(channel);
    }
    doServices();
    if((getMs() - start) > seven_minutes) {
        killWithWatchdog();
        delayMs(2000);
    }
    blink_yellow_led();
    if (packet = radioQueueRxCurrentPacket()) {
        uint8 len = packet[0];
        fOffset[channel] += FREQEST;
        memcpy(pkt, packet, min8(len+2, sizeof(Dexcom_packet)));
        if(radioCrcPassed()) {
            if(pkt->src_addr == dex_tx_id …
Run Code Online (Sandbox Code Playgroud)

c conditional while-loop

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

对于参数类型int,boolean,运算符^未定义

我正在解决Hackerrank问题'最大化xor'.(https://www.hackerrank.com/challenges/maximizing-xor)

我使用'if'语句来检查i xor j是否大于'max',如代码所示.

static int maxXor(int l, int r) {
    int max=0;
    for(int i=l;i<r;i++)
        for(int j=l;j<r;j++)
        {
            if(i^j>max)/*error part*/
            max=i^j;
        }
    return max;
}
Run Code Online (Sandbox Code Playgroud)

但为什么我会收到此错误?

对于参数类型int,boolean',运算符^未定义

java operator-precedence boolean-operations

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