这是我在谈论的问题 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) 我正在研究一个需要多维数字的程序.长话短说,我需要将数字与字符串进行比较,所以当转换为字符串时,我需要摆脱double给出的小数位.为此,我使用Math.round
并保存为long
.这适用于相对正常的数字,但数字可以达到999,999.
我使用275393(给定的测试编号,所以我认为它必须对我正在处理的问题是正确的)并且计算器和计算机似乎都没有得到正确的答案.正确答案应该在结果的某处包含123457,但计算器有12346(我认为它只是四舍五入,因为它在此之后停止列出数字)并且计算机有123456(计算机在此点之后停止列出数字).是四舍五入给它的问题(它不应该因为我很确定它只会到十分之一,但谁知道)?或者是别的什么?
我刚开始使用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) 我正在为游戏创建一个保存游戏文件,但我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
但它不起作用.
我在这里有一段代码:
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()
得到它的价值,来自哪里?
我需要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 …
如何连接到列表中的最后一个元素?
List<String> x = {cow, cat, dog}; //explanation purpose
Run Code Online (Sandbox Code Playgroud)
如果if语句被触发,我想将"dog"连接成"dog".
我收到“无法编译的源代码 - 错误的树类型”错误和“找不到符号”错误我关闭了保存时编译,现在找不到符号。似乎破坏它的区域是我在 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) 我正在尝试在我的代码中使用 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)
这给了我一条错误消息:
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
我认为它告诉我 counter_iv …
我有这个通用功能:
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) 试图破译这段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) 我正在解决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 ×7
python ×2
c ×1
c++ ×1
computation ×1
conditional ×1
cryptography ×1
exception ×1
hash ×1
io ×1
javascript ×1
list ×1
printwriter ×1
random ×1
type-hinting ×1
while-loop ×1