Welcome;
I need help with an algorithm in C# to this problem. Such alghoritm should find specific numbers itself, and I don't even know how to start. If anyone knows how solve it, or have any ideas, please help.
I've got algorithm in c++ and I don't know how change it to C#. I'll put him right under my thread.
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
char a[] = "SEND";
char b[] = "MORE";
char c[] = "MONEY";
char *unikalne = 0;
unsigned char *liczba = 0;
int LicznikPetli = 0;
unsigned int power (unsigned int x, unsigned int n)
{
unsigned int i, res=1;
for (i=1; i<=n; i++)
res *= x;
return res;
}
char * znajdz_unikalne(char *a, char* b, char *c)
{
unsigned int i = 0;
char *tab;
tab = new char[11*sizeof(char)];
memset(tab, '\0', 11*sizeof(char));
for (i=0; i<strlen(a); i++)
if (strchr(tab, a[i]) == NULL)
tab[strlen(tab)] = a[i];
for (i=0; i<strlen(b); i++)
if (strchr(tab, b[i]) == NULL)
tab[strlen(tab)] = b[i];
for (i=0; i<strlen©; i++)
if (strchr(tab, c[i]) == NULL)
tab[strlen(tab)] = c[i];
return tab;
}
int jaka_liczba (char c)
{
return liczba[strchr(unikalne, c)-unikalne];
}
void check (unsigned char *r)
{
unsigned int skladnikA, skladnikB, suma, i;
// SkładnikA
for (i=0, skladnikA=0; i<strlen(a); i++)
{
skladnikA += jaka_liczba(a[i]) * power(10, strlen(a)-1-i);
}
// SkładnikB
for (i=0, skladnikB=0; i<strlen(b); i++)
{
skladnikB += jaka_liczba(b[i]) * power(10, strlen(b)-1-i);
}
// SUMA
for (i=0, suma=0; i<strlen©; i++)
{
suma += jaka_liczba(c[i]) * power(10, strlen©-1-i);
}
// SUMA ?
if ((skladnikA + skladnikB) == suma && r[4] != 0)
{
for (i=0; i<strlen(unikalne); i++)
cout << ""<<unikalne[i] <<"="<<int(r[i])<<endl;
}
}
void generuj (int level, int counter)
{
unsigned int i, j, con = 0;
if (level > 0)
{
for (i=0; i<10; i++)
{
for (j=0, con=0; j<counter; j++)
{
if (liczba[j] == i)
con++;
LicznikPetli++;
}
if (con)
continue;
liczba[counter] = i;
generuj(level-1, counter+1);
}
//printf("\n");
}
else
{
check(liczba);
}
}
int main (void)
{
clock_t start = clock();
unikalne = znajdz_unikalne(a,b,c);
liczba = (unsigned char *) new char[strlen(unikalne)];
generuj(strlen(unikalne), 0);
// Czas wykonywania i licznik
cout<<"\nCzas wykonywania = "<<(((double) clock() - start) / CLOCKS_PER_SEC);
//cout<<("Ilosc przejsc petli = {0}",LicznikPetli);
getchar();
return 0;
}
Send+more=money solution
Started by Twister_86, May 12 2010 06:22 AM
7 replies to this topic
#1
Posted 12 May 2010 - 06:22 AM
|
|
|
#2
Posted 12 May 2010 - 06:42 AM
I don't think any one wants to go through all that code and convert it for you, try this free converter: C++ to C# Converter
or this one: https://sourceforge....ojects/cpptocs/
or this one: https://sourceforge....ojects/cpptocs/
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.
Science is only an educated theory, which we cannot disprove.
#3
Posted 17 May 2010 - 05:51 AM
And when u write code. Write it in english not in polish. You can try it on polish forum codeguru. But it's still in bad taste.
#4
Posted 17 May 2010 - 09:17 AM
lobo521 said:
And when u write code. Write it in english not in polish. You can try it on polish forum codeguru. But it's still in bad taste.
It's his code, how was he supposed to know that he will ever need forum help?
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics
#5
Posted 17 May 2010 - 11:01 PM
Im polish and i always write code in english.
#6
Posted 17 May 2010 - 11:14 PM
lobo521 said:
Im polish and i always write code in english.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics
#7
Posted 18 May 2010 - 12:31 AM
always wrap your code with CODE tags :D
#8
Posted 18 May 2010 - 02:38 AM
this one will compile but its not complete. Throws one exception.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static string a = "SEND";
static string b = "MORE";
static string c = "MONEY";
static string unikalne = null;
static char[] liczba = null;
static int LicznikPetli = 0;
static int power(int x, int n)
{
int i, res = 1;
for (i = 1; i <= n; i++)
res *= x;
return res;
}
static string znajdz_unikalne(string a, string b, string c)
{
int i = 0;
string tab = string.Empty;
for (i = 0; i < a.Length; i++)
{
if (!tab.Contains(a[i]))
tab += a[i];
}
for (i = 0; i < b.Length; i++)
{
if (!tab.Contains(b[i]))
tab += b[i];
}
for (i = 0; i < c.Length; i++)
{
if (!tab.Contains(c[i]))
tab += c[i];
}
return tab;
}
static int jaka_liczba(char c)
{
throw new NotImplementedException("don't know how to implement that");
//return liczba[strchr(unikalne, c) - unikalne];
}
static void check(char[] r)
{
int skladnikA, skladnikB, suma, i;
// SkładnikA
for (i = 0, skladnikA = 0; i < a.Length; i++)
{
skladnikA += jaka_liczba(a[i]) * power(10, a.Length - 1 - i);
}
// SkładnikB
for (i = 0, skladnikB = 0; i < b.Length; i++)
{
skladnikB += jaka_liczba(b[i]) * power(10, b.Length - 1 - i);
}
// SUMA
for (i = 0, suma = 0; i < c.Length; i++)
{
suma += jaka_liczba(c[i]) * power(10, c.Length - 1 - i);
}
// SUMA ?
if ((skladnikA + skladnikB) == suma && r[4] != 0)
{
for (i = 0; i < unikalne.Length; i++)
Console.WriteLine("{0} = {1}", unikalne[i], r[i]);
}
}
static void generuj(int level, int counter)
{
int i, j, con = 0;
if (level > 0)
{
for (i = 0; i < 10; i++)
{
for (j = 0, con = 0; j < counter; j++)
{
if (liczba[j] == i)
con++;
LicznikPetli++;
}
if (con > 0)
continue;
liczba[counter] = (char)i;
generuj(level - 1, counter + 1);
}
//printf("\n");
}
else
{
check(liczba);
}
}
static int Main(string[] args)
{
//clock_t start = clock();
unikalne = znajdz_unikalne(a, b, c);
liczba = new char[unikalne.Length];
generuj(unikalne.Length, 0);
// Czas wykonywania i licznik
//cout<<"\nCzas wykonywania = "<<(((double) clock() - start) / CLOCKS_PER_SEC);
//cout<<("Ilosc przejsc petli = {0}",LicznikPetli);
Console.Read();
return 0;
}
}
}


Sign In
Create Account

Back to top









