我正在尝试用C++编写一个非常简单的程序,它找到两个数字的模数,如下所示:
#include <iostream>
using namespace std;
int n;
int d;
int modulus;
int main()
{
cout<<"***Welcome to the MODULUS calculator***";
cout<<"Enter the numerator, then press ENTER: ";
cin>>n;
cout<<"Enter the denominator, then press ENTER: ";
cin>>d;
modulus=n%d;
cout<<"The modulus is ---> "<<modulus;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试编译它时,我得到以下内容:

怎么解决这个问题?
谢谢.
我发现有两种方法可以从c ++中的一个分区得到一个整数
问题是哪种方式更有效(更快)
第一种方式:
Quotient = value1 / value2; // normal division haveing splitted number
floor(Quotient); // rounding the number down to the first integer
Run Code Online (Sandbox Code Playgroud)
第二种方式:
Rest = value1 % value2; // getting the Rest with modulus % operator
Quotient = (value1-Rest) / value2; // substracting the Rest so the division will match
Run Code Online (Sandbox Code Playgroud)
还请说明如何找出哪种方法更快
在Java中,我想执行102%100并将余数设为02,而不是2。我该怎么做?
整数a =(102%100);
a返回2而不是02 ..我想得到02 ..因为最初应该是1.02,但是mod返回2。:(有人知道怎么做吗?
我正在进行旧测试以进行测试,此代码为longVariable打印10的值.现在,手工,对我来说,数学将是9 + 1%10 = 0的余数,而不是10 ......我怎么错了?
谢谢你的帮忙!
public class ExamSectionA
{
public static void main(String[] args)
{
int intVariable1 = 9;
int intVariable2 = 10;
double doubleVariable = 11.2;
char charVariable = 'A';
long longVariable;
longVariable = intVariable1 + 1 % intVariable2;
intVariable2 = (int) (doubleVariable / 10f);
String[] theirSalary = {"10","20","30","40"};
System.out.println(intVariable2);
System.out.println(longVariable);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:PEMDAS.以为我明白了.
我一直在通过Kernighan和Ritchie"The C Programming Language"进行处理并阅读x mod y == x & y-1.所以,我用铅笔和纸做了它,工作得很好,所以我试着测试它,这就是问题所在:
码:
#include <stdio.h>
main()
{
int i, j;
for(i = 1; i < 10; i++){
for(j = 1; j < 10; j++)
printf("%3d",i & j-1);
printf("\n");
}
}
Run Code Online (Sandbox Code Playgroud)
给出输出:
0 1 0 1 0 1 0 1 0
0 0 2 2 0 0 2 2 0
0 1 2 3 0 1 2 3 0
0 0 0 0 4 4 4 4 0
0 1 0 …Run Code Online (Sandbox Code Playgroud) 我想%在Java中包含模数的等式中得到未知数的值
例如:
x % 26 = y如果我有价值y我怎么能得到x
假设我尝试执行以下操作:
y = 0;
z = x % y;
Run Code Online (Sandbox Code Playgroud)
是这个明确定义,平台相关或未定义的语义?我主要是关于C/C++的问题,但我对各种编程/脚本语言(Java,perl,sh等)的答案感兴趣
我需要一些帮助来计算:
10**7342345.54334 % m
Run Code Online (Sandbox Code Playgroud)
我在python中计算它,但结果太大了
pow(10,7342345.54334,m)
Run Code Online (Sandbox Code Playgroud)
无法使用,因为所有参数必须是整数.如果我将此十进制值转换为整数,则答案会更改.
int doEveryTwoTimes= 1; // Counter to do something every two loops
int doEveryFourTimes= 2; // Counter to do something every four loops
// add a nested infinite loop to increment counter
while(true){
if(doEveryTwoTimes%2 == 0){
// DO STUFF EVERY **TWO** LOOPS
}
if(?????){
// DO STUFF EVERY **FOUR** LOOPS
}
doEveryTwoTimes++;
doEveryFourTimes++;
}
Run Code Online (Sandbox Code Playgroud)
我可以添加一个条件来使事情每两个循环发生一次,但是如何为每个第四个循环创建一个条件?
我正在尝试将列表分成尽可能大的子列表。如果列表不能以这种方式划分,我将根据需要进行处理,但是我需要获得除N本身以外的最大数目,该数目将N均分。
我写了一个非常幼稚的解决方案,但是我觉得应该有一个公式或某种东西可以在恒定时间内做到这一点。我的列表不是很大,最大大小为1000。这可能不是关键路径,但是有更好的算法吗?
public static int largestDivisor(int n){
int divisor = 0;
for (int i = 1; i <= n/2; i++)
if (n % i == 0)
divisor = i;
return divisor;
}
Run Code Online (Sandbox Code Playgroud)