小编yob*_*o97的帖子

寻找不可减少的部分

给定一个正整数n,它要求找到一个可以选择两个号码的可能性A,并B从集合[1...n],使得GCDABB.所以我的方法是计算对的数量,使得一个可被另一个整除.答案预计是不可缩减的分数形式.
示例:
1 2 3
输出:
1/1 3/4 5/9

long n = sc.nextLong();
long sum=0;
for(long i=1;i<=n/2;i++)
    sum+=(n/i)-1;
 long tot = n*n;
 sum+=n;
 long bro = hcf(tot,sum);
 sum/=bro;
 tot/=bro;
 System.out.print(sum+"/"+tot);
Run Code Online (Sandbox Code Playgroud)

我的hcf功能是:

public static long hcf(long n1,long n2)
{
    if (n2!=0)
        return hcf(n2, n1%n2);
    else 
        return n1;
}
Run Code Online (Sandbox Code Playgroud)

但编译器消息超时.我认为这个hcf函数可能存在一些问题,或者有一种更好,更有效的方法来找到不可约的部分.由于它对于较小的输入是成功的,我认为最有可能找到不可简化的分数形式的有效方法.有什么建议?

java algorithm math performance

8
推荐指数
1
解决办法
670
查看次数

更有效的算法来查找两组的OR

给定's和's 的n行和m列矩阵,需要找出可以选择的行对的数量,以便它们是.10OR11111....m times

例:

1 0 1 0 1
0 1 0 0 1
1 1 1 1 0
Run Code Online (Sandbox Code Playgroud)

回答:

2 ---> OR of row number [1,3] and [2,3]
Run Code Online (Sandbox Code Playgroud)

鉴于n并且m可能是一个订单<= 3000,这个问题的解决效率如何?

PS:我已经尝试过一种天真的O(n*n*m)方法.我在考虑一个更好的解决方案.

algorithm binary-operators

7
推荐指数
1
解决办法
231
查看次数

找到可能数量的有效方法

给定三个整数n,k并且d,有多少种方式可以n表示为正整数之和i<=k,这样d在总和中至少出现一次.保证这一点0<d<=k.我的方法是递归的;

