## 10019 - Funny Encryption Method

All about problems in Volume 100. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

gix
New poster
Posts: 3
Joined: Sun Jun 30, 2002 1:35 am

### 10019 - Funny Encryption Method

I get a sigsev on this source:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int mypow (int b, int exp)
{
int res = b;

if (exp==0)
return 1;

while (exp>1)
{
res = res * b;
exp --;
}

return res;
}

int main (void)
{
#ifndef ONLINE_JUDGE
close (0); open ("10019.in", O_RDONLY);
close (1); open ("10019.out", O_WRONLY | O_CREAT, 0600);
#endif

int n, i;
int number[256], dimn;
char achar;
int X1, X2;
unsigned int b1, b2;

cin >> n;
scanf ("%c", &achar); /* skip new line */
while (n>0)
{
n--;
dimn=0;

scanf ("%c", &achar);

while (achar!='\n')
{
number[dimn] = achar - 48;
dimn++;
scanf("%c", &achar);
}

X1 = X2 = 0;
for (i=0; i<dimn; i++)
{
X1 += number * mypow(10, dimn-i-1);
X2 += number * mypow(16, dimn-i-1);
}

b1 = b2 = 0;
for (i=0; i<14; i++)
{
if ( ( X1 & (1<<i) ) == (1<<i) )
b1++;
if ( ( X2 & (1<<i) ) == (1<<i) )
b2++;
}

cout << b1 << " " << b2 << endl;
}
return 0;
}

the problem seem to be in these lines:

while (achar!='\n')
{
number[dimn] = achar - 48;
dimn++;
scanf("%c", &achar);
}

uhmmm but what is wrong on these lines ? ...
thanks for reading my orrible english!
good night
GiGi

Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:
you shouldn't mix C++ iostream with FILE i/o. input and output can be different, but don't use iostream and FILE for the same purpose.
one solution: replace all scanf("%c",..) with ...=cin.get().

hint1: i whould change the reading process to watch out for blank or eof
hint2: is 2^13 really the highest bit possible in X1, X2?

gix
New poster
Posts: 3
Joined: Sun Jun 30, 2002 1:35 am

### ok, solved!

thanks a lot, I suppose the problem was on blank or EOF checks ... Maybe I've to study iostream better
but why 14 bits wasn't too much?
if 0<M<9999 and 2^14 = 16384 I think 14 bits are enogh (ok 13 were few ... But when I try to submit my source with the following lines:

for (i=0; i<15; i++)
{
if ( ( X1 & (1<<i) ) == (1<<i) )
b1++;
if ( ( X2 & (1<<i) ) == (1<<i) )
b2++;
}

... I get a WA! doh!

Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:
65536 > 0x9999 > 32768

gix
New poster
Posts: 3
Joined: Sun Jun 30, 2002 1:35 am

### decimal or hexadecimal?

but the problem says:
"Each of the following N Lines ( 0<N<=1000) will contain the number M (0<M<=9999, in decimal representation)"

....
?????
mah
maybe I should try another hobby

10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:
Just quoted from the problem description (I think you should know also ):

[quote]1) Read the number N to encrypt M = 265
2) Interpret N as a decimal number X1= 265 (decimal)
3) Convert the decimal interpretation of N to its binary representation X1= 100001001 (binary)
4) Let b1 be equal to the number of 1

XeoNite
New poster
Posts: 1
Joined: Mon Oct 14, 2002 3:46 am
How am I supposed to deal with hexadecimals such as 2B2 or 3E9. Basicly any with letters. If I just convert them value by value to binary and count the amount of 1s I get the wrong answer.

And I have no idea how to do it any other way. Some hints/help, please and thanks.

Stumbled onto a solution by looking in a math guidebook. Didn't know that hex letters had a numeric equivelency.

Trinity
New poster
Posts: 12
Joined: Tue Jun 10, 2003 3:40 pm

### 10019 - Little help

People!

I`m getting a compiler error, just need a help w/ the output format

I`m working w/ java.

[/java]

The error is in that space <" ">
But the answer must come w/ 2 separate numbers (5 6)...
I got confused....

