P5719 Classification and Average
Description
You're given n(n<=10000) and k(k<=100). You need to class the range from 1 to n into two groups. Class A can be divided by k completely(namely it's the multiple of k), and class B can't. Please output the average value of both of them, and set the precise to 1 digit after the dot, separating by space.
The data you are provided for will not be zero.
Input format
None
Output format
None
Example
Input
100 16
Output
56.0 50.1
!!!!!!!!!!!!!!!!!!!!!!!!!!
Coding
#include <iostream>
#include <iomanip>
using namespace std;
int classification(){
int n,k;
double nsum=0,ksum=0;
int amount=0;
cin >> n >> k;
for(int i=1;i<=n;i++){
if(i%k==0){
ksum+=i;
amount++;
}else
nsum+=i;
}
cout << fixed << setprecision(1) << ksum/amount << " " << nsum/(n-amount);
return 0;
}
What I learned
- What I thought first is looping the numbers from 1 to n and make an if judgment.
We can enumerate from k and set the step to k for further optimization.
for (int i = k; i <= n; i += k)
sum += i;

浙公网安备 33010602011771号