注册 登录
编程论坛 C++教室

菜鸟在看C++教材,每天晚上做题目,在此请教各位牛人点评啊

m8107318 发布于 2012-03-09 21:16, 845 次点击
在看C++教材,每天晚上做题目,在此请教各位牛人点评,有些不会做,有些做出来可能不够简洁,请各位帮帮忙啊!
今天看到循环咯!第一题.编写一个要求用户输入数字的程序,该程序将计算并输出这两个整数之间(包括这两个整数)的和,这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序将指出2~9之间所有证书的和为44.
我的答案是:
#include <iostream>
int main()
{
    using namespace std;
    int a,b;
    cout << "Enter the smaller one";
    cin >> a;
    cout << "Enter the larger one";
    cin >>b;
    int c=a;
    while (a<b)
        c+=(++a);
    cout << "the result is " << c;
    cin get();
    return 0;
}
我感觉我做的不是很明了,而且为什么后面的那个cin.get(),没有起到等待键击的作用呢,我看的书上是这么写的呢!
我现在开始第二题....

[ 本帖最后由 m8107318 于 2012-3-9 21:18 编辑 ]
9 回复
#2
y14901018342012-03-09 23:38
等待应该用system(“pause”)
#3
非死亡!2012-03-11 17:03
程序代码:
#include <iostream>
int main()
{
    using namespace std;
    int a,b;
    cout << "Enter the smaller one:";
    cin >> a;
    cout << "Enter the larger one:";
    cin >>b;
   
    cout << "the result is "<<(b-a+1)*(a+b)/2<<endl;  //有的时候也要学会用公式
    cin.get(); //这个把回车符接收了.
    cin.get();//这个没有接收.
    return 0;
}
#4
S11632503772012-03-11 17:05
爸爸妈妈
#5
cheng5799342012-03-12 21:40
#include <iostream>
using namespace std;
void main()
{
    int a,b;
    cout<<"please input two integer:"<<endl;
    cin>>a>>b;
    int c=a;
    while(a<b)
    {
        a++;
        c+=a;
    }
    cout<<c<<endl;
}

#6
ronghuang2012-03-16 20:10
不错

using namespace std; 放外面

用公式实在NB了。。。
#7
nicum2012-03-16 22:33
知道答案的可以直接写数字了哈
#8
andyzuo2012-03-16 22:52
学习学习。。。
#9
哆啦不想A梦2012-03-21 00:07
我喜欢你们 因为你们厉害。
#10
andyzuo2012-05-18 16:13
来了 来了
1