How to print in the console that figure? - eclipse

I want to print that in the console:
*
**
***
**
*
so, my code is:
Scanner input = new Scanner(System.in);
int n = input.nextInt();
char c = '*';
if (1 < n && n < 20) {
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(c);
}
System.out.println();
}
any proposals how to finish?

You want to print "* **". Write this:
System.out.print("* **");
If my answer is not that what you excpected, than give us more information. What is your actual problem. See StackOverflowFAQ

If n is your "*" length,below code:
if (1 < n && n < 20) {
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(c);
}
System.out.println();
}
for (int row =1; row <= n; row++) {
for (int col = n-row; col >0 ; col--) {
System.out.print(c);
}
System.out.println();
}
}

Related

How to create a layout that all its cells are initially empty and accessible by moving agents (such as equipment)?

In Copper Nickel Mine (Cloud) Simulation, In MinePanel Agent, there is a function Called setupTunnelLayout.
The original code in the above function is as following:
//create corridor of already empty rooms
`RoomBlock emptyRoom;
for ( int j = 0; j < nColumns; j++ ) {
emptyRoom = add_roomBlocks();
emptyRoom.jumpToCell( 0, j );
if ( j == nColumns - 1 )
emptyRoom.isStartBlock = true;
emptyRoom.isTunnel = true;
}`
But in my scenario all the cells are accessible initially, so all can be tunnel (path to move), not only the (0, j) row as the above example!
I was thinking I can change it as the followings; (1) or (2);
(1)
//create corridor of already empty rooms
RoomBlock emptyRoom;
for ( int j = 0; j < nColumns; j++ )
for (int i = 0; i < nRows; i++){
emptyRoom = add_roomBlocks();
emptyRoom.jumpToCell( 0, j );
emptyRoom.jumpToCell( i, 0 );
if ( j == nColumns - 1 )
emptyRoom.isStartBlock = true;
emptyRoom.isTunnel = true;
}
Or it can be like this;
(2)
//create corridor of already empty rooms
`RoomBlock emptyRoom;
for ( int j = 0; j < nColumns; j++ )
for (int i = 0; i < nRows; i++){
emptyRoom = add_roomBlocks();
emptyRoom.jumpToCell( i, j );
if ( j == nColumns - 1 )
emptyRoom.isStartBlock = true;
emptyRoom.isTunnel = true;
}`
'Can you please let me know if (1) or (2) are correct? Which one is preferred?
Thank you so much,
Neda.'
The second is preferable; there is no point jumping the agent to the column and then the row when you can directly move it to the exact cell coordinates.
In both cases though you are missing a closing } for your extra i loop (and you're missing an opening one in your second case).
//create corridor of already empty rooms
RoomBlock emptyRoom;
for (int j = 0; j < nColumns; j++) {
for (int i = 0; i < nRows; i++) {
emptyRoom = add_roomBlocks();
emptyRoom.jumpToCell(i, j);
if (j == nColumns - 1) {
emptyRoom.isStartBlock = true;
}
emptyRoom.isTunnel = true;
}
}
[I used the StackOverflow formatting to mark the full block as Java code. It's also best-practice (although AnyLogic doesn't do it many of their example models) to always use curly brackets for loops, etc. even if their body only has one line.]
Plus whether the change will do what you want or not obviously depends on the rest of the model and how it handles the grid of cells and tunnels.

Why .hash section in my ELF file is not valid?

