我目前正在比较Python3和C中的两个循环计算.对于Python,我有:
# Python3
t1 = time.process_time()
a = 100234555
b = 22333335
c = 341500
for i in range(1, 10000000001):
a = a - (b % 2)
b = b - (c % 2)
print("Sum is", a+b)
t2 = time.process_time()
print(t2-t1, "Seconds")
Run Code Online (Sandbox Code Playgroud)
然后在C中,我做同样的事情:
#include <stdio.h>
int main() {
long long a = 100234555;
long long b = 22333335;
long long c = 341500;
for(long long i = 1; i <= 10000000000; i++){
a = a - (b % 2);
b …
Run Code Online (Sandbox Code Playgroud) 我今天在我的CS课上参加了一个测验并得到了关于模运算符错误的问题,因为我不知道C中%的可用性,我一直在使用fmod()
.为什么两者都存在?是一个更好/更快还是只处理不同的数据类型?
我发现使用模运算符的一个非常奇怪的行为.
给出以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main()
{
uint8_t x = 2;
uint8_t i;
for(i=0; i<5; i++)
{
x = (x - 1) % 10;
printf("%d ", x);
}
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
我希望结果1 0 3 4 2
,但我得到了1 0 255 4 3
.
我认为它与整体推广有关,但我不明白转换是如何完成的.
有人在编程竞赛中提出这个问题: -
-1%1000000009是-1或1000000008
我想知道,这甚至可能吗?我试过我的系统,每次都得-1.另外,我必须找出10 ^ -9%10 ^ 9,我用fmod并得到答案1e-009,不应该是1吗?
我的解释: - 10 ^ -9/10 ^ 9 = 1/10 ^ 18所以,答案= 1.
请告诉我我错在哪里.
我知道对于无符号整数,如果除数是 2 的幂,我可以用位掩码替换模运算。对于浮点数,是否有任何数字具有类似的属性?也就是说,是否有任何数字n
可以f mod n
比一般情况更有效地计算,而不必使用位掩码?
当然,除了一个。 脑部衰竭
编辑:澄清一下,f
是任何浮点数(在运行时确定),
n
是任何格式的任何编译时常量,我希望结果是浮点数。
例如,假设您有以下变量:
int i = 9;
int j = 7;
Run Code Online (Sandbox Code Playgroud)
根据实现,,,的值(-i)/j
可以是–1
或–2
.怎么可能得到这两个不同的结果?
我想知道Javascript如何处理模数.例如,Javascript评估47%8?谢谢.我似乎无法找到任何文档,我的模数技能不是最好的...谢谢!
在这种情况下,百分比是指什么?
int myInt = 27 % 10;
myInt = 7;
Run Code Online (Sandbox Code Playgroud)
%
这段代码的意思是什么?
Let's say I have (-5) mod 8
.
I tried it in both languages Java and C, and they gave me a -5
result when I was expecting 3
.
Why is this happening? Can a modulus be negative? And what should I change to get the correct result?
Java code
public class Example {
public static void main(String[] args) {
int x;
x = -5%8;
System.out.println(x);
}
}
Run Code Online (Sandbox Code Playgroud)
C code
int main(){
int x;
x = -5%8;
printf("%d", x); …
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main (void)
{
int x = 10^2;
long a = 4000465006540123; //(16 places)
long b = 4000465006540123 % x;
printf("%li\n", b);
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时(它正确编译),代码打印出“3”。难道它不应该打印出“23”,因为 x 是 100,而不是 10?
我们有 3 个数字 y、x 和 n。我们被要求在 1 <= k <= n 且 k % x = y 的情况下找到最大的 k。例如:输入:1 2 100 输出:99
我可以写的是:
#include <stdio.h>
int main()
{
int y, x, n, max = 1;
scanf("%d %d %d", &y, &x, &n);
for (int k = 1; k <= n; k++)
{
if ((k % x == y) && (k >= max))
max = k;
}
printf("%d", max);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它完全正常工作。但问题是程序应该在不使用任何循环或 if 的情况下编写。有人有什么想法吗?
昨天我不得不面对面试,在那次面试中他们问了我一个问题:告诉我 % 和 / 之间的区别?
我知道他们给出的结果,但我无法从理论上解释它。请问哪位大神能给个理论上的解释一下,我也做过一个程序:
class FindDifference
{
public static void main(String[] args)
{
int num1=12;
int num2=3;
int modulus=num1%num2;
int divide=num1/num2;
System.out.println("modulus :"+modulus);
System.out.println("division :"+divide);
}
}
Run Code Online (Sandbox Code Playgroud)
我的结果是:
modulus :0
division :4
Run Code Online (Sandbox Code Playgroud)
但我怎么能给出它们之间的理论解释呢?请你帮帮我好吗?