zoukankan      html  css  js  c++  java
  • UVA 10783 Odd Sum

    Given a range [a, b], you are to find the summation of all the odd integers in this range. For example, the summation of all the odd integers in the range [3, 9] is 3 + 5 + 7 + 9 = 24.

    Input 

    There can be at multiple test cases. The first line of input gives you the number of test cases, T ( 1$ \le$T$ \le$100). Then T test cases follow. Each test case consists of 2 integers a and b ( 0$ \le$a$ \le$b$ \le$100) in two separate lines.

    Output 

    For each test case you are to print one line of output - the serial number of the test case followed by the summation of the odd integers in the range [a, b].

    Sample Input 

    2
    1
    5
    3
    5
    

    Sample Output 

    Case 1: 9
    Case 2: 8
    



    题意:给定两个整数,求这两个数之间所有奇数的和。
    思路:判断出两个正整数的奇偶性,然后累加,注意求和的参数的初始化!
    AC源代码(C语言):

     1 #include<stdio.h>
     2 int main()
     3 {
     4   int a,b,T,i,j,sum;
     5   while(scanf("%d",&T)==1)
     6    {
     7      for(i=1;i<=T;i++)
     8       {
     9         sum=0;
    10         scanf("%d%d",&a,&b);
    11         for(j=a;j<=b;j++)
    12          {
    13            if(j%2==1)  sum+=j;             
    14          } 
    15         printf("Case %d:%d\n",i,sum);                
    16       } 
    17      break;  //保证只输入T组数据!!!!!                                            
    18    }    
    19   return 0;  
    20 }
    2013-03-14
    
    
    
  • 相关阅读:
    python
    weui 问题
    Mac 问题
    ORM存储过程和实体类代码生成工具
    说说QQ空间SEO
    用户体验走嘴和走心的区别
    一切不以用户为中心的O2O 都是耍流氓
    10分钟制作自己的手机QQ
    一无所有其实没什么
    别人的鞋不一定合脚
  • 原文地址:https://www.cnblogs.com/fjutacm/p/2932717.html
Copyright © 2011-2022 走看看