由于这些代码也是我初学时写的代码,故其中的规范程度及简洁程度并不很好(此处我后来写的有可以参考一下->C语言代码规范),但是能很好的接近出初学者的水平,也更有参考价值!排版不易,喜欢就点个赞吧!如有问题,请勿吐槽,欢迎留言互相学习。
第11周编程题在线测试
代码实现
#include
#include
long Fun(long M, long T, long U, long F, long D, char str[]);
int main()
{
long m,t,u,f,d,q;
char str[1000];
printf("Input M,T,U,F,D:");
scanf("%ld%ld%ld%ld%ld",&m,&t,&u,&f,&d);
printf("Input conditions of road:");
scanf("%s",str);
q=Fun(m,t,u,f,d,str);
printf("num=%ld\n",q);
return 0;
}
long Fun(long M, long T, long U, long F, long D, char str[])
{
long s=0,i;
for(i=0;i<T;i++)
{
if(str[i]=='f')
{
s+=F+F;
}
else
{
s+=D+U;
}
if(s>=M)
{
return i;
}
}
return i-1;
}
代码实现
#include
#include
void Seperate(int a[], int n);
int main()
{
int n,a[100],i;
printf("Input n:");
scanf("%d",&n);
printf("Input numbers:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
Seperate(a,n);
return 0;
}
void Seperate(int a[], int n)
{
int b[100],c[100],i,y,t;
for(y=i=t=0;y<n;y++)
{
if(a[y]%2==0)
{
b[i]=a[y];
i++;
}
else
{
c[t]=a[y];
t++;
}
}
printf("%d",c[0]);
for(y=1;y<t;y++)
{
printf(",%d",c[y]);
}
printf("\n%d",b[0]);
for(y=1;y<i;y++)
{
printf(",%d",b[y]);
}
}
代码实现
#include
#include
int SearchString(char s[], char d[]);
int main()
{
char a[80],b[80];
int c;
printf("Input the first string:");
gets(a);
printf("Input the second string:");
gets(b);
c=SearchString(a,b);
if(c==0)
{
printf( "No\n");
}
else
{
printf("Yes\n");
}
return 0;
}
int SearchString(char s[], char d[])
{
int a,b,c,e,f,q;
b=strlen(s);
f=strlen(d);
for(a=0;a<b;a++)
{
q=1;
for(e=0,c=a;e<f;e++,c++)
{
if(s[c]!=d[e])
{
q=0;
}
}
if(q==1)
return 1;
}
return 0;
}
代码实现
#include
#include
#define N 20
int main(void)
{
char weekDay[7][N] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
char str[N];
char (*p)[N] = weekDay;
printf("Please enter a string:\n");
gets(str);
for(int i = 0; i < 7; i++)
{
if(strcmp(str,p + i) == 0)
{
printf("%s is %d\n",str,i);
return 0;
}
}
printf("Not found!\n");
return 0;
}
练兵区——编程题
代码实现
#include
#include
#include
int main()
{
char a[5][10],*min=a[0];
int i;
printf("Input five countries' names:\n");
for(i=0;i<5;i++)
{
gets(a[i]);
}
for(i=1,*min=a[0];i<5;i++)
{
if(strcmp(min,a[i])>0)
{
strncpy(min,a[i],sizeof(a[i]));
}
}
printf("The minimum is:%s\n", min);
return 0;
}
代码实现
#include
#include
void DD(int a,long *b,float *c);
void DD1(int a,long *b,float *c);
void EE(int a,long *b,float *c);
void FF(int a,long *b,float *c);
void GG(int a,float *c);
int main()
{
int n,i,u,a=1,max;
long b[100];
float c[100],sum=0;
printf("Input student number(n<30):\n");
scanf("%d",&n);
while(1!=0)
{
printf("Management for Students' scores\n\
1.Input record\n\
2.Caculate total and average score of course\n\
3.Sort in descending order by score\n\
4.Sort in ascending order by score\n\
5.Sort in ascending order by number\n\
6.Search by number\n\
7.Statistic analysis\n\
8.List record\n\
0.Exit\n\
Please Input your choice:\n");
scanf("%d",&a);
if(a==1)
{
printf("Input student's ID and score:\n");
for(u=0;u<n;u++)
{
scanf("%ld%f",&b[u],&c[u]);
}
}
else if(a==2)
{
for(u=0;u<n;u++)
{
sum+=c[u];
}
printf("sum=%.0f,aver=%.2f\n",sum,sum/n);
}
else if(a==3)
{
printf("Sort in descending order by score:\n");
DD(n,&b,&c);
}
else if(a==4)
{
printf("Sort in ascending order by score:\n");
DD1(n,&b,&c);
}
else if(a==5)
{
printf("Sort in ascending order by number:\n");
EE(n,&b,&c);
}
else if(a==6)
{
printf("Input the number you want to search:\n");
FF(n,&b,&c);
}
else if(a==7)
{
GG(n,&c);
}
else if(a==8)
{
EE(n,&b,&c);
}
else if(a==0)
{
printf("End of program!\n");
break;
}
else
{
printf("Input error!\n");
}
}
return 0;
}
void DD(int a,long *b,float *c)
{
int i,y,r;
float t;
for(i=0;i<a-1;i++)
{
for(y=i+1;y<a;y++)
{
if(*(c+i)<=*(c+y))
{
t=*(c+y),r=*(b+y);
*(c+y)=*(c+i),*(b+y)=*(b+i);
*(c+i)=t,*(b+i)=r;
}
}
}
for(i=0;i<a;i++)
{
printf("%ld\t%.0f\n",*(b+i),*(c+i));
}
}
void DD1(int a,long *b,float *c)
{
int i,y,r;
float t;
for(i=0;i<a-1;i++)
{
for(y=i+1;y<a;y++)
{
if(*(c+i)<=*(c+y))
{
t=*(c+y),r=*(b+y);
*(c+y)=*(c+i),*(b+y)=*(b+i);
*(c+i)=t,*(b+i)=r;
}
}
}
for(i=a-1;i>=0;i--)
{
printf("%ld\t%.0f\n",*(b+i),*(c+i));
}
}
void EE(int a,long *b,float *c)
{
int i,y,r;
float t;
for(i=0;i<a-1;i++)
{
for(y=i+1;y<a;y++)
{
if(*(b+i)<=*(b+y))
{
t=*(c+y),r=*(b+y);
*(c+y)=*(c+i),*(b+y)=*(b+i);
*(c+i)=t,*(b+i)=r;
}
}
}
for(i=a-1;i>=0;i--)
{
printf("%ld\t%.0f\n",*(b+i),*(c+i));
}
}
void FF(int a,long *b,float *c)
{
long int d,t=0,i;
scanf("%ld",&d);
for(i=0;i<a;i++)
{
if(*(b+i)==d)
{
printf("%ld\t%.0f\n",d,*(c+i));
t=1;
break;
}
}
if(t==0)
{
printf("Not found!\n");
}
}
void GG(int a,float *c)
{
int i,q,w,e,r,t,y;
q=w=e=r=t=y=0;
for(i=0;i<a;i++)
{
if(*(c+i)<60)
{
q++;
}
else if(*(c+i)<70)
{
w++;
}
else if(*(c+i)<80)
{
e++;
}
else if(*(c+i)<90)
{
r++;
}
else if(*(c+i)<100)
{
t++;
}
else if(*(c+i)==100)
{
y++;
}
}
printf("<60\t%d\t%.2f%%\n",q,(float)(100*q)/a);
printf("60-69\t%d\t%.2f%%\n",w,(float)(100*w)/a);
printf("70-79\t%d\t%.2f%%\n",e,(float)(100*e)/a);
printf("80-89\t%d\t%.2f%%\n",r,(float)(100*r)/a);
printf("90-99\t%d\t%.2f%%\n",t,(float)(100*t)/a);
printf("100\t%d\t%.2f%%\n",y,(float)(100*y)/a);
}
代码实现
#include
#include
#include
int main()
{
int n,i;
char a[13][100]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
printf("Input month number:\n");
scanf("%d",&n);
if(n>12 || n<1)
{
printf("Illegal month");
}
else
{
printf("month %d is %s\n",n,a[n-1]);
}
return 0;
}
#include
#define STUD 30 //最多可能的学生人数
#define COURSE 5 //最多可能的考试科目数
void Total(int *score, int sum[], float aver[], int m, int n);
void Print(int *score, int sum[], float aver[], int m, int n);
int main(void)
{
int i, j, m, n, score[STUD][COURSE], sum[STUD];
float aver[STUD];
printf("Enter the total number of students and courses:\n");
scanf("%d%d",&m,&n);
printf("Enter score:\n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
scanf("%d", &score[i][j]);
}
}
Total(*score, sum, aver, m, n);
Print(*score, sum, aver, m, n);
return 0;
}
void Total(int *score, int sum[], float aver[], int m, int n)
{
int i, j;
for (i=0; i<m; i++)
{
sum[i] = 0;
for (j=0; j<n; j++)
{
sum[i] = sum[i] + *(score + i * n + j);
}
aver[i] = (float) sum[i] / n;
}
}
void Print(int *score, int sum[], float aver[], int m, int n)
{
int i, j;
printf("Result:\n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
printf("%4d\t", *(score + i * n + j));
}
printf("%5d\t%6.1f\n", sum[i], aver[i]);
}
}
代码实现
#include
#define STUD 30
#define COURSE 5
void Total(int score[STUD][COURSE], int sum[], float aver[], int m, int n);
void Print(int score[STUD][COURSE], int sum[], float aver[], int m, int n);
int main(void)
{
int i, j, m, n, score[STUD][COURSE], sum[STUD];
float aver[STUD];
printf("Enter the total number of students and courses:\n");
scanf("%d%d",&m,&n);
printf("Enter score:\n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
scanf("%d", &score[i][j]);
}
}
Total(*score, sum, aver, m, n);
Print(*score, sum, aver, m, n);
return 0;
}
void Total(int score[STUD][COURSE], int sum[], float aver[], int m, int n)
{
int i, j;
for (i=0; i<m; i++)
{
sum[i] = 0;
for (j=0; j<n; j++)
{
sum[i] = sum[i] + *(*(score+i)+j);
}
aver[i] = (float) sum[i] / n;
}
}
void Print(int score[STUD][COURSE], int sum[], float aver[], int m, int n)
{
int i, j;
printf("Result:\n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
printf("%4d", *(*(score+i)+j));
}
printf("%5d%6.1f\n", sum[i], aver[i]);
}
}
#include
#include
#define M 150 /* 最多的字符串个数 */
#define N 10 /* 字符串最大长度 */
void SortString(char *ptr[], int n);
int main()
{
int i, n;
char *pStr[M];
printf("How many countries?\n");
scanf("%d",&n);
getchar(); /* 读走输入缓冲区中的回车符 */
printf("Input their names:\n");
for (i=0; i<n; i++)
{
gets(pStr[i]); /* 输入n个字符串 */
}
SortString(pStr, n); /* 字符串按字典顺序排序 */
printf("Sorted results:\n");
for (i=0; i<n; i++)
{
puts(pStr[i]); /* 输出排序后的n个字符串 */
}
return 0;
}
void SortString(char *ptr[], int n)
{
int i, j;
char *temp = NULL;
for (i=0; i<n-1; i++)
{
for (j=i+1; j<n; j++)
{
if (strcmp(ptr[j], ptr[i]) < 0)
{
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
}
}
}
代码实现
#include
#include
#define M 150
#define N 10
void SortString(char *ptr[], int n);
int main()
{
int i, n;
char *pStr[M],str[M][M];
for(i=0;i<M;i++)
{
pStr[i]=str[i];
}
printf("How many countries?\n");
scanf("%d",&n);
getchar();
printf("Input their names:\n");
for (i=0; i<n; i++)
{
gets(pStr[i]);
}
SortString(pStr, n);
printf("Sorted results:\n");
for (i=0; i<n; i++)
{
puts(pStr[i]);
}
return 0;
}
void SortString(char *ptr[], int n)
{
int i, j;
char *temp = NULL;
for (i=0; i<n-1; i++)
{
for (j=i+1; j<n; j++)
{
if (strcmp(ptr[j], ptr[i]) < 0)
{
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
}
}
}
代码实现
#include
#include
void InputArray(int *p, int m, int n);
int FindMax(int *p, int m, int n, int *pRow, int *pCol);
int main()
{
int m,n,a[10][10]={0},*p=NULL,max=0,*q=0,*w=0,i,y;
printf("Input m,n:\n");
scanf("%d,%d",&m,&n);
p=a;
printf("Input %d*%d array:\n",m,n);
InputArray(p,m,n);
max=FindMax(p,m,n,&q,&w);
printf( "max=%d,row=%d,col=%d\n",max,q,w);
return 0;
}
void InputArray(int *p, int m, int n)
{
int i,y;
for(i=0;i<m;i++)
{
for(y=0;y<n;y++)
{
scanf("%d",p+i*n+y);
}
}
}
int FindMax(int *p, int m, int n, int *pRow, int *pCol)
{
int i,y;
int max=*p;
for(i=0;i<m;i++)
{
for(y=0;y<n;y++)
{
if(max<*(p+i*n+y))
{
max=*(p+i*n+y);
*pRow=i,*pCol=y;
}
}
}
return max;
}
代码实现
#include
#include
int main()
{
int n,i,a[100],y,t;
printf("Input n:");
scanf("%d",&n);
printf("Input %d numbers:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(y=i+1;y<n;y++)
{
if(a[i]>a[y])
{
t=a[y];
a[y]=a[i];
a[i]=t;
}
}
}
printf("Sorting results:");
for(i=0;i<n;i++)
{
printf("%4d",a[i]);
}
return 0;
}
代码实现
#include
#include
int main()
{
char a[100],b,c[100];
int i,y;
printf( "Input a string:\n");
gets(a);
printf( "Input a character:\n");
b=getchar();
for(i=y=0;i<strlen(a);i++)
{
if(a[i]!=b)
{
c[y]=a[i];
y++;
}
}
c[y]='\0';
printf("Results:%s\n",c);
return 0;
}
代码实现
#include
#include
int main()
{
int a[10],max,min,i,y;
printf("Input 10 numbers:\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
max=min=a[0];
for(i=1;i<10;i++)
{
if(a[i]>max)
{
max=a[i];
}
if(a[i]<min)
{
min=a[i];
}
}
printf("maxNum=%d\n",max);
printf("minNum=%d\n",min);
for(y=min;y>0;y--)
{
if(min%y==0 && max%y==0)
{
printf("%d",y);
break;
}
}
return 0;
}
代码实现
#include
#define N 5
void Merge(int a[], int b[], int c[], int m, int n);
int main(void)
{
int m,n;
int a[N] = {0};
int b[N] = {0};
int c[N*2] = {0};
printf("Input m,n:");
scanf("%d,%d", &m, &n);
printf("Input array a:");
for(int i = 0; i < m; i++)
scanf("%d",&a[i]);
printf("Input array b:");
for(int i = 0; i < n; i++)
scanf("%d", &b[i]);
Merge(a,b,c,m,n);
for(int i = 0; i <(m + n); i++)
printf("%4d",c[i]);
return 0;
}
void Merge(int a[], int b[], int c[], int m, int n)
{
int i = 0;
int j = 0;
int k = 0;
while(i < m && j < n)
{
if(a[i] >= b[j])
{
c[k++] = a[i++];
}
else
{
c[k++] = b[j++];
}
}
while(i < m)
c[k++] = a[i++];
while(j < n)
c[k++] = b[j++];
}