In a .hash section, for some x, if chain[x] != SHN_UNDEF,
it should hold hash(name(bucket[x])) === hash(name(bucket[chain[x]])) % nbucket
But why it's not the case for my shared object file?
For example, name(bucket[224]) == "_ZN9VADEnergyD0Ev" whose (ELF hash % nbucket) is 224,
name(bucket[8]) == "speex_bits_write_whole_bytes" whose (ELF hash % nbucket) is 8,
but chain[224] == 8.
(the file is avalible here)
Or my code for reading elf is wrong?
nbucket = ((int *)hash)[0];
nchain = ((int *)hash)[1];
memcpy(bucket, hash + 8, nbucket * 4);
memcpy(succ, hash + nbucket * 4 + 8, nchain * 4);
for (i = 0; i < nbucket; i++) {
printf("%d %d\n", bucket[i], succ[i]);
if (bucket[i] && succ[i])
pred[succ[i]] = i;
}
printf("%d %d\n", nbucket, nchain);
#define sym_name(x, symtbl, strtbl) (strtbl + symtbl[x].st_name)
for (i = 0; i < nbucket; i++) {
if (pred[i] == 0) {
printf("=======\n");
for (j = i; j; j = succ[j]) {
char *sname = sym_name(bucket[j], dynsym, dynstr);
printf("%d,succ=%d ", j, succ[j]);
printf("%d:%s\n", _dl_elf_hash(sname) % nbucket, sname);
}
}
}
It's my fault. It should be
hash(name(bucket[x])) === hash(name(chain[bucket[x]])) % nbucket
and
nbucket = ((int *)hash)[0];
nchain = ((int *)hash)[1];
memcpy(bucket, hash + 8, nbucket * 4);
memcpy(succ, hash + nbucket * 4 + 8, nchain * 4);
for (i = 0; i < nbucket; i++) {
printf("%d %d\n", bucket[i], succ[i]);
if (bucket[i] && succ[i])
pred[succ[i]] = i;
}
printf("%d %d\n", nbucket, nchain);
#define sym_name(x, symtbl, strtbl) (strtbl + symtbl[x].st_name)
for (i = 0; i < nbucket; i++) {
printf("=======\n");
for (j = bucket[i]; j; j = succ[j]) {
char *sname = sym_name(j, dynsym, dynstr);
printf("%d,succ=%d ", j, succ[j]);
printf("%d:%s\n", _dl_elf_hash(sname) % nbucket, sname);
}
}

