簡體   English   中英

Q - 給定正整數 - m,n.. 查找並打印 3^min(m,n) 和 2^max(m,n) 的值

[英]Q - Given positive integers- m,n.. Find and print the values of 3^min(m,n) and 2^max(m,n)

使用 C++ 編寫了以下內容:

#include <iostream>
using namespace std;
int main()
{
    int m,n;
    int threemin, twomax;
    threemin = 1; twomax = 1;
    cout<<"Enter m"<<"Enter n";
    cin>>m>>n;
    int i,j;
    for ((i = 1, j = 1) ; ( (i <= m), (j <= n) ) ; (i++,j++))
    {
        if (m>n){ i <= n ; threemin = threemin*3;}  //  for changing max value of i if m > n because we want to print 3^min(m,n)
        else {  threemin = threemin*3 ;} ; // 
        if (m>n){ j <= m ; twomax = twomax*2;} // same for changing j
        else {  twomax = twomax*2 ; } 
        
        
    }
    cout<<"Threemax is"<<threemin<<"Twomax is"<<twomax;
    return 0;
    
}

問題 -

  1. 例如 m = 4 和 n = 3 但這里 max(4,3) = 4 和 min(4,3) = 所以,我嘗試了這樣的代碼,它將給出 3^min(m,n) 和 2 ^max (4,3)。 但是 Output 結果是 threemin = 3^3 = 81 和 twomax = 2^3 = 8。兩者都以 n 作為指數。

我是初學者。請幫我糾正。

您可以改為執行以下操作:

#include <iostream>
using namespace std;

int main()
{
    int m, n, lesser, greater, threemin=1, twomax=1, i=1;

    cout << "Enter m " << "Enter n";
    cin >> m >> n;

    lesser = min(m, n);     // find smallest
    greater = max(m, n);    // find largest

    while(i <= lesser) {    // ride on smallest untill done
        threemin *= 3;
        twomax *= 2;
        i++;
    }

    while(i <= greater) {    // continue for largest
        twomax *= 2;
        i++;
    }

    cout << "Threemin is: " << threemin << ", Twomax is: " << twomax;
    return 0;
}

查找m * m(2 &lt;= m

[英]Find a subarray of m*m (2<=m<n) having largest sum; out of an n*n int array(having +ve, -ve, 0s)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM