几道C++题目,谁来帮帮我?

1)Writeaprogramthattakesin2integersfromtheuser,andcomputestheirgcd(greatestcommondivi... 1) Write a program that takes in 2 integers from the user, and computes their gcd (greatest
common divisor).

The gcd of two integers is the largest integer that divides both numbers. For instance,
the gcd of 15 and 25 is 5.

Implement the followng method to compute the gcd. Let the two numbers be x, y. Take the
larger of the two numbers (let that be x), and divide x by the smaller number (y).
Let there be a remainder of z. Now you divide y by z, compute the remainder and so on.
Keep doing this until you get a remainder of 0. The smaller number when we get the
remainder of 0 is the gcd.

For example, let us find the gcd of 735 and 252.
You divide 735 by 252. You get a remainder of 231.
Now you divide 252 by 231. You get a remainder of 21.
Now you divide 231 by 21. You get a remainder of 0.
Therefore the gcd is 21.
2) Write a simple arithmetic expression translator that reads in expressions such as
25.5 + 34.2, and displays their value. Each expression consists of a number (type: float)
followed by a space followed by an operator (type: char) followed by
another number
(type: float)
. The operator you support must include + - * / You must keep evaluating
the expressions until the user enters an expression such as 0 + 0

3)
Drivers are concerned with the mileage obtained by their cars. One driver has kept track
of several tankful of gasoline by recording miles driven and gallons used for each tankful.
Develop a c++ program that uses a while statement to input the miles driven and gallons
used for each tankful. The program should calculate and display the miles per gallon
obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up
to that point. If the mileage is -1, the program quits.
4) Input an integer containing only 0s and 1s (i.e. binary integer) and prints its decimal
equivalent. Use the modulus and division operators to pick off the binary digits as before.
You only need to handle a fixed-length, 5-digit binary integer for this question. Assume that the
user will always enter a 5-digit integer.
5) The process of finding the largest and smallest numbers is used frequently in computer applications. Write a C++ program that uses a while statement to determine and print the
largest and the second largest number of x integers entered by the user, where x should
also be input by the user.

Data set Largest1 Largest2
======== ======== ========
9,9 9 9
9,9,7 9 9
9,9,9,9,9,7,8 9 9
1,1,1,1,8,6 8 6

(求高手帮帮忙,谢谢)
展开
 我来答
1025bit
推荐于2016-06-18
知道答主
回答量:34
采纳率:0%
帮助的人:0
展开全部
1)
#include <iostream>
using namespace std;
int iGcd(int iNum1,int iNum2)
{
int iSmall,iBig,iRemainder=1;
if(iNum1>iNum2)
{
iBig=iNum1;
iSmall=iNum2;
}
else
{
iBig=iNum2;
iSmall=iNum1;
}
for(;;)
{
iRemainder=iBig%iSmall;
iBig=iSmall;
if(iRemainder==0)
return iSmall;
iSmall=iRemainder;
}
}
int main()
{
int iN1,iN2;
cout<<"Input the two number:";
cin>>iN1>>iN2;
cout<<iGcd(iN1,iN2)<<endl;

return 0;
}
2)----------------------------------------------------
#include <iostream>
using namespace std;
void vOperator()
{
bool bMark=1;
for(;bMark;)
{
float fNum1,fNum2;
char cChar;
cout<<"Input the expression please:";
cin>>fNum1>>cChar>>fNum2;
if(fNum1==0&&cChar=='+'&&fNum2==0)
bMark=0;
else if(cChar=='+')
cout<<fNum1+fNum2<<endl;
else if(cChar=='-')
cout<<fNum1-fNum2<<endl;
else if(cChar=='*')
cout<<fNum1*fNum2<<endl;
else if(cChar=='/')
cout<<fNum1/fNum2<<endl;
}
}
int main()
{
vOperator();
return 0;
}
3)---------------------------------------------------------------
#include <iostream>
using namespace std;
void vMpg()
{
long lMiles,lGallons;
for(;;)
{
cout<<"Input the miles please:";
cin>>lMiles;
if (lMiles==-1)
break;
cout<<"Input the gallons please:";
cin>>lGallons;
cout<<lMiles/lGallons<<"miles per gallon\n";
}
}
int main()
{
vMpg();
return 0;
}
4)-------------------------------------------------
#include <iostream>
using namespace std;
int iBtoD(int iBin)
{
int iDec=0,iTmp1,iTmp2;
for(int i=0;i<5;i++)
{
iTmp1=0;
iTmp2=1;
int k=0;
iTmp1=iBin%10;
do
{
iTmp2*=(iTmp1*2);
k++;
}
while(k<i);
iDec+=iTmp2;
iBin/=10;
}
return iDec;
}
int main()
{
int iB;
cout<<"Input the binary number(5-digit) please:";
cin>>iB;
cout<<iBtoD(iB)<<endl;
return 0;
}
5)-----------------------------------------------------------
#include <iostream>
using namespace std;
void vFind(int* piInt,int iTotal)
{
for(int i=0;i<2;i++)
for(int j=0;j<iTotal-1;j++)
if(piInt[j]>piInt[j+1])
{
piInt[j]=piInt[j]+piInt[j+1];
piInt[j+1]=piInt[j]-piInt[j+1];
piInt[j]=piInt[j]-piInt[j+1];
}
}
int main()
{
int iTotal;
cout<<"Input the total number of integers:";
cin>>iTotal;
cout<<"Input the number separate with space:";
int* piNum;
piNum=new int[iTotal];
for(int i=0;i<iTotal;i++)
cin>>piNum[i];
vFind(piNum,iTotal);
cout<<"The largest number is:"<<piNum[iTotal-1]<<endl;
cout<<"The second largest number is:"<<piNum[iTotal-2]<<endl;
return 0;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Irreappearable
2010-02-08 · TA获得超过4956个赞
知道大有可为答主
回答量:1423
采纳率:25%
帮助的人:3119万
展开全部
1.(没有检查输入,也不保证当输入有负数的时候会返回什么。。)
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int gcd(int m, int n) {
if (n > m) {
int t = n;
n = m;
m = t;
}

int p;
do {
p = m % n;
m = n;
n = p;
} while (p != 0);

return m;
}

int main() {
int m, n;
cin >> m >> n;
cout << gcd(m, n);
return 0;
}

2.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

bool is_terminal(float op1, float op2, char op) {
return op1 == 0 && op2 == 0 && op == '+';
}

int main() {
float operand1, operand2, result;
char op;

while (true) {
cin >> operand1 >> op >> operand2;
if (is_terminal(operand1, operand2, op))
break;
switch (op) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
if (operand2 == 0) {
cout << "divider cannot be 0" << endl;
continue;
}
result = operand1 / operand2;
break;
default:
cout << "invalid input" << endl;
continue;
}
cout << result << endl;
}
return 0;
}

3.
(没看懂。。。= = 最好有解释或者给一个sample input/output)

4.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main() {
int value = 0;
char c;
for (int i = 0; i < 5; ++i) {
cin >> c;
if (c == '0') {
value <<= 1;
} else if (c == '1') {
++(value <<= 1);
} else {
cout << "invalid input" << endl;
}
}
cout << value << endl;
return 0;
}

5.(假定至少有两个输入)
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <queue>
using std::priority_queue;

int main() {
int n, t;
cout << "Input the number of integers: " << endl;
cin >> n;
cout << "Input the integers: " << endl;

priority_queue<int> q;
for (int i = 0; i < n; ++i) {
cin >> t;
q.push(t);
}

cout << "largest: " << q.top() << endl;
q.pop();
cout << "largets2: " << q.top() << endl;

return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式