I can't get Accepted in this problem. Someone please help me.
![:oops:](./images/smilies/icon_redface.gif)
Code: Select all
removed
Moderator: Board moderators
Code: Select all
removed
Code: Select all
removed
Code: Select all
if(y>x)
printf("%lld - %lld = %lld = %lld * %lld\n",y,x,y-x,9LL,(y-x)/9);
else
printf("%lld - %lld = %lld = %lld * %lld\n",x,y,x-y,9LL,(x-y)/9);
Code: Select all
#include<iostream>
#include<math.h>
using namespace std;
int main(){
char a[15];
int b[15],f[15];
int c,d,e,t,q=10;
long double r=0,s=0,ans;
#ifndef ONLINE JUDGE
freopen("11371.txt","r",stdin);
#endif
while(gets(a)){
for(c=0;a[c];c++){
b[c]=a[c]-49+1;
f[c]=b[c];
}
for(d=1;d<c;++d){
for(e=c-1;e>=d;--e){
if(b[e-1]>b[e]){
t=b[e-1];
b[e-1]=b[e];
b[e]=t;
}
}
}
for(d=1;d<c;++d){
for(e=c-1;e>=d;--e){
if(f[e-1]<f[e]){
t=f[e-1];
f[e-1]=f[e];
f[e]=t;
}
}
}
if(b[0]==0){
for(d=0;d<c;d++){
if(b[d]>0){
t=b[d];
b[d]=0;
b[0]=t;
break;
}
}
}
r=0;
for(d=0;d<c;d++){
r=r+ pow(static_cast<long double>(q),static_cast<long double>(c-d-1.00))*(static_cast<long double>(b[d]));
}
s=0;
for(d=0;d<c;d++){
s=s+ pow(static_cast<long double>(q),static_cast<long double>(c-d-1.00))*(static_cast<long double>(f[d]));
}
ans=s-r;
printf("%.0Lf - %.0Lf = %.0Lf = 9 * %.0Lf\n",s,r,ans,(ans)/(9));
}
return 0;
}
Code: Select all
#include <iostream>
#include <cstdio>
#include <cmath>
long long Reverse(long long a)
{
long long rev = 0, temp, digit;
while(a > 0)
{
digit = a % 10;
rev = rev * 10 + digit;
a /= 10;
}
return rev;
}
using namespace std;
int main()
{
long long a, b, c, d;
while(scanf("%lld", &a) == 1)
{
b = Reverse(a);
if(a > b)
{
c = (a - b);
d = c/9;
printf("%lld - %lld = %lld = 9 * %lld\n", a, b, c, d);
}
else
{
c = (b - a);
d = c/9;
printf("%lld - %lld = %lld = 9 * %lld\n", b, a, c, d);
}
}
return 0;
}
Code: Select all
Accepted................... :)
Code: Select all
100102
1
3
12300
100
147020
1990
199
5501
1293
400
Code: Select all
211000 - 100012 = 110988 = 9 * 12332
1 - 1 = 0 = 9 * 0
3 - 3 = 0 = 9 * 0
32100 - 10023 = 22077 = 9 * 2453
100 - 100 = 0 = 9 * 0
742100 - 100247 = 641853 = 9 * 71317
9910 - 1099 = 8811 = 9 * 979
991 - 199 = 792 = 9 * 88
5510 - 1055 = 4455 = 9 * 495
9321 - 1239 = 8082 = 9 * 898
400 - 400 = 0 = 9 * 0
Is the correct answer can also be "911 - 191 = 720 = 9 * 80" ?? because the question says about permutation...Input 191, correct output:911 - 119 = 792 = 9 * 88
Code: Select all
import java.io.*;
import java.util.*;
public class Main{
public static String printPermutations( char [] c ) {
String ans = "";
char [] cc = new char[c.length];
cc = c;
if(( c = nextPermutation( c ) ) != null) {
for(Character d : c){
ans = ans + d;
}
return ans;
}
else{
for(Character d : cc){
ans = ans + d;
}
StringBuffer r = new StringBuffer(ans);
r = r.reverse();
ans = r+"";
char [] n = ans.toCharArray();
n = nextPermutation( n );
ans = "";
for(Character d : n){
ans = ans + d;
}
return ans;
}
}
public static char[] nextPermutation( char[] c ) {
// 1. finds the largest k, that c[k] < c[k+1]
int first = getFirst( c );
if ( first == -1 ) return null; // no greater permutation
// 2. find last index toSwap, that c[k] < c[toSwap]
int toSwap = c.length - 1;
while ( c[ first ]>=( c[ toSwap ] ))
--toSwap;
// 3. swap elements with indexes first and last
swap( c, first++, toSwap );
// 4. reverse sequence from k+1 to n (inclusive)
toSwap = c.length - 1;
while ( first < toSwap )
swap( c, first++, toSwap-- );
return c;
}
public static int getFirst( char [] c ) {
for ( int i = c.length - 2; i >= 0; --i )
if ( c[ i ]<( c[ i + 1 ] ))
return i;
return -1;
}
// swaps two elements (with indexes i and j) in array
public static void swap( char [] c, int i, int j ) {
char tmp = c[ i ];
c[ i ] = c[ j ];
c[ j ] = tmp;
}
public static void main(String[] args)throws IOException {
BufferedReader k = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader k = new BufferedReader(new FileReader("E:/Uva-input.txt.txt"));
PrintWriter z = new PrintWriter(System.out);
String line;
while((line = k.readLine())!=null){
String line2 = "";
if(line.length()!=1){
char [] j = line.toCharArray();
line2 = printPermutations(j);
}
else{
line2 = line;
}
long n = Long.valueOf(line);
long m = Long.valueOf(line2);
long max = 0,min = 0;
if(n>m){
max = n;min = m;
}
else{
max = m;min = n;
}
long ans = max - min;
z.println(max+" - "+min+" = "+(ans)+" = 9 * "+(ans/9));
}
z.flush();
}
}