(1)以“@”为字符,打印一个正三角形。 |
-
近期文章
近期评论
- coolker 发表在《打造最快的Hash表》
- struggle 发表在《提供C语言教学课件(适用于初学者)》
- zhanghaibo 发表在《提供C语言教学课件(适用于初学者)》
- zhanghaibo 发表在《提供C语言教学课件(适用于初学者)》
- diys 发表在《C语言编程宝典(王大刚) 1.1 C 语言的产生与发展》
文章归档
- 2022 年十月
- 2014 年一月
- 2013 年十二月
- 2012 年十一月
- 2012 年七月
- 2012 年六月
- 2012 年五月
- 2012 年四月
- 2012 年三月
- 2012 年二月
- 2011 年十二月
- 2011 年十月
- 2011 年九月
- 2011 年八月
- 2011 年七月
- 2011 年六月
- 2011 年五月
- 2011 年四月
- 2011 年三月
- 2011 年二月
- 2011 年一月
- 2010 年十二月
- 2010 年十一月
- 2010 年十月
- 2010 年九月
- 2010 年八月
- 2010 年七月
- 2010 年六月
- 2010 年五月
- 2010 年四月
- 2010 年三月
- 2010 年二月
- 2010 年一月
- 2009 年十二月
- 2009 年十一月
- 2009 年十月
- 2009 年九月
- 2009 年八月
- 2009 年七月
- 2009 年六月
- 2009 年五月
- 2009 年四月
- 2009 年三月
- 2009 年二月
- 2009 年一月
- 2008 年十二月
- 2008 年十一月
- 2008 年十月
- 2008 年九月
- 2008 年八月
- 2008 年七月
- 2008 年六月
- 2008 年五月
- 2008 年四月
- 2008 年三月
- 2008 年二月
- 2008 年一月
- 2007 年十二月
- 2007 年十一月
- 2007 年十月
- 2007 年九月
- 2007 年八月
- 2007 年七月
- 2007 年六月
- 2007 年三月
- 2007 年二月
- 2007 年一月
- 2006 年十二月
- 2006 年十一月
- 2006 年十月
- 2006 年九月
- 2006 年八月
- 2006 年七月
- 2006 年六月
- 2006 年五月
- 2006 年四月
- 2006 年三月
- 2006 年二月
- 2006 年一月
- 2005 年十二月
- 2005 年十一月
分类目录
功能

1.文本模式下不可能打印一个标准的正三角形,顶多打印一个近似的正三角形
int row = 10;//边的长度,可以在程序中定义,也可以让用户输入
int i = 0;
int j = 0;
for (i=0; i<row; i++)
{
for (j=(row – i -1); j>0; j–)
{
cout<<’ ‘;
}
for (j=0; j<=i; j++)
{
cout<<”@ “;
}
cout<<endl;
}
2.
int matrix[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int i = 0;//行下标
int j = 0;//列下标
int sum = 0;//结果
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
if ( (i+j)%2 == 0 )
{
sum += matrix[i][j];
}
}
}
cout << sum << endl;
3.递归
int f(int x)
{
if (x == 1)
{
return 1;
}
else
{
return ((int)pow(-1, x+1) * x) + f(x – 1) ;
}
}
4.
char * getalpha(char ch[])
{
char *rst = new char[strlen(ch)];
int pos = 0;
int i = 0;
for (i=0; i < (int)strlen(ch); i++)
{
if ((ch[i]>65 && ch[i]<90) || (ch[i]>97 && ch[i]<122))
{
rst[pos] = ch[i];
pos++;
}
}
rst[pos] = ‘\0′;
return rst;
}
5.
void getalpha(char ch[])
{
char *rst = new char[strlen(ch)];
int pos = 0;
int i = 0;
for (i=0; i < (int)strlen(ch); i++)
{
if (ch[i]<48 || ch[i]>57)
{
rst[pos] = ch[i];
pos++;
}
}
rst[pos] = ‘\0′;
strcpy(ch,rst);
}
6.
void getdigit(char *des,char *ch)
{
int pos = 0;
int i = 0;
for (i=0; i < (int)strlen(ch); i++)
{
if (ch[i]>=48 && ch[i]<=57)
{
des[pos] = ch[i];
pos++;
}
}
des[pos] = ‘\0′;
}
7.
void add(int a[5][5] , int b[5])
{
int max = 0;//存放最大值
int i = 0;
int j = 0;
for (i=0; i<5; i++)
{
max = a[i][0];
for (j=0; j<5; j++)
{
if (a[i][j] > max)
{
max = a[i][j];
}
}
b[i] = max;
}
}
1、同ningweidong
2、同ningweidong
3、可以找到规律,这样速度会快,而且没有限制。(递规有层数限制的)
[code]
int foo( int nNum )
{
if ( 0 == ( nNum % 2 ) ) {
/* nNum为偶数 */
return -nNum / 2;
} else {
/ * nNum为齐数 */
return (nNum + 1) / 2;
}
}
[/code]
或
[code]
int foo( int nNum )
{
return ( nNum % 2 ) ? ( (nNum + 1) / 2 ) : ( -nNum / 2 );
}
[/code]
4、虽然可以在函数内部分配内存,不过这样不推荐。
(当然如果有配对的函数则可以考虑这样写。)
推荐改为(在函数外部准备空间):
[code]
char *GetAlpha( const char *strInput, char *strOutput )
{
/* 判断数据有效性 */
_ASSERT( ((const char *)NULL != strInput) && ((char *)NULL != strOutput ) );
const char *strTmpInput = strInput;
char *strTmpOutput = strOutput;
while ( '\0' != *strTmpInput ) {
if ( (('a' <= *strTmpInput) && ('z' >= *strTmpInput)) ||
(('A' <= *strTmpInput) && ('Z' >= *strTmpInput)) )
{
*strTmpOutput = *strTmpInput;
strTmpOutput++;
}
strTmpInput++;
}
/* '\0' 结尾 */
*strTmpOutput = '\0';
return strOutput;
}
[/code]
5、理由同上
[code]
char *GetAlpha( const char *strInput, char *strOutput )
{
/* 判断数据有效性 */
_ASSERT( ((const char *)NULL != strInput) && ((char *)NULL != strOutput ) );
const char *strTmpInput = strInput;
char *strTmpOutput = strOutput;
while ( '\0' != *strTmpInput ) {
if ( (('0' > *strTmpInput) && ('9' < *strTmpInput)) ) {
*strTmpOutput = *strTmpInput;
strTmpOutput++;
}
strTmpInput++;
}
/* '\0' 结尾 */
*strTmpOutput = '\0';
return strOutput;
}
[/code]
6、7、基本同ningweidong的
3.
int n,i,sum=0;
int flag=1;
for(i=1;i<=n;i++)
{ sum=sum+flag*i;
flag=-flag;
}
见有人这样做~~~感觉不错~~其他不熟悉就不发言了!!
8错8错
4题
#include<stdio.h>
void main()
{
char ch;
loop:scanf(“%c”,&ch);
if(ch>64&&ch<91||ch>96&&ch<123)
printf(“%c”,ch);
goto loop;
}
这些要好好考虑啊