Simple cryptographic puzzle [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm looking for a way to solve this crypt arithmetic problem of:
ROBERT + GERALD = DONALD
and potentially others as well, where each letter represents a digit.
How would you go about solving this by hand and how does that relate to solving it programmatically?
Thank you in advance
You can actually work this out as a sum:
robert
+ gerald
------
= donald
and use basic mathematical knowledge.
For example, there's no carry possible in the right column (6) and we have T + D = D. That means T must be zero.
Similarly, for column 5, there's no carry from column 6 and R + L = L means R is zero as well, and no carry to column 4.
Same with column 4, E + A = A so E is zero.
So we now have:
0ob000
+ g00ald
------
= donald
From there, we can infer from columns 3 and 1 that b==n and g==d and they (along with o/a/l/d) can be any value since every digit is being added to zero so there is no chance of carry anywhere. So let's just make them all one:
011000
+ 100111
------
= 111111
In fact, you could make them all zero and end up with 000000 + 000000 = 000000.
But that's hardly programming related, so let's make it so:
#include <stdio.h>
int main (void) {
int robert, gerald, donald;
for (int r = 0; r < 10; r++) {
for (int o = 0; o < 10; o++) {
for (int b = 0; b < 10; b++) {
for (int e = 0; e < 10; e++) {
for (int t = 0; t < 10; t++) {
for (int g = 0; g < 10; g++) {
for (int a = 0; a < 10; a++) {
for (int l = 0; l < 10; l++) {
for (int d = 0; d < 10; d++) {
for (int n = 0; n < 10; n++) {
robert = r * 100000 + o * 10000 + b * 1000 + e * 100 + r * 10 + t;
gerald = g * 100000 + e * 10000 + r * 1000 + a * 100 + l * 10 + d;
donald = d * 100000 + o * 10000 + n * 1000 + a * 100 + l * 10 + d;
if (robert + gerald == donald) {
printf (" %06d\n", robert);
printf ("+ %06d\n", gerald);
printf (" ------\n");
printf ("= %06d\n", donald);
printf ("........\n");
}
}
}
}
}
}
}
}
}
}
}
return 0;
}
That will give you a whole host of solutions.
And, before you complain that you cannot have repeated digits, there is no solution if that's the case, since mathematically both T and R must be zero, as shown in the original reasoning above. And you can prove this empirically with:
#include <stdio.h>
int main (void) {
int robert, gerald, donald;
for (int r = 0; r < 10; r++) {
for (int o = 0; o < 10; o++) {
if (o==r) continue;
for (int b = 0; b < 10; b++) {
if ((b==r) || (b==o)) continue;
for (int e = 0; e < 10; e++) {
if ((e==r) || (e==o) || (e==b)) continue;
for (int t = 0; t < 10; t++) {
if ((t==r) || (t==o) || (t==b) || (t==e)) continue;
for (int g = 0; g < 10; g++) {
if ((g==r) || (g==o) || (g==b) || (g==e) || (g==t)) continue;
for (int a = 0; a < 10; a++) {
if ((a==r) || (a==o) || (a==b) || (a==e) || (a==t) || (a==g)) continue;
for (int l = 0; l < 10; l++) {
if ((l==r) || (l==o) || (l==b) || (l==e) || (l==t) || (l==g) || (l==a)) continue;
for (int d = 0; d < 10; d++) {
if ((d==r) || (d==o) || (d==b) || (d==e) || (d==t) || (d==g) || (d==a) || (d==l)) continue;
for (int n = 0; n < 10; n++) {
if ((n==r) || (n==o) || (n==b) || (n==e) || (n==t) || (n==g) || (n==a) || (n==l) || (n==d)) continue;
robert = r * 100000 + o * 10000 + b * 1000 + e * 100 + r * 10 + t;
gerald = g * 100000 + e * 10000 + r * 1000 + a * 100 + l * 10 + d;
donald = d * 100000 + o * 10000 + n * 1000 + a * 100 + l * 10 + d;
if (robert + gerald == donald) {
printf (" %06d\n", robert);
printf ("+ %06d\n", gerald);
printf (" ------\n");
printf ("= %06d\n", donald);
printf ("........\n");
}
}
}
}
}
}
}
}
}
}
}
return 0;
}
which outputs no solutions.
Now DONALD + GERALD = ROBERT, that's a different matter but you can solve that simply by modifying the code above slightly, making the if statement into:
if (donald + gerald == robert) {
printf (" %06d\n", donald);
printf ("+ %06d\n", gerald);
printf (" ------\n");
printf ("= %06d\n", robert);
printf ("........\n");
}
and you get the single solution:
526485
+ 197485
------
= 723970

coffeescript for in bug?

On coffeescript loop 'for'
eg.
if 1 < x, code like below:
console.debug i for i in [1..0]
Generated code is:
var i;
for (i = 1; i >= 0; i--) {
console.debug(i);
}
if 1 > x,code like below:
console.debug i for i in [1..2]
Generated code is:
var i;
for (i = 1; i <= 2; i++) {
console.debug(i);
}
If i want write that javascript.How to ?
for(var i=1;i<=0;i++){
console.debug(i);
}
Because i don't know the condition is greater than left side or less than left side.
But i just want it i++
What's wrong with me?
EDIT BELOW:
For coffeescript's feature,I add condition before the loop or add condition on for loop.
eg:
if x - y >=1
console.debug i for i in [1..x-y]
or
console.debug i for i in [1..x-y] and x-y >=1
That's my way.Some one have good advice?
It looks like you want to do this:
console.debug i for i in [1..x-y] by 1
Which gets compiled to:
var i, _i, _ref;
for (i = _i = 1, _ref = x - y; _i <= _ref; i = _i += 1) {
console.debug(i);
}
for(var i=1;i<=0;i++){
console.debug(i);
}
is equivalent to
var i = 1;
while(true) {
console.debug(i);
i++;
}
which in coffeescript is written as
i = 1
while true
console.debug(i);
i++;

Memory Access Exceedingly Slow For a Simple Loop Through an Array

I am taking about 50 times as long as expected to loop through a simple assignment. My first reaction was that I had disordered my memory access in the arrays, resulting in cache misses. This doesn't seem the case, however.
The pixel value assignment and updating the arrays takes a dogs age. Do any one of you folks have an inclining as to why this is happening? (I am compiling for an iPod with an A4)
memset(columnSumsCurrentFrameA, 0, sizeof(unsigned int) * (_validImageWidth/numSubdivisions) );
memset(rowSumsCurrentFrameA, 0, sizeof(unsigned int) * (_validImageHeight/numSubdivisions) );
int pixelValue = 0;
int startingRow = 0;
int startingColumn = 0;
for (int i = 0; i < _validImageHeight/numSubdivisions; i++)
{
int index = (i + startingRow) * _imageWidth;
for( int j = 0; j < (_validImageWidth/numSubdivisions); j++)
{
pixelValue = imageData[index + startingColumn + j];
columnSumsCurrentFrameA[j] += pixelValue;
rowSumsCurrentFrameA[i] += pixelValue;
}
}
The result of _validImageWidth/numSubdivisions must be an integer, are you sure that is always the case?
Also, you should calculate _validImageWidth/numSubdivisions before entering the double loops, it's not safe to assume your compiler takes care of it.
int limit = _validImageHeight/numSubdivisions;
for (int i = 0; i < limit; i++)
{
int index = (i + startingRow) * _imageWidth;
for( int j = 0; j < limit; j++)
{
pixelValue = imageData[index + startingColumn + j];
columnSumsCurrentFrameA[j] += pixelValue;
rowSumsCurrentFrameA[i] += pixelValue;
}
}