滴水逆向联盟
标题:
基于visual Studio2013解决C语言竞赛题之1006填空
[打印本页]
作者:
大灰狼
时间:
2014-9-3 09:16
标题:
基于visual Studio2013解决C语言竞赛题之1006填空
题目
download.png
(67.8 KB, 下载次数: 366)
下载附件
保存到相册
2014-9-3 09:16 上传
解决代码及点评
<pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre class="cpp" name="code" code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre></pre></pre>
[cpp]
view plain
copy
<pre code_snippet_id="91880" snippet_file_name="blog_20131209_3_249098" name="code" class="cpp">/************************************************************************/
/*6. 将一个6×6数组的第一行、第六行、主对角线和副对角线上的元素都存入1,
其余的元素都存入-1。请编程序实现。
*/
/************************************************************************/
#include <stdio.h>
#include <stdlib.h>
void main()
{
int arr[6][6]={0};
// 用两重循环遍历所有的元素
for (int i=0;i<6;i++)
{
for (int j=0;j<6;j++)
{
if (i==0) // i==0则是第一行
{
arr
[j]=1;
}
else if (i==5) // i==5则是最后一行
{
arr
[j]=1;
}
else if (i==j) // i==j则是主对角线
{
arr
[j]=1;
}
else if ((i+j)==5) // i+j == 5则是副对角线
{
arr
[j]=1;
}
else
arr
[j]=-1; // 其他情况则是-1
}
}
// 用一个双重循环去打印,同学们也可以考虑,一边赋值一边打印的效果
// 这里为了便于学习,把打印放在另外一个循环里
for (int i=0;i<6;i++)
{
for (int j=0;j<6;j++)
{
printf("%3d ",arr
[j]);
}
printf("\n");
}
system("pause");
}</pre><br>
欢迎光临 滴水逆向联盟 (http://dtdebug.com/)
Powered by Discuz! X3.2