RE: Multiples of 3 and 5 11-25-2016, 10:08 PM
#6
(11-25-2016, 09:01 PM)Vi-Sion Wrote: @Pikami you share the solution here in this thread .Thanks, that explained a lot.
I think you missed the thread with the rules and the explanation so here you go https://sinister.li/Thread-Competition-R...scoreboard.
Here is my solution in C++ (Tested on windows):
Code:
#include <iostream>
#include <vector>
int getSum(); //A function that gets the sum of all the multiples of 3 or 5 below 1000
int reverseNum(int a); //A function that reverses a number
int sortNum(int a); //A function for sorting a number
int main(){
int sum = getSum();
std::cout<<"The sum of all the multiples of 3 or 5 below 1000: "<<sum<<std::endl;
std::cout<<"This number reversed is: "<<reverseNum(sum)<<std::endl;
std::cout<<"This number sorted number is: "<<sortNum(sum)<<std::endl;
}
int getSum(){
int sum = 0;
for(int i=1;i<1000;i++)
if(i%3==0 || i%5==0)
sum+=i;
return sum;
}
int reverseNum(int a){
int newNum = 0; //The new number
while(a>0){
newNum*=10;
newNum += a%10;
a/=10;
}
return newNum;
}
int sortNum(int a){
int n = 0; //The number of digits
std::vector<int> D; //The digits
//Get the digits
while(a>0){
D.push_back(a%10);
a/=10;
n++;
}
//Sort the vector
for(int i=0;i<n;i++)
for(int o=i;o<n;o++)
if(D[o]<D[i])
std::swap(D[o],D[i]);
//Build the new number
int newNum = 0;
for(int i=0;i<n;i++)
newNum=newNum*10+D[i];
return newNum;
}Here's the output:
Code:
The sum of all the multiples of 3 or 5 below 1000: 233168
This number reversed is: 861332
This number sorted number is: 123368
(This post was last modified: 11-25-2016, 10:12 PM by Pikami.
Edit Reason: Added the output of my code
)












![[Image: 9H83e18.png]](https://i.imgur.com/9H83e18.png)
![[+]](https://sinister.li/images/modern/collapse_collapsed.png)