其他输入似乎可以很好地找到结果,我对此很满意,但当我输入15和5时,我得到的结果是0而不是5,为什么会发生这种情况?
从我能想到的逻辑来看,使用注释来跟随keep track,它应该工作得很好,但事实并非如此。
#include <stdio.h>
#include <math.h>
int m;
int n;
int GCD(int m, int n);
int main(void)
{
scanf("%d %d", &m, &n);
printf("M = %d, N = %d", m, n);
我已经写了一个分数类,在简化方面遇到了麻烦。
当我创建分数对象时,一切都很好,我只是认为我的逻辑与简化混乱。
(num和den分别是类中分子和分母的私有变量)
下面是我的GCD和Simplify方法:
/**
* Returns the absolute value of the greatest common divisor of this
* fraction's numerator and denominator. If the numerator or denominator is
* zero, this method returns 0. This method al
“两个整数的最大公约数是将两个数字中的每一个均等分的最大整数。编写返回两个整数的最大公约数的方法Gcd。将该方法合并到从用户读取两个值并显示结果的应用程序中。”
(这不是家庭作业,只是我正在使用的书中的练习)
你能帮我解决这个问题吗?这是我到目前为止所得到的。
(编辑-我可以提交这两个数字,但它不会为我计算Gcd )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Greatest_Comm
我已经运行了下面的代码,我认为它是正确的。然而,它只是不断地返回堆栈溢出。当我在调试模式下运行它时,我注意到函数x%y以某种方式返回y,而不是应该为0的余数。有没有人能帮帮忙看看为什么会这样?
public class test
{
public static void main (String [] args)
{
System.out.println(gcd(50,10));
}
static double gcd(double x, double y)
{
if (x > y)
{
大家好,我正在尝试创建一个接受两个数字的LCM函数。这段代码中的findCommonMultiple()函数基本上返回一个表示该数字的质因数的数组。我在这个函数中尝试做的是检查两个数组中是否有重复项,如果有,则将该数字推入一个新数组中。在推送一个数字之后,内部循环应该会中断,并继续进行下一次迭代。如果这两个数字不相等,它们都将被推送。即使其中一个数组超过了它们的索引,这种情况也会发生。在推送了所有重复因子和唯一因子之后,我将开始将它们相乘,并返回这两个数字的LCM。我还没有为此创建一个助手函数,但我需要先解决这个问题。
function leastCommonMultiple(num1,
我有下面的函数,它可以找到2个整数的最大公约数。我不明白在返回greatestCommonDivisor(b,(a%b));部分中发生了什么。
如果我做greatestCommonDivisor (8,12),我得到4,这是正确的,但是当我试图计算返回的greatestCommonDivisor(b,(a%b))时;第一部分得到(12,(8% 12)),它简化为(12,0),这是如何等于4的?
// Finds greatest common divisor
function greatestCommonDivisor(a, b) {
if (b == 0) {
ret
我是ios编程的新手。我有一个关于GCD项目的问题。
01 // This program finds the greatest common divisor of two nonnegative integer values
02
03 #import <Foundation/Foundation.h>
04
05 int main (int argc, const char * argv[]) {
06 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
07 unsigned int u
我有一个关于python优先级的问题。我有以下代码:
def gcdIter(a, b):
ans = min(a,b)
while ((a%ans is not 0) and (b%ans is not 0)):
ans -= 1
return ans
我的问题是关于while逻辑语句。我添加了几个括号,以确保表达式的计算方式与我的想法相同,但事实并非如此。while循环在两个表达式为真之前就被破坏了。我说错了吗?
我找到了一种不用两个表达式就能做同样事情的方法,在下面的代码中:
def gcdIter(a, b):
ans = min(a,b)
我知道这是一个经典的面试问题,但下面是我创建一个函数的快速尝试,该函数返回两个数字的最小公倍数,这是我在日常工作中从不需要做的事情:
def calc_common_multiplyer(int_low, int_high)
i = 1
int_high_res = []
while true
int_high_res << int_high * i
if int_high_res.include?(int_low * i)
return int_low * i
end
i = i+1
end
end
我觉得这很笨拙。
这是我编写的一些代码,用于搜索非常干净(无噪声)信号的峰值,其中fun是一个包含正弦波均匀采样数据的数组。
J=[fun(1)];
K=[1];
count=1;
for i=2:1.0:(length(fun)-2)
if fun(i-1)<fun(i) && fun(i)>fun(i+1)
J=[J,fun(i+1)];
K=[K,count+1];
end
count=count+1;
end
下面包含的是我试图处理的数据。
代码在664和991处找到了峰值,但没有介于两者之间的峰值。我用c++编写
我试图从Euler项目中解决一个问题: 13195的素因子是5,7,13和29。数字600851475143中最大的素因子是什么?以下是我的尝试:
def getPrimeNumbers(testNumber):
'''
For a given number it returns a list of all prime numbers from 2 to given number.
>>> getPrimeNumbers(15)
[2,3,5,7,11,13]
'''
我不能解决下面的问题。你能帮我解决这个问题吗?
You are given two jugs with capacities jug1Capacity and jug2Capacity liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly targetCapacity liters using these two jugs.
If targetCapacity liters of water are measurabl
我想在C中找到给定数组中最大元素的索引。
我尝试过插入排序算法来确定数组中的最大数目,之后,我将最大值与以前所有数组的元素进行了比较,但没有工作。
void insertion_array(float array[], int n) //* insertion algorithm*//
{
int i = 1, j;
float x;
for (; i < n; i++) {
x = array[i];
j = i - 1;
while ((j >= 0) && (array[j] >
我正在使用euclids算法的一个简化版本来找到两个整数的hcf。使用递归函数。似乎不起作用,它只是一直返回c。你知道为什么它最终不会返回a+b吗?
public class Euclid {
public static void main(String[] args) {
// TODO Class to find HCF (GCD) of two ints, using recursion
Euclid r = new Euclid();
System.out.println(r.hcf(188, 112));
}
public int hcf(
def gei(a, b):
'''
a, b: only positive integers! If you don't, we will make you. Max 1337
For extracting the juice of the numbers in the form of a common divider
'''
#Not Ruby, no to_i, but int()
smallest = int(abs(min(a, b)))
biggest =
这就是挑战所在。给定两个名为a和b的整数:
//找出两个最大的数,小于a和b,可被彼此整除。
//输入: a:102,b:53 //输出:(102,51)
//输入: a:7,b:4 //输出:(6,3)
//输入: a:3,b:2 //输出:(2,2)
关键是,我不想暴力破解它。我想结果是O(n²)。
下面是该方法的签名:
static Tuple<int, int> Analyze(int a, int b)
{
if (a % b == 0)
return new Tuple<int, int>(a, b);
else