#include <stdio.h>
#include <stdlib.h>
int n,k,d,ans=0;
void solve(int totw,int flag)//totw is the total sum till now, and flag is to keep track of the number of d's in the sum.
{
    if(totw>n)
        return;
    if(totw==n && flag>0)//flag>0--->at least 1 d
    {
        ans = (ans+1)%1000000007;//answer is expected modulo 10^9+7
        return;
    }
    int i=1,h=k;
    if(h>n-totw)
        h=n-totw;
    while(i<=h)
    {
        if(i==d)
            flag++;
        solve(totw+i,flag);
        i++;
    }
}
int main()
{
    scanf("%d …
Run Code Online (Sandbox Code Playgroud)

c algorithm performance time

5
推荐指数
1
解决办法
160
查看次数

从数组中选择最大子数组

给定一个长度为 的数组,n如果不允许选择数组的两个以上连续元素,则需要找到可以选择的最大元素和。例如;

n=5;
arr[5] = {10,3,5,7,3};
Output : 23
10+3+7+3=23
Run Code Online (Sandbox Code Playgroud)

所以我写了这段代码;

#include <stdio.h>
#include <stdlib.h>
int max=0;
void solve(int arr[],int ind,int sum,int n,int count)
{
    if(ind==n){
          if(sum>max)
              max=sum;
      }
      else{
          sum+=arr[ind];
          if(ind==n-1)
            solve(arr,ind+1,sum,n,1);
          if(ind==n-2 && count>1)
            solve(arr,ind+2,sum,n,1);
          if(ind<n-1 && count<2){
              count++;
              solve(arr,ind+1,sum,n,count);
          }
          if(ind<n-2)
              solve(arr,ind+2,sum,n,1);
          if(ind<n-3)
              solve(arr,ind+3,sum,n,1);
      }
}
int main()
{
    int n;
    scanf("%d",&n);
    int i=0,arr[n];
    while(i<n){
        scanf("%d",&arr[i]);
        i++;
    }
    int count=1;
    //going into all three possibilities
    solve(arr,0,0,n,count);
    solve(arr,1,0,n,count);
    solve(arr,2,0,n,count);
    printf("%d\n",max);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序n<1000为较大的输入生成预期的输出,但显示运行时错误 …

c arrays algorithm recursion runtime-error

4
推荐指数
1
解决办法
148
查看次数

解决2d阵列中的迷宫

我已经提到了许多文章和问题,这些文章和问题有效地解决了如何解决迷宫问题,但在这里我想确认我的代码中出了什么问题.考虑迷宫:

2 1 0 0 3
0 1 0 1 1
0 1 0 0 1
0 1 1 0 0
0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)

其中1代表墙壁,0代表路径.(来源为2,目的地为3).我必须输出是否有路径.

int y=0;
        while(y==0)
        {
            robo1(n,m,maze);//this function adds 2 to any '0'/'3' in (i,j+1),(i+1,j),(i-1,j),(i,j-1) (if exists),where (i,j) is 2
            robo2(n,m,k2,maze);//this function adds 3 to any '0'/'2' in (i,j+1),(i+1,j),(i-1,j),(i,j-1) (if exists), where (i,j) is 3
            if(find5(n,m,maze)==1)//this function returns 1 if there is '5' in the maze
                y++;
            if(find0(n,m,maze)==0)//this function returns 0 if there are no …
Run Code Online (Sandbox Code Playgroud)

c c++ algorithm

3
推荐指数
1
解决办法
1016
查看次数

时间复杂度有何不同?

如果首先存储值然后使用它,是否可以节省时间?
例如;

while(i<strlen(s))
 //code
Run Code Online (Sandbox Code Playgroud)

int n = strlen(s);
while(i<n)
 //code
Run Code Online (Sandbox Code Playgroud)

第二种方法的时间复杂度是否较低,因为首先存储然后使用函数的返回值,从而避免每次进入循环时调用函数?

c performance time function

3
推荐指数
1
解决办法
98
查看次数

可能是直角三角形吗?

给定h斜边和s表面区域,如果可能,要求它打印直角三角形的边,否则打印-1.所以这是我的方法;

   double h,s;
   scanf("%lf %lf",&h,&s);
   s*=4;
   double squaresum=(h*h) + s;
   double squarediff=(h*h) - s;
   if(squarediff<0)
       printf("-1\n");
   else
   {
       double a = sqrt(squaresum)+sqrt(squarediff);
       a/=2;
       double b = sqrt(squaresum)-sqrt(squarediff);
       b/=2;
       if(h>=a+b)
            printf("-1\n");
       else
        printf("%.6lf %.6lf %.6lf\n",h,a,b);
   }
Run Code Online (Sandbox Code Playgroud)

我的方法:
给定s,如果我们相乘4,那么它是2*a*b,三角形的其他边ab位置.然后我找到了(a+b)^2,(a-b)^2就像我一样h*h=a^2+b^2.它甚至通过了自定义测试用例:

4
5 6
6 10
258303 89837245228
616153 77878145466
Run Code Online (Sandbox Code Playgroud)

输出:

4.000000 3.000000 5.000000
-1
-1
546189.769984 285168.817674 616153.000000
Run Code Online (Sandbox Code Playgroud)

但答案被判断为错误.我不能拿起怎样的答案可能出错给出0<=h<=10^9和 …

c math

2
推荐指数
1
解决办法
131
查看次数

按原始索引排序的排序算法

做任何sort algorithms给定的整数数组,以防万一a[i]=a[j],a[i]>a[j]如果i>j

arrays sorting algorithm quicksort

0
推荐指数
1
解决办法
45
查看次数

查找截断到小数位数的十进制值

给定一个整数k,预计会找到分数的值103993/33102,截断到k小数位.所以我的方法如下:

    int k ;
    scanf("%d",&k);
    printf("3");
    if(k>0)
    printf(".");
    long long int num=30;
    long long int numer = 1039930;
    long long int denom = 33102;
    while(k--)
    {
        long long int bro = numer/denom;
        printf("%lld",bro-num);
        num=bro;
        num*=10;
        numer*=10;
    }
Run Code Online (Sandbox Code Playgroud)

但是,如果k20它呈现出怪异的答案....在回路中的任何问题?
http://ideone.com/dndib2

c algorithm runtime-error

-1
推荐指数
1
解决办法
57
查看次数