Go re-order

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.