 |
help |
 |
23.06.2003, 03:24
|
#1
|
Дошкольник
Join Date: 04 2003
Location: USA
Posts: 103
Rep Power: 0
|
help
Guys please help.I need to write a program that inputs an integer and a character, the output of the program should be a diamond composed of character and extending the width specified by the integer.For example, if the integer is 7 and character is ' * ' , the diamond would look like this:
---- *------
---***-----
--*****----
-*******---
--*****---
---***-----
---- *------
[ -- ] is here just to make pic clear.
If the input integer is an even number , it should be increased to the next odd number.
I have to use while loop, cant use for . It must be easy for u
Any suggestions?
__________________
No se preocupe, sea feliz !
|
|
|
23.06.2003, 04:55
|
#2
|
Грустно...
Join Date: 08 2002
Location: Там, где всегда идут дожди
Age: 43
Posts: 21,717
Rep Power: 9
|
PHP Code:
int main(int argc, char* argv[])
{
int iCount;
std::cin >> iCount;
iCount & 0x01 ? iCount : ++iCount;
int i = 1, j, k = (iCount >> 1) + 1, l;
while (k--)
{
j = (iCount - i) >> 1;
while (j--)
std::cout << ' ';
l = i;
while (l--)
std::cout << '*';
j = (iCount - i) >> 1;
while (j--)
std::cout << ' ';
i += 2;
std::cout << std::endl;
}
k = iCount >> 1;
i = iCount - 2;
while (k--)
{
j = (iCount - i) >> 1;
while (j--)
std::cout << ' ';
l = i;
while (l--)
std::cout << '*';
j = (iCount - i) >> 1;
while (j--)
std::cout << ' ';
i -= 2;
std::cout << std::endl;
}
////
return 0;
}
|
|
|
23.06.2003, 05:01
|
#3
|
Грустно...
Join Date: 08 2002
Location: Там, где всегда идут дожди
Age: 43
Posts: 21,717
Rep Power: 9
|
Если нужны будут комментарии - то пожалуйста.
Ее можно соптимизировать.
i.e. не вычислять 2 раза (iCount - 1) shr 1 и т.д. и т.п.
|
|
|
 |
|
 |
23.06.2003, 08:22
|
#4
|
VIP Faraon
Join Date: 09 2002
Location: Пески сахары
Age: 43
Posts: 2,895
Rep Power: 0
|
Quote:
Originally posted by Agregat
PHP Code:
int main(int argc, char* argv[])
{
int iCount;
std::cin >> iCount;
iCount & 0x01 ? iCount : ++iCount;
int i = 1, j, k = (iCount >> 1) + 1, l;
while (k--)
{
j = (iCount - i) >> 1;
while (j--)
std::cout << ' ';
l = i;
while (l--)
std::cout << '*';
j = (iCount - i) >> 1;
while (j--)
std::cout << ' ';
i += 2;
std::cout << std::endl;
}
k = iCount >> 1;
i = iCount - 2;
while (k--)
{
j = (iCount - i) >> 1;
while (j--)
std::cout << ' ';
l = i;
while (l--)
std::cout << '*';
j = (iCount - i) >> 1;
while (j--)
std::cout << ' ';
i -= 2;
std::cout << std::endl;
}
////
return 0;
}
|
хе-хе... Я тут ее немного соптимизировал  , и она стала работать быстрее на треть, и размер уменьшился
Ты че туда столь циклов понапихал ?
кстати, для сравнения времени использовал функцию clock()
смотри
PHP Code:
int main(int argc, char* argv[])
{
int iCount;
cin >> iCount;
iCount & 0x01 ? iCount : ++iCount;
int k = (iCount >> 1) + 1, nStars = 0;
char* pch = new char[iCount + 1];
pch[iCount] = 0;
memset(pch, ' ', iCount);
for(int i = 0; i < k; ++i)
{
nStars = i * 2 + 1;
memset(pch + (iCount - nStars)/2, '*', nStars);
cout << pch << endl;
}
i-= 2;
memset(pch, '*', iCount);
int nSpaces = 0;
for( ; i >= 0; --i )
{
nStars = i * 2 + 1;
nSpaces = (iCount - nStars)/2;
memset(pch, ' ', nSpaces);
memset(pch + (iCount + nStars)/2, ' ', (iCount - nStars)/2);
cout << pch << endl;
}
////
return 0;
}
__________________
Храни тебя Господь в сухом и прохладном месте...
|
|
|
 |
23.06.2003, 08:27
|
#5
|
Грустно...
Join Date: 08 2002
Location: Там, где всегда идут дожди
Age: 43
Posts: 21,717
Rep Power: 9
|
Извиняюсь, конечно, но вы мудак!
Программу надо было написать только циклами while... прочти условие!
|
|
|
23.06.2003, 08:30
|
#6
|
VIP Faraon
Join Date: 09 2002
Location: Пески сахары
Age: 43
Posts: 2,895
Rep Power: 0
|
Quote:
Originally posted by Agregat
Извиняюсь, конечно, но вы мудак!
Программу надо было написать только циклами while... прочти условие!
|
Это не оправдывает 3 вложенных цикла
а циклы for в моем примере можно ЛЕГКО заменить на while ..
Так что господин хороший, не выпендривайтесь
__________________
Храни тебя Господь в сухом и прохладном месте...
|
|
|
23.06.2003, 08:32
|
#7
|
Грустно...
Join Date: 08 2002
Location: Там, где всегда идут дожди
Age: 43
Posts: 21,717
Rep Power: 9
|
Я, отнюдь, не выпендриваюсь...
|
|
|
 |
|
 |
24.06.2003, 01:16
|
#8
|
Дошкольник
Join Date: 04 2003
Location: USA
Posts: 103
Rep Power: 0
|
Rebyata spasibo bolshoe , tolko ne nado soritsya izza etgo.
ya programku napisala , tolko nemnogo uproshenee , tak kak eto nachalo kursa , tut nado pisat prostenko , a to uchitel ne poverit chto ya ee sama napisala. dlya vas ona naverno budet ochen uj"detskoi", no dlya menay ne ochen.
int main()
{
int num, counter, number, count;
char user;
cout << "Input the character you wish to use>";
cin >> user;
cout << "Enter the height and width>";
cin >> num;
if(num % 2 == 0)
num += 1;
number = num;
counter = 1;
count = 1;//number of characters
while(counter > 0)
{
if(counter != num)
{
cout << setw(number) << " ";
while(count <= counter)
{
cout << user;
count++;
}
cout << endl;
counter += 2;
number -= 1;
}
count = 1;
if(counter == num)
{
cout << setw(number) << " ";
while(count <= counter)
{
cout << user;
count++;
}
cout << endl;
num -= 2;
counter -= 2;
number += 1;
}
}
return 0;
}
Eshe raz spasibo za pomosh
__________________
No se preocupe, sea feliz !
|
|
|
 |
24.06.2003, 04:23
|
#9
|
Грустно...
Join Date: 08 2002
Location: Там, где всегда идут дожди
Age: 43
Posts: 21,717
Rep Power: 9
|
Пожалуйста
|
|
|
All times are GMT. The time now is 17:47. |
|
|