2018年1月12日 星期五

[MM74-易] Odd1(C++)

[MM74-易] Odd1

Time Limit: 1 seconds
Problem Description
Random odd numbers in a given range or set. A range a to b, please find between a, b and all the odd. For example, the range [1,6] of all odds is 1 + 3 + 5 = 9.
Input File Format
Each set of test data for two, including two figures, namely a and b, and a and b are two numbers input range (0 ≦ a ≦ b ≦ 100).
Output Format
Each set of test data output one, the contents of a and b between (including) the sum of all odd.
Example

Sample Input:Sample Output:
1 5
2 4
3 7
10 11
1 1
9
3
15
11
1
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int main() {  
  5.     // [MM74-易] Odd1  
  6.     int a, b;  
  7.     while(cin >> a >> b)  
  8.     {  
  9.         int re = 0;  
  10.         for(int i = a;i <= b;i++)  
  11.         {  
  12.             if(i % 2 != 0)  
  13.             {  
  14.                 re = re + i;  
  15.             }  
  16.         }  
  17.         cout << re << endl;  
  18.     }  
  19.     return 0;  
  20. }  

沒有留言:

張貼留言