首页
社区
课程
招聘
[求助]很菜的问题
2008-5-17 17:17 3805

[求助]很菜的问题

2008-5-17 17:17
3805
最近无所事事自学c++
从网上下载C++Primer中文版(第4版)完整版.rar
一习题:
编写程序,提示用户输入两个数单并将两个数的范围内的每个数写到标准输出
我的答案是
#include<iostream>
main()
{
      std::cout<<"输入两个数"<<std::endl;
      int v1, v3;
      std::cin >>v1 >>v3;
      if(v1<v3){
                while(v1<(v3-1)){
                             ++v1;
                             std::cout<<" "<<v1;
                             }
                             }
                             else{
                                  while(v3<(v1-1)){
                                                    ++v3;
                                                std::cout<<" "<<v3;
                                               }
                                               }
                                               }
我用dev-c++4.992测试通过
但接着一题想不出怎么实现
如果给上题给定数1000和2000,程序将会产生什么结果?修改程序,便每行输出不超过10个数
请各位大大不要笑.........不好意思浪费大家时间了

[培训]科锐软件逆向50期预科班报名即将截止,速来!!! 50期正式班报名火爆招生中!!!

收藏
免费 0
打赏
分享
最新回复 (2)
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
zgphappy 2008-5-17 20:43
2
0
#include <iostream>

int main()
{
        int theFirstValue, theSecondValue;

        std::cout << "please input two integers:";
        std::cin >> theFirstValue >> theSecondValue;

        // 保证theFirstValue为小的
        if (theFirstValue > theSecondValue)
        {
                int temp = theFirstValue;
                theFirstValue = theSecondValue;
                theSecondValue = temp;
        }

        int i = theFirstValue;
        while (i <= theSecondValue)
        {
                std::cout << i << ' ';

                if (((i - theFirstValue + 1) % 10) == 0)
                        std::cout << std::endl;

                i++;
        }

        return 0;
}

呵呵,看看这样行吗~~
雪    币: 107
活跃值: (54)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
kkx2008 2008-5-18 12:52
3
0
3Q
学习了...
游客
登录 | 注册 方可回帖
返回