【问题标题】:how to find the position of the factor number in c++如何在c ++中找到因子编号的位置
【发布时间】:2020-11-22 02:53:30
【问题描述】:

我在这里找到因子号的位置

例如

当用户进入运行时 10

1 2 5 10

当用户输入第3个因素的位置时

然后返回第三个因子意味着 5

例如

当用户进入运行时 20

1 2 4 5 10 20

以及当用户输入因子5的位置时

然后返回第 5 个因子表示 10

我创建了一个程序,但剩下一件事来查找因子编号位置


#include<iostream>

using namespace std;

int Factor(int n) {
    
    int i,j=0;
    
    for(i=1;i<=n;i++)
    {
        if(n%i==0)            //10mod1=0 reminder
        {
            //j=i;            //here i am storing the factor number
            cout << i ;
        }
    }
    return i;
}

int main()
{
    int x;
    
    cout << "Enter your factor number:";
    cin >> x;
    
    Factor(x);

    return 0;
}

直播节目链接:https://onlinegdb.com/HJ4yAyXZw

现在我要做的是找到因子编号位置

如何实现查找因子号位置的逻辑

帮助

【问题讨论】:

  • 在 for 循环中,您还可以对找到的因子进行计数,如果计数达到该位置,则可以返回该数字。

标签: c++


【解决方案1】:

在 for 循环中,您可以对找到的因子进行计数,如果计数达到该位置,则该位置的因子就是您想要的答案。

您可以在此处自行返回此答案,也可以将其存储在局部变量中并在函数结束时返回。

解释这个逻辑的代码:


#include<iostream>

using namespace std;

int Factor(int n, int factorPosition) {
    int i,j=0;
    int factorsSoFar = 0;
    int factor = -1;
    for(i=1;i<=n;i++)
    {
        if(n%i==0)            //10mod1=0 reminder
        {
            factorsSoFar++;          //here i am storing the factor number
            if(factorsSoFar == factorPosition){
                factor = i;
                cout << i ;
                break;
            }
        }
    }
    return factor;
}

int main()
{
    int x, position;
    
    cout << "Enter your factor number: ";
    cin >> x;
    
    cout << "Enter the factor position: ";
    cin >> position;
    
    int factor = Factor(x,position);
    if(factor == -1){
        cout<<"No factor exists for position "<< position<<endl;
    }else{
        cout<<"Factor is: "<<factor<<endl;
    }
    return 0;
}

【讨论】:

  • 您也可以避免使用factor。对于真实情况,只需返回 i。见这里:wandbox.org/permlink/vnlV8nDNpWbs9axE
  • @eepak Patankar 感谢您的帮助,但在打印所有因子编号时输出不正确
  • @DeepakPatankar 请检查我的程序onlinegdb.com/BJLlvxQ-P
  • 嘿@Rahul,你期待什么输出,你得到什么?
  • @DeepakPatankar 当用户输入 10 然后显示所有类似因子的 1 2 5 10 然后询问因子 2 的位置然后因子的位置是 2
【解决方案2】:

在 for 循环中,您可以计算因子的索引(位置)。我使用 bits/stdc++.h 因为它比 iostream 快​​。我试图以简单的方式解决这个问题,发现你的 int j=0 变量是未使用的。
所以这是我的方式:

#include<bits/stdc++.h>
using namespace std;

void Factor(int n , int position){

    int i,count=0;

    for(i=1;i<=n;i++)
    {
        if(n%i==0)            //10mod1=0 reminder
        {
            count++;            // count position
                                //here i am storing the factor number
            if (count==position){
                cout<<"factor of position "<<position<<" is "<<i<<"\n";
                break;
            }
        }
    }
    if (position>count)
        cout<<"no factor for this position\n";
}

int main()
{
    int factor,position;

    cout << "Enter your factor number: ";
    cin >> factor;

    cout << "Enter the factor position: ";
    cin >> position;

    Factor(factor,position);
}

【讨论】:

  • @JohnnyMopp 这个问题没有太大区别。检查这个link
  • @mo1ein 尽量不要使用这种方法。 “但是,我建议您花时间了解每个 sl/stl 标头并将它们分别包含在内,而不是使用“超级标头”。”
  • @MehdiMostafavi 好的。感谢 Mehdi 的好建议 :))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
  • 2015-05-12
  • 1970-01-01
相关资源
最近更新 更多