用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;
}
问题——
- 例如m = 4和n = 3,但这里max(4,3) = 4和min(4,3) =所以,我尝试过这样的代码,它将给出3^min(m,n)和2 ^max(4,3)。但是输出结果是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;
}