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

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

(求高手帮帮忙,谢谢)

第1个回答  推荐于2016-06-18
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;
}本回答被提问者采纳
第2个回答  2010-02-08
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;
}

谁可以帮我做一下这个C++C语言的作业
因为我对C语言的输入输出不太熟悉,对输入输出的错误不予判断!我按C++的方式给予判断。一、 基础题(15%)1.定义int n=5,a=12;写出a*=(n%=3);执行后变量n与a的值。【答案】n%=3 取余结果n=2 执行后:n=2;a=24 2.设a=b=5;分别判断表达式的真假:6>a>2与++b==6。【答案】...

关于C++的六个问题,,想请高手帮我看看 加分..
你的第一个问题没看懂。如果想要了解数组,那么应该去看内存。你给个你看不懂的例子,我帮你看看吧。2.C++最难的,最核心的,最重要的是继承和虚函数。而C++比C好用的地方是STL。3.你先了解下new吧,其他的几个你在使用过程中会慢慢体会出来的。4.区分还是要看内存... ...5.隐式类型转换,...

C语言编程的几道题?我是初学者,谁帮我把步骤写下~
int main(){ printf("***\\n");printf("* LANGUAGE *\\n");printf("* BEGINE *\\n");printf("***\\n");return 0;} 第二题 include "stdafx.h"include "stdio.h"define P 3.14 \/\/用宏定义圆周率的值 int main(){ float r=1.5,h=3,s,v;s=P*r*r; \/\/圆的...

谁帮我做下C++语言的选择题 谢谢了
1. 可以在类的构造函数中对静态数据成员进行初始化。错误。2. 友元关系可以继承。错误。3. 析构函数是在对象的生存期即将结束的时刻被自动调用的。错误。4. 使用关键字class定义的类中默认的访问权限是私有的。正确。5. delete用来删除由new建立的对象,释放指针所指向的内存空间。正确。6. ...

用C++做的黑色星期五,谁来帮我看看哪错了啊??NOCOW上就是不过!_百度知 ...
黑色星期五:#include <stdio.h>void main(), b[13]=,i,j,k,m,n,p,q,c[13]=; for(i=1990;i<2000;i++) else if(j<=5) p=1+5-j; else p=7-j+5+1; for(;p<=k;p+=7) } }} 如果帮助到您,请记得采纳为满意答案哈,谢谢!祝您生活愉快! vae.la ...

关于C++的一道编程题,请高手帮忙阿
百度网友1d6c0ad66 2006-03-21 知道答主 回答量:18 采纳率:0% 帮助的人:5.1万 我也去答题访问个人页 关注 展开全部 不要老是高手高手的,真正的高手才不愿意来帮你这种小问题,问问题可以随便问,至于谁来回答你就不要做这个主了,会的愿意的当然会回答你了,不会的,就算是高手也不会回答你 ...

谁能解决一道C++编程题
谁能帮我用c++语言帮我做一个通讯录系统啊,要求是必须用文件的输入输出做,每条记录里包括学生的姓名,编号和电话号码... 谁能帮我用c++语言帮我做一个通讯录系统啊,要求是必须用文件的输入输出做,每条记录里包括学生的姓名,编号和电话号码 展开  我来答 ...

求一个整数任意次方的最后三位数.用C++来编的,有谁能帮帮我?
每次都如此,就不会出现溢出,因为千位以上对最后三位没影响,程序如下 int lastthr(int num,int n) \/\/num的n次方 { int thr,tmp;tmp=num%1000; \/\/对1000取余,得最后三位 thr=tmp;while(--n){ thr*=tmp;thr%=1000;} return thr;} 对于num,n均为正情况,以上程序在Dev-C++通过。

求一个整数任意次方的最后三位数.用C++来编的,有谁能帮帮我?谢谢!
include<iostream> using namespace std;void main(){ const int N=10; \/\/N次方 int i=0;int num=25; \/\/整数num int sum=1;for(i=0;i<N;i++){ sum*=num;} printf("%d\\n",sum%1000); \/\/输出最后三位数 }

c++谁来帮我注释一下这段程序,看不懂
{ public:static int number;\/\/静态变量 声明 静态变量属于类不属于对象 point(){number++;}\/\/构造函数 ~point(){number--;}\/\/析构函数 };int point::number=0;\/\/静态变量类外初始化 void main(){ point *ptr;\/\/定义一个指向point的指针 point a,b;\/\/定义 对象 { point *ptr_...

相似回答