第一个C语言程序。
其实以前断断续续都学过一些C语言,但是除了printf 还真没几个记得住的。
这次下决心从基础搞起,看看以前的视频教程,看看书,感觉还是一头雾水,于是决定从做题开始。
选了一本《C语言名题精选百题技巧》,今天开始做第一题:
数组的最长平台: 一个随机的数组,每个元素0~10之间,整型。然后按照从小到大排列。
选出最长的那个数字。
于是开始写:
数组: a[i]
数组长度 sizeof(a)/sizeof(int)
随机:RAND()
从小到大排列: a[i]>a[j] 则 temp=a[i],a[i]=a[j],a[j]=temp;
最后……我去,怎么数数组的个数,容我仔细想想?
先把前面写好的代码贴出来吧:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int a[100],aa;
int i;
int count = 0;
srand((unsigned)time(NULL));
int j;
int temp;
int len;
len=sizeof(a)/sizeof(int);
for (i=0;i<100;i++)
{
aa=rand()%10+1;
a[i]=aa;
}
for (i=0;i<len-1;i++)
{
for(j=i;j<len;j++)
{
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for (i=0;i<100;i++)
{
printf("%-1d",a[i]);
count++;
if (count == 10)
{
printf("\n");
count = 0;
}
}
system("pause");
return 0;
}
1、思路:定义11个变量:a[i]=0时,第一个变量++,a[i]=2时 第二个变量累加,以此类推。
那如果不知道变量的值是多少呢?另外这个方法变量太多了。
2.、思路2:定义3个变量:a[i]=a[i+1]时,第一个变量++,遍历完了,把第一个变量的值付给第二个变量,a[i]付给第三个变量
遍历第二次,,这次还是重复上一次的遍历,把第一个变量的值和第二个变量的值进行比较,如果第一个大于第二个,则第三个变量等于a[i] 否则a[i]还等于原来的值
遍历完。打印第三个变量以及 第二个变量。就是那个数字,并且有多少个。
3、最长平台数只有一个吗?有可能不止,我随机了1~9 的100个数组,如果7和9 都是19个怎么办?
错误代码示范:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int a[100],aa;
int i;
int count = 0;
int longest =0;
int b=0;
int c=0;
srand((unsigned)time(NULL));
int j;
int temp;
int len;
len=sizeof(a)/sizeof(int);
//生成随机数组
for (i=0;i<100;i++)
{
aa=rand()%9+1;
a[i]=aa;
}
//数组排序
for (i=0;i<len-1;i++)
{
for(j=i;j<len;j++)
{
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
//数组输出,每隔10个换一行。
for (i=0;i<100;i++)
{
printf("%-1d",a[i]);
count++;
if (count == 10)
{
printf("\n");
count = 0;
}
}
//求最长平台
for (i=0;i<100;i++)
{
if (a[i]=a[i+1])
{
longest++;
}
else
{
i++;
longest=0;
}
if (c<longest)
{
c=longest;
b=i-1;
}
}
printf("the longest number is %d and the number of it is %d\n", a[b],c) ;
system("pause");
return 0;
}
想了一晚上,还是想不出来。于是只好查答案……然后让答案打脸……
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int a[100],aa;
int i;
int count = 0;
int longest =0;
int b=0;
int c=0;
int j;
int temp;
int len;
int longest_plateau(int x[],int n)
{
int length = 1;
int i;
for(i = 1;i < n;i++)
{
if(x[i] == x[i - length])
length ++;
}
return length;
}
int main()
{
srand((unsigned)time(NULL));
len=sizeof(a)/sizeof(int);
//生成随机数组
for (i=0;i<100;i++)
{
aa=rand()%9+1;
a[i]=aa;
}
//数组排序
for (i=0;i<len-1;i++)
{
for(j=i;j<len;j++)
{
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
//数组输出,每隔10个换一行。
for (i=0;i<100;i++)
{
printf("%-1d",a[i]);
count++;
if (count == 10)
{
printf("\n");
count = 0;
}
}
//求最长平台
int length = longest_plateau(a,100);
printf("The longest_plateau is %d\n",length);
system("pause");
return 0;
}
这个代码有个问题是,我想要知道是哪个数字最长,结果就是查不到,继续在这个基础上努力改代码。
不过这个
if(x[i] == x[i - length])
length ++;
是真的巧妙,不管最大的是大几,只要打就加1就可以了,加到最后肯定是最大值……以下是运行结果:
下面思考下,怎么把最大的那个数打印出来。
最后在我冥思苦想了半个小时……娃哈哈,终于打印出来了:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int a[100],aa;
int i;
int count = 0;
int longest =0;
int b=0;
int c=0;
int j;
int temp;
int len;
int longest_plateau(int x[],int n)
{
int length = 1;
int i;
for(i = 1;i < n;i++)
{
if(x[i] == x[i - length])
{
b=i-length;
length ++;
}
}
return length;
}
int main()
{
srand((unsigned)time(NULL));
len=sizeof(a)/sizeof(int);
//生成随机数组
for (i=0;i<100;i++)
{
aa=rand()%9+1;
a[i]=aa;
}
//数组排序
for (i=0;i<len-1;i++)
{
for(j=i;j<len;j++)
{
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
//数组输出,每隔10个换一行。
for (i=0;i<100;i++)
{
printf("%-1d",a[i]);
count++;
if (count == 10)
{
printf("\n");
count = 0;
}
}
//求最长平台
int length = longest_plateau(a,100);
printf("The longest_plateau is %d the longest number is %d\n",length,a[b]);
system("pause");
return 0;
}
起始累加的是长度,只要a[i]=a[i-length]的长度有增加,则length 就增加一个,那么最后一个增加的值,就是最长的那个数字,把这个a[i-length] 记录下来就可以了。
运行结果:
下面再思考一下,如果有2个平台等长怎么办?就像这个随机数组,如果2和7 都是最长的怎么办?
我的天啊~我真是第一次这么写代码……功能再增加下去,是不是就可以做成小游戏了……