| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1537 人关注过本帖
标题:一道简单的编程练习题,恳求赐教。
只看楼主 加入收藏
yang_net
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-8-11
收藏
得分:0 
// sample.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<valarray>
using namespace std;

void _tmain()
{
    int firstNumber,secondNumber;
    cout<<"First number _>";
    cin>>firstNumber;
    cout<<"Second numner _>";
    cin>>secondNumber;
    cout<<"The sum is:"<<(firstNumber+secondNumber)*(abs(firstNumber-secondNumber)+1)/2
        <<endl;
    
}
2008-08-11 13:41
xzx1002002
Rank: 2
等 级:论坛游民
帖 子:68
专家分:27
注 册:2006-3-7
收藏
得分:0 
有简单的算法为什么不用呢,
(首项+末项)*项数/2
多简单,多明了。
ls的就很好。。。
2008-08-11 16:14
gloomyboy
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-8-10
收藏
得分:0 
lz你好,我对你的那个图形问题的输出很感兴趣,不知道你后来有没有得到更加好的,简便的方法呢?
2008-08-11 18:06
wahl1986
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-8-17
收藏
得分:0 
楼主的程序算法也有问题,你最后输出的不应该是num[b],你仔细分析你的程序,实际上他没有初始化...
另外.s=&x这句会引起内存的错错.S是指针他指向X,最后程序结束时要释放指针,会引起内存错误.把这句删了,他没有一点作用.下面是我帮你改的程序:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void main()
{
cout<<"input the first number:";
int x;
cin>>x;
cout<<"input the last number:";
int y;
cin>>y;
int *s=new int[200];
for (int i=0;i<y-x;i++)
{
    s[i]=x+i;   
}
int num[200];
num[0]=s[0];
for (int b=1;b<y-x;b++)
{num[b]=num[b-1]+s[b];
}
num[y-x]=num[y-x-1]+y;
cout<<"the sum is :"<<num[y-x]<<endl;
delete[] s;
system("pause");
}
2008-08-17 10:05
快速回复:一道简单的编程练习题,恳求赐教。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.010983 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved