Topic background
A simple math problem ...
Title Description
Enter a number N (n<=200 bit), split, to derive the smallest and largest sequence.
such as: 123 to find the smallest =123
123 Find out the biggest =321
and find out the difference between them.
such as: 321-123 =?
If it is not a number, direct output no
Input/output format
Input format:
First line: Enter a number N (n<=200 bit)
Output format:
Output See "title description"
Input and Output Sample input example # #:
123
Sample # # of output:
321-123=198
Input Sample #:
10
Output Example #:
10-01=9
Input Sample # #:
Satan opposes Wei Feng net
Output Sample # #:
NO
Description
Data =<30 bit for%30
Data =<60 bit for%60
Data =<200 bit for%100
Ideas:
Two times sort, subtract again, remove leading 0 and output;
There is a point where the maximum arrangement is equal to the smallest arrangement, when a 0 is output;
Come on, on the code:
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;intlen,ans[ -];Charch[ -],ch_1[ -],ch_2[ -];BOOLcmpCharACharb) { returnA>b;}intMain () {CIN>>ch; Len=strlen (CH); for(intI=0; i<len;i++) { if(ch[i]>'9'|| ch[i]<'0') {cout<<"NO"<<Endl; return 0; } Ch_1[i]=ch[i],ch_2[i]=Ch[i]; } sort (ch_1,ch_1+len,cmp); Sort (ch_2,ch_2+Len); cout<<ch_1<<'-'<<ch_2<<'='; for(intI=0; i<len;i++) {Ans[i]=ch_1[i]-Ch_2[i]; } for(inti=len-1; i>=0; i--) { if(ans[i]<0) ans[i-1]--, ans[i]+=Ten; } BOOLif_=true; for(intI=0; i<len;i++) { if(ans[i]==0)Continue; If_=false; for(intj=i;j<len;j++) cout<<Ans[j]; Break; } if(if_) cout<<'0'; return 0;}
Go re-order