Created
December 20, 2019 21:10
-
-
Save jin-x/ea2d144d5ffa23ce4aff0c6615d023dc to your computer and use it in GitHub Desktop.
@jinxonik / UniLecs #201
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| // Returns -1 for illegal source number (n) | |
| int spec_sum(int n) | |
| { | |
| if (n < 100 || n > 999) return -1; | |
| int a = n/100, b = n/10%10, c = n%10; | |
| if (b+c == 0) return n*2; | |
| int mn = min(a,min(b,c)), mx = max(a,max(b,c)); | |
| if (mn == 0) return (a+b+c)*110 - mx*9; // (a+b+c-mx)*110 + mx*101 | |
| return (mn+mx)*81 + (a+b+c)*20; // (mn+mx)*101 + (a+b+c-mn-mx)*20 | |
| } | |
| int main() | |
| { | |
| cout << spec_sum( 99) << endl; // -1 | |
| cout << spec_sum(100) << endl; // 100+100=200 | |
| cout << spec_sum(800) << endl; // 800+800=1600 | |
| cout << spec_sum(504) << endl; // 405+540=945 | |
| cout << spec_sum(709) << endl; // 709+970=1679 | |
| cout << spec_sum(413) << endl; // 134+431=565 | |
| cout << spec_sum(997) << endl; // 799+997=1796 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment