Az alábbi letöltési lehetőségek közül választhatsz: (
segítség)
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: utf-8
Méret: 4 KB
using System;
using System.IO;
using System.Collections.Generic;
namespace Szamjegy
{
class Solution
{
Dictionary<char,int> charToIdx = new Dictionary<char,int>();
int[] values;
String[] numbers;
int eqIndex = 0;
public Solution() {}
public void solution() {
long sum = 0;
for( int i=0; i<numbers.Length; i++ ) {
if( i == eqIndex ) {
Console.Write("=");
} else {
if( i != 0) Console.Write("+");
}
sum = 0;
for( int a=0; a<numbers[i].Length; a++ ) {
sum += values[ charToIdx[ numbers[i][a] ] ] * (long)Math.Pow( 10.0, (double)(numbers[i].Length-a-1) );
}
Console.Write( sum );
}
Console.WriteLine( "" );
}
public bool handle()
{
long sum = 0;
long savedSum = 0;
bool cool = true;
for( int i=0; i<numbers.Length; i++ ) {
if( i == eqIndex ) {
savedSum = sum;
sum = 0;
}
for( int a=0; a<numbers[i].Length; a++ ) {
if( a==0 ) {
if( values[ charToIdx[ numbers[i][a] ] ] == 0 ) {
sum = 0;
cool = false;
break;
}
}
sum += values[ charToIdx[ numbers[i][a] ] ] * (long)Math.Pow( 10.0, (double)(numbers[i].Length-a-1) );
}
if( !cool ) break;
}
if( cool && savedSum == sum ) {
solution();
return true;
}
return false;
}
public void solve(String s)
{
String[] oldalak = s.Split('=');
if( oldalak.Length != 2 ) return;
String[] numbers1 = oldalak[0].Split('+');
String[] numbers2 = oldalak[1].Split('+');
eqIndex = numbers1.Length;
numbers = new String[numbers1.Length+numbers2.Length];
for( int i=0; i<numbers1.Length; i++ ) {
numbers[i] = numbers1[i];
}
for( int i=0; i<numbers2.Length; i++ ) {
numbers[i+numbers1.Length] = numbers2[i];
}
charToIdx.Clear();
int c = 0;
for( int i=0; i<numbers.Length; i++ ) {
for( int a=0; a<numbers[i].Length; a++ ) {
if( !charToIdx.ContainsKey( numbers[i][a] ) ) {
charToIdx.Add( numbers[i][a], c++ );
}
}
}
values = new int[10];
for( int i=0; i<10; i++ ) {
values[i] = i;
}
int j, m, k;
int n = 10;
bool cserelve = true;
while(true) {
if( cserelve ) {
if( handle() ) break;
}
j = n - 2;
while( values[j] >= values[j+1] ) {
j--;
if( j == -1 ) break;
}
if( j == -1) break;
m = n-1;
while( values[j] >= values[m] ) {
m--;
if( m == -1 ) break;
}
if( j > charToIdx.Count-1 ) {
cserelve = false;
} else {
cserelve = true;
}
swap( j, m );
k = j+1;
m = n-1;
while( k < m ) {
swap( k++, m-- );
}
}
}
private void swap(int j, int m) {
int tmp = values[j];
values[j] = values[m];
values[m] = tmp;
}
}
class Program
{
public Program()
{
StreamReader r = new StreamReader("szamjegy.be");
Solution s = new Solution( );
String str;
while( (str = r.ReadLine()) != null ) {
s.solve( str );
}
}
public static void Main(string[] args)
{
Program p = new Program();
Console.ReadKey();
}
}
}