求一个范围内的所有整数的平方和

转载 2012年03月21日 16:10:14
#include <stdio.h>
// validate that input is an integer
int get_int(void);
// validate that range limits are valid
bool bad_limits(int begin, int end, int low, int high);
// calculate the sum of the squares of the integers
// a through b
double sum_squares(int a, int b);
int main(void)
{
    const int MIN = -1000;  // lower limit to range
    const int MAX = +1000;  // upper limit to range
    int start;              // start of range
    int stop;               // end of range
    double answer;
   
    printf("This program computes the sum of the squares of "
           "integers in a range.\nThe lower bound should not "
           "be less than -1000 and\nthe upper bound should not "
           "be more than +1000.\nEnter the limits (enter 0 for "
           "both limits to quit):\nlower limit: ");
    start = get_int();
    printf("upper limit: ");
    stop = get_int();
    while (start !=0 || stop != 0) 
    {
        if (bad_limits(start, stop, MIN, MAX))
            printf("Please try again.\n");
        else
        {
            answer = sum_squares(start, stop);
            printf("The sum of the squares of the integers ");
            printf("from %d to %d is %g\n", start, stop, answer);
        }
        printf("Enter the limits (enter 0 for both "
               "limits to quit):\n");
        printf("lower limit: ");
        start = get_int();
        printf("upper limit: ");
        stop = get_int();
    }
    printf("Done.\n");
   
    return 0;
}

int get_int(void)
{
    int input;
    char ch;
   
    while (scanf("%d", &input) != 1)
    {
        while ((ch = getchar()) != '\n')
            putchar(ch);  // dispose of bad input
        printf(" is not an integer.\nPlease enter an ");
        printf("integer value, such as 25, -178, or 3: ");
    }
   
    return input;
}

double sum_squares(int a, int b)
{
    double total = 0;
    int i;
   
    for (i = a; i <= b; i++)
        total += i * i;
   
    return total;
}

bool bad_limits(int begin, int end, int low, int high)
{
    bool not_good = false;
    
    if (begin > end)
    {
        printf("%d isn't smaller than %d.\n", begin, end);
        not_good = true;
    }
    if (begin < low || end < low)
    {
        printf("Values must be %d or greater.\n", low);
        not_good = true;
    }
    if (begin > high || end > high)
    {
        printf("Values must be %d or less.\n", high);
        not_good = true;
    }
      
    return not_good;
}

求任意整数范围内的完全数

  • 2012年04月20日 10:39
  • 502KB
  • 下载

①C#,利用数组求一个范围内的质数

/*质数(prime number)又称素数,有无限个。除了1和它本身以外不再有其他的因数;否则称为合数。 * 根据算术基本定理,每一个比1大的整数,要么本身是一个质数,要么...

c++训练题(求某个数的最大质数因子)兼自己写的一个打印一定范围内质数方法

质数的算法,求解最大质数因子

使一个控件在一定范围内移动

  • 2015年08月12日 17:14
  • 59KB
  • 下载

编程珠玑: 13章 搜索 13.2使用链表结构,生成[0 ,maxval]范围内m各随机整数的有序序列 -------解题总结

#include #include using namespace std; /* 问题:使用链表,生成[0 ,maxval]范围内m各随机整数的有序序列 分析:这是典型的插入排序。每次将一个带...

数字(int型范围内正整数)和中文的相互转换

数字(int型范围内正整数)和中文的相互转换

生成区间范围内的随机整数和浮点值

生成区间范围内的随机整数和浮点值
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:求一个范围内的所有整数的平方和
举报原因:
原因补充:

(最多只允许输入30个字)