Aside from the header that isAMindForeverVoyaging wrote:Except that your code really is Cavrrobot wrote:well, I think I'm one of the first solving it with c++
Valuation
-
- Forum Admin
- Posts: 496
- Joined: Sat May 28, 2011 9:14 am
- Location: Germany
iostream is used for cout << x & cin >> x style streams in C++ so is usually included in programs but usually with using namespace std; too so you don't have to do std::cout << x all over the place. That technically makes it C++ even if it isn't used as a C compiler wouldn't know what to do with it, otherwise it is normal C89 as even the variables are declared at the top of blocks.
Funnily enough though I think that code works on GCC because it coerces printf from the standard libraries even though it isn't explicitly told to do so (as iostream doesn't actually contain printf) so doesn't really use any C++ features at all, aside from being backward compatible with C. Hooray for technicalities
Funnily enough though I think that code works on GCC because it coerces printf from the standard libraries even though it isn't explicitly told to do so (as iostream doesn't actually contain printf) so doesn't really use any C++ features at all, aside from being backward compatible with C. Hooray for technicalities
-
- Forum Admin
- Posts: 496
- Joined: Sat May 28, 2011 9:14 am
- Location: Germany
I just tried to compile avrrobot's code with gcc and I get linker errors. ("undefined reference to")CodeX wrote: Funnily enough though I think that code works on GCC because it coerces printf from the standard libraries even though it isn't explicitly told to do so
However with g++ instead of gcc and the very same options it compiles and links fine. And with stdio.h instead of iostream it compiles fine with gcc.
If you change the source code filename ending from .cpp to .c, that changes the behaviour too it seems... at least as long as you leave out the #include.
Technicalities indeed
arg, I forgot gcc was c and g++ was the cpp compiler, I tested it on www.codepad.org
c++ solved it for me:
Code: Select all
int chartoint(char ch) {
ch = ch - 48;
return int(ch);
}
int main() {
char buff1 = '0';
char buff2 = '0';
char ch;
int ans;
cin.get(ch);
while (ch != '\n') {
if (isdigit(ch)) {
ans += chartoint(ch);
}
else if (ch == 'x') {
ans += chartoint(buff1) + chartoint(buff2);
cin.get(ch);
continue;
}
else {}
buff2 = buff1;
buff1 = ch;
cin.get(ch);
}
cout << endl << ans;
cin.get();
return 0;
}
Thought about doing this in the HVM because it seemed like it would be right up it's alley. then I decided to use python and vi. 2 minutes later:
[/code]
Code: Select all
!/usr/bin/python
import sys,io
def calculate (s):
l = list(s)
sum = 0
i = 0
while i < len(l):
if l[i] == 'x':
del l[i]
i -= 2
else:
print l[i]
sum += int(l[i])
i += 1
return sum
s = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx'
print calculate(s)
I just knocked up some HVM code to solve this as it seemed like a good idea thanks to gunn4r: it's a bit long as I used a very rough linker made just to get the job done before I finish a more complete tool set, and also made no attempt to make the code smaller. To make up for that the smallest code possible, at least as far as I have figured at the moment, is:
Code: Select all
001^<83*5*:57* ?1^<86*-2v1+2v2^+1^<47* ?049+4*-g2^+3^+1v1+1v049+3*-gp!
Code: Select all
67*9*5-4*P
A challenger appears: C#
Code: Select all
namespace Xe
{
class Program
{
static void Main(string[] args)
{
StringBuilder input = new StringBuilder("93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx");
//StringBuilder input = new StringBuilder("93752xxx"); // testing
int sum = 0;
for (int i = 0; i < input.Length; i++)
{
if (input[i] == 'x')
{
input.Remove(i,1);
i-=3;
}
else
{
sum += (Convert.ToInt16(input[i]) - 48);
}
}
Console.WriteLine(sum);
Console.ReadKey();
}
}
}
chown -R us ./base
Reminds me of this [/url]CodeX wrote:To make up for that the smallest code possible, at least as far as I have figured at the moment, is:Code: Select all
67*9*5-4*P
There is no spoon.
I like using Python for string manipulation - writing the program takes as long as it takes to type it on the keyboard (simply rewrite the sentences in code):
Code: Select all
s='...'
sum=0
i=0
while i < len(s):
if s[i].isdigit():
sum += int(s[i])
i+=1
elif s[i] == 'x':
s = s[0:i]+s[i+1:len(s)]
i-=2
print sum
Last edited by Smee80 on Sun Nov 27, 2011 6:03 pm, edited 1 time in total.
my solution with visual c++
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
int l = 0;
int sum = 0;
int number = 0;
String ^test = "x";
String ^string2;
String ^string1 = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
Console::WriteLine(L"Problem Valuation:");
l = string1->Length;
for (int i=0; i < l; i++)
{
if (string1->Substring(i,1) == test)
{
string1 = string1->Remove(i,1);
l = string1->Length;
i -= 3;
}
else
{
string2 = string1->Substring(i,1);
number = Convert::ToInt32(string2);
sum += number;
}
}
Console::WriteLine();
Console::WriteLine("The sum is: "+sum);
Console::ReadLine();
return 0;
}
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
int l = 0;
int sum = 0;
int number = 0;
String ^test = "x";
String ^string2;
String ^string1 = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
Console::WriteLine(L"Problem Valuation:");
l = string1->Length;
for (int i=0; i < l; i++)
{
if (string1->Substring(i,1) == test)
{
string1 = string1->Remove(i,1);
l = string1->Length;
i -= 3;
}
else
{
string2 = string1->Substring(i,1);
number = Convert::ToInt32(string2);
sum += number;
}
}
Console::WriteLine();
Console::WriteLine("The sum is: "+sum);
Console::ReadLine();
return 0;
}
my c++ code
Code: Select all
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char str[] = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
int len = strlen(str);
int *flag = new int[len];
int i=0;
int sum = 0;
memset(flag, 0, len*sizeof(int));
while( i<len )
{
if (flag[i])
{
i++;
continue;
}
else if (str[i]>='0' && str[i]<='9')
{
sum += str[i]-'0';
i++;
}
else if (str[i]=='x')
{
flag[i] = 1;
i--;
while(flag[i]) i--;
i--;
while(flag[i]) i--;
}
}
printf("%d\n", sum);
}