Could I get some help please?! I mean, Can`t I use spaces???

Thanx a lot
Trinity

zsepi
Learning poster
Posts: 51
Joined: Thu Sep 26, 2002 7:43 pm
Location: Easton, PA, USA
trinity,
I'd say that that line shouldn't be the reason why you get compiler error... check out the judge's info pages about JAVA, 'coz I believe they aren't using the sun java compiler, but the gcj - read more about JAVA support:
http://acm.uva.es/problemset/java.html
http://acm.uva.es/problemset/howtows.html#java
or you could send me your code in private, and I could have a look at it
cheers!
Dealing with failure is easy: Work hard to improve.
Success is also easy to handle: You've solved the wrong problem. Work hard to improve.

Trinity
New poster
Posts: 12
Joined: Tue Jun 10, 2003 3:40 pm

### Outputs?!

Guys!

I think I'm writting wrong my outputs...
I'm getting a compiler error in both programs a tried to submit, but I don't know what is...

program: 10019

[java]public static void main(String[] args)
{
FunnyCryption funnyCryp = new FunnyCryption();
int input = funnyCryp.input();
int dados[] = new int[input+1];
for (int i=1; i <= input; i++)
{
}
for (int i=1; i <= input; i++)
{
}
}
[/java]

ERROR
/tmp/ccAKmd2Omain.o: In function `main':
/tmp/ccSzJJ2nmain.i(.text+0x12): undefined reference to `_CL_4Main'
collect2: ld returned 1 exit status

and program: 10107
[java]/*x is the lastest array's position.
This method is called inside a loop that goes from the first to the last element of the input data
Example input = [4,3,5,1,7,2]. Is called with 0, then 1, then 2 (the position of the last element
that will be considered to find the median*/

if (x%2!=0)
{
num = Math.abs(x/2);
v1 = valor2[num];
v2 = valor2[num+1];
mediana = Math.abs((v1+v2)/2);
System.out.println(mediana);
}
else
{
num = (x/2);
{mediana = valor2[num];}
System.out.println(mediana);
}
[/java]

ERROR
/tmp/cc4xonPrmain.o: In function `main':
/tmp/ccYsGgxfmain.i(.text+0x12): undefined reference to `_CL_4Main'
collect2: ld returned 1 exit status

Does anyone knows what is???!!!

Thanx

Trinity

Trinity
New poster
Posts: 12
Joined: Tue Jun 10, 2003 3:40 pm
Another one guys!!!

I solve that one, was not about the space, I think it was going something wrong by e-mail, then I changed some things and worked

But now shows this error...

[java]public static void main(String[] args)
{
FunnyCryption funnyCryp = new FunnyCryption();
int input = funnyCryp.input();
int dados[] = new int[input+1];
for (int i=1; i <= input; i++)
{
}
for (int i=1; i <= input; i++)
{
int bin = funnyCryp.binario(dados);
System.out.println(bin+" "+hexa);
}
}[/java]

/tmp/ccAKmd2Omain.o: In function `main':
/tmp/ccSzJJ2nmain.i(.text+0x12): undefined reference to `_CL_4Main'
collect2: ld returned 1 exit status

The other program I tied to submit came w/ the same error...
I REALLY NEED THE HELP, PLEASE....

THANX

TRINITY

hectorzeta
New poster
Posts: 4
Joined: Mon Jun 23, 2003 1:54 am

### is your class named Main?

I thought I had the same problem and it was because the class must be named Main. And it could be the reason of get a compiler error.

Moni
Experienced poster
Posts: 202
Joined: Fri Mar 22, 2002 2:00 am
Location: Chittagong. CSE - CUET
Contact:
Hai! Are you meant binary() ???

Then tell me how the compiler will behave

Clear!
We are all in a circular way, no advances, only moving and moving!

r.z.
Learning poster
Posts: 56
Joined: Thu Jun 05, 2003 1:57 pm

### 10019 - Compile Error

Why is my code resulting compil error?
I run it with Borland C 3.1 and there's no warning or error message
can you tell me what's wrong?

[c]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int pangkat(int a);

void main()
{ int m,i,j,b1,b2,n,p1,p2,temp;
char a1[100],a2[100];
while(scanf("%d",&n)!=EOF)
{
for(j=0;j<n;j++)
{ b1=0;
b2=0;
scanf("%d",&m);
itoa(m,a1,2);
p1=strlen(a1);
for(i=0;i<p1;i++)
{ if(a1=='1')
b1+=1;
}
itoa(m,a2,10);
p2=strlen(a2);
temp=0;
for(i=0;i<p2;i++)
{ temp+=(a2-48)*pangkat(p2-1-i);
}
itoa(temp,a2,2);
p2=strlen(a2);
for(i=0;i<p2;i++)
{ if(a2=='1')
b2+=1;
}
printf("%d %d\n",b1,b2);
}
}
}

int pangkat(int a)
{ if(a==0) return 1;
if(a==1) return 16;
if(a==2) return 256;
if(a==3) return 4096;
return -1;
}
[/c]
thanks

asif_rahman0
Experienced poster
Posts: 209
Joined: Sun Jan 16, 2005 6:22 pm

### 10019

Hello,I can't understand the problem 10019.Actually,what to do.So,anybody plz help me to understand the problem.