如何尽可能快地对嵌套循环进行计数? [英] How Do I Make Counting In Nested Loops As Fast As Possible?

查看:158
本文介绍了如何尽可能快地对嵌套循环进行计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入三个用户数量选择

数字1

数字2

最终数字



我想知道的是,数字1和数字2的多少组合将导致最终数字= number1 * x + number2 * y =最终数字。



我使用的是嵌套循环



  int  i; 
int j;
long long int pocet = 0 ; // 组合数
long long int one; // 用户输入的第一个数字
long long int 两个; // 用户输入的第二个数字
long long int last; // 最终用户输入的数字 - 想要的结果
long long int one_max;
long long int two_max ;
long long int add = 1 ; // 用于递增循环
long long int reduc = 1 ; // 用于递减循环

if ((1%2 == 0 )&&(两个%2 == 0 )){
if (one> two){
add = two / 2;
reduc = 1/2;
}
}
one_max = last / one;
two_max = last / two;
for (i = 0 ; i< = one_max; i + = add){
for (j = two_max; j> = 0 ; j- = reduc){
long long int tmp_one =(one *一世);
long long int tmp_two =(2 * j)的;
if (tmp_one + tmp_two == last){
two_max = j;
pocet ++;
}
}
}





如果我输入这些数字

one = 10;

two = 13;

final = 100;

唯一可能的前两个数字组合最终号码的结果是

10 * 10 + 13 * 0

数字必须为非负数

解决方案

< blockquote>您已经在Algorithms论坛中发布了这个问题。请仅在一个地方发帖。


这是第一批更改的代码。由于实际原因,代码更改为函数。

  long   long   int  MyFunc( long   long   int  one, long   long   int 两个, long   long   int  last){
// one; //第一个由用户输入的数字
// two; //用户输入的第二个数字
// int last; //用户输入的最终数字 - 想要的结果

// 问题:有多少(i,j)对(正整数)将验证
// one * i + two * j = last(常数部分在右侧)

long long int i;
long long int j ;
long long int pocet = 0 ; // 组合数

for (i = 0 ; i * one< = last; i ++){
// 两个* j =最后一个* i(常数部分在右侧)
// j =(last - one * i)/ two(常数部分在右侧)
if ((last - one * i)%two == 0 ){ // 整数?
// 解决方案
j =(last - one * i)/ two;
pocet ++;
}
}
return pocet;
}



我只是坚持这里的问题。

我只是利用数学来改变等式已知。这应该已经改善了运行时。


Im inputing three numbers of users choice
number 1
number 2
final number

what i want to find out is , how many combinations of number 1 and number 2 would result in final number = number1 *x + number2*y = final number.

I am using nested loops

int i;
int j;
long long int pocet=0; // the numbers of combination
long long int one; // first number inputed by user
long long int two; // second number inputed by user
long long int last; // final number inputed by user - wanted result
long long int one_max;
long long int two_max;
long long int add=1; //for incrementing loops
long long int reduc=1; // for decrementing loops

if((one%2==0) && (two%2==0)){
        if(one>two){
            add=two/2;
            reduc=one/2;
    }
}
one_max=last/one;
two_max=last/two;
for(i=0;i<=one_max;i+=add){
    for(j=two_max;j>=0;j-=reduc){
        long long int tmp_one=(one*i);
        long long int tmp_two=(two*j);
            if(tmp_one+tmp_two==last){
                two_max=j;
                pocet++;
        }
    }
}



if i input these numbers
one = 10;
two = 13;
final =100;
the only possible combination of first two numbers to result in final number is
10 * 10 + 13 * 0
The numbers have to be non negative

解决方案

You already posted this question in the Algorithms forum. Please post in one place only.


Here is code with first batch of changes. the code is changed to a function for practical reasons.

long long int MyFunc (long long int one, long long int two, long long int last) {
	// one; // first number inputed by user
	// two; // second number inputed by user
	// int last; // final number inputed by user - wanted result
	
	// Question: How many (i,j) pairs (as positive integers) will verify
	// one*i+ two*j = last (constant part is on right)
	
	long long int i;
	long long int j;
	long long int pocet=0; // the numbers of combination
	
	for(i=0;i*one <=last;i++){
		// two*j = last - one*i (constant part is on right)
		// j = (last - one*i) / two (constant part is on right)
		if ((last - one*i )% two == 0) { // integer ?
			// solution
			j= (last - one*i) / two;
			pocet++;
		}
	}
	return pocet;
}


I just stick to the problem here.
I just take advantage of mathematics to change the equation as i is known. This should already improve the runtime.


这篇关于如何尽可能快地对嵌套循环进行计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