2018年7月23日 星期一

itsa [C_AR09-易]兩數差值

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

void replaceD(string &s)
{
int i = 0;
while (i < s.length())
{
if (s[i] == ',')
s[i] = ' ';
i++;
}
}

int main()
{
int x = 0, MAX = 0, MIN = 0, largest = 1, ml = 1, nl = 1;
int n[7] = { 0 };
string s;
getline(cin, s);
replaceD(s);
stringstream ss(s);
while (ss >> n[x])
{
x++;
largest *= 10;
}
for (int i = 0; i < x - 1; i++)
{
for (int j = i; j < x; j++)
{
if (n[i] > n[j])
{
int tmp = n[i];
n[i] = n[j];
n[j] = tmp;
}
}
}
for (int i = 0; i < x; i++)
{
ml = 1;
nl = largest / 10;
for (int j = 0; j < i; j++)
{
ml *= 10;
nl /= 10;
}
MAX += n[i] * ml;
MIN += n[i] * nl;
}
cout << MAX - MIN << endl;
return 0;
}

沒有留言:

張貼留言