我最近在课堂上做了一个测试。问题之一是:
给定一个数字n,用C / C ++编写一个函数,该函数返回数字平方的数字总和。(以下内容很重要)。的范围的Ñ为[ - (10 ^ 7),10 ^ 7]。示例:如果n = 123,则您的函数应返回14(1 ^ 2 + 2 ^ 2 + 3 ^ 2 = 14)。
这是我写的函数:
int sum_of_digits_squared(int n)
{
int s = 0, c;
while (n) {
c = n % 10;
s += (c * c);
n /= 10;
}
return s;
}
Run Code Online (Sandbox Code Playgroud)
看着我正确。所以现在测试又回来了,我发现老师由于我不明白的原因没有给我所有的分数。据他说,为了使我的功能更完整,我应该添加以下细节:
int sum_of_digits_squared(int n)
{
int s = 0, c;
if (n == 0) { // …Run Code Online (Sandbox Code Playgroud) 在ac程序中我正在尝试以下操作(只是检查行为)
x = 5 % (-3);
y = (-5) % (3);
z = (-5) % (-3);
printf("%d ,%d ,%d", x, y, z);
Run Code Online (Sandbox Code Playgroud)
给我输出为(2, -2 , -2)gcc.我每次都期待一个积极的结果.模数可以为负数吗?任何人都可以解释这种行为吗?
数学:
如果你有这样的等式:
x = 3 mod 7
Run Code Online (Sandbox Code Playgroud)
x可以是...... -4,3,10,17 ......,或更一般地:
x = 3 + k * 7
Run Code Online (Sandbox Code Playgroud)
其中k可以是任何整数.我不知道为数学定义了模运算,但因子环肯定是.
Python:
在Python中,当您使用%正数时,您将始终获得非负值m:
#!/usr/bin/python
# -*- coding: utf-8 -*-
m = 7
for i in xrange(-8, 10 + 1):
print(i % 7)
Run Code Online (Sandbox Code Playgroud)
结果是:
6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3
Run Code Online (Sandbox Code Playgroud)
C++:
#include <iostream>
using namespace std;
int main(){
int m = 7;
for(int i=-8; i …Run Code Online (Sandbox Code Playgroud) 假设我需要格式化数组的输出以在每行显示固定数量的元素.我如何使用模数运算来做到这一点?
使用C++,下面的代码适用于每行显示6个元素,但我不知道它是如何工作的?
for ( count = 0 ; count < size ; count++)
{
cout << somearray[count];
if( count % 6 == 5) cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
如果我想每行显示5个元素怎么办?我如何找到所需的确切表达式?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> v = {1, 2, 3, 4, 5, 6, 7};
int i = -4;
cout << i << endl;
cout << v.size() << endl;
cout << i % v.size() << endl;
cout << -4 % 7 << endl;
}
Run Code Online (Sandbox Code Playgroud)
The above code prints:
-4
7
5
-4
Run Code Online (Sandbox Code Playgroud)
Can someone please explain why i % v.size() prints 5 instead of -4? I'm guessing it has something to do …
我正在编写一个用c ++保存日期的类,我发现了以下问题:
我N自参考日期(在我的情况下将是公元0001年1月1 日)以来有多天,包括自参考日以来经过的闰日.我怎么能转换这个数字到一年Y,月M和日D有效?
我想尽可能高效地完成这项工作,因此最佳实现显然会具有O(1)复杂性.
接下来的部分将解释我已经学到的一些东西.
要确定一年是否跳跃,有一些规则:
这将转换为这样的代码:
bool IsLeapYear(int year)
{
// Corrected after Henrick's suggestion
if (year % 400 == 0) return true;
if ((year % 4 == 0) && (year % 100 != 0)) return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
计算一年前飞跃多少年的有效方法是:
int LeapDaysBefore(int year)
{
// Years divisible by 4, not divisible by 100, but divisible by 400
return ((year-1)/4 - (year-1)/100 + …Run Code Online (Sandbox Code Playgroud) 对于给定的数字x,y并且n,我想计算x-y mod n在C.在这个例子来看一下:
int substract_modulu(int x, int y, int n)
{
return (x-y) % n;
}
Run Code Online (Sandbox Code Playgroud)
只要x>y,我们没事.然而,在另一种情况下,modulu操作是未定义的.
你可以想到x,y,n>0.我希望结果是正的,所以如果(x-y)<0,那么((x-y)-substract_modulu(x,y,n))/ n应该是一个整数.
您知道的最快算法是什么?有没有避免任何电话if和operator??
我很想理解mod操作背后的逻辑,因为我知道可以执行位移操作来执行不同的操作,例如位移到乘法.
我可以看到它完成的一种方法是通过递归算法继续划分,直到你不能再分裂,但这似乎并不高效.
任何想法都会有所帮助.提前致谢!
鉴于此示例:
std::vector<int> numbers = {5,6,7}; //size is 3
int i = -1;
std::cout << i % 3 <<"\n"; // output: -1
std::cout << i % numbers.size() << "\n"; // output: 0
Run Code Online (Sandbox Code Playgroud)
基本上在两个语句中我处理-1%3但编译器输出不同的数字.我不明白这个结果,也许有人可以向我解释.
编辑: @ Chris,@ Keith Thompson @AnT建议片段
std::cout << std::numeric_limits<std::size_t>::max() % 3 <<"\n"; //output: 0
std::cout << i % numbers.size() << "\n"; // output: 0
Run Code Online (Sandbox Code Playgroud)
打印预期的输出.感谢大家的有益建议!
在回答另一个问题时,提供了以下解决方案,由OpenBSD提供,为了简洁起见,
uint32_t foo( uint32_t limit ) {
uint32_t min = -limit % limit, r = 0;
for(;;) {
r = random_function();
if ( r >= min ) break;
}
return r % limit;
}
Run Code Online (Sandbox Code Playgroud)
这条线是如何uint32_t min = -limit % limit工作的?我想知道的是,是否有数学证据证明它确实为随机数计算了一些下限并充分消除了模偏差?