A. 一道c語言編程題,各位大神幫幫忙啊!
代碼如下:
#include<stdio.h>
#include<stdlib.h>
intmain()
{
intscore=0,sum=0,max=0,min=100,count=0,aver=0;
printf("請輸入學生成績:");
scanf("%d",&score);
while(score>=0&&score<=100){
sum+=score;
if(score>max){
max=score;
}
if(score<min){
min=score;
}
count++;
scanf("%d",&score);
}
printf("共錄入了%d個學生成績。 ",count);
printf("最高分:%d ",max);
printf("最低分:%d ",min);
printf("平均分:%d ",sum/count);
system("pause");
return0;
}
B. 用C語言編寫1ms延遲子程序,不明白for (j=0;j<120;j++),求助大大們
void delay1ms(int x)//延遲函數開始
{
int i,j;
for (i=0;i<x;i++) //計數x次,延遲x 1ms
for (j=0;j<120;j++); //計數120次,延遲1ms
}
這是一個for循環嵌套,每執行一次第一個for語句,那麼第二個for語句要執行120次
for (j=0;j<120;j++); 就是沒執行一次第一個for循環,就令 j =0,再判斷是否 j 滿足循環條件(這里就是判斷是否 j 小於120)如果滿足就繼續循環,如果不滿足就跳出循環。
C. C語言初學編程問題 排序問題!求助 感謝!
#include<stdio.h> //冒泡排序,從小到大。void sort(int a[], int n) { int i, j; int temp; int len = n; for (i = 0; i < len - 1; i++) for (j = 0; j < len - 1 - i; j++){ if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j+1] = temp; } } } int main() { int a[5] = {2, 3 ,1, 5, 4}; int i; sort(a, 5); for(i = 0; i < 5; i++) { printf("%d ", a[i]); } printf("\n");}
D. 求助各位C語言編程高手~幫我做3道題~
#include<stdio.h>
#include<string.h>
#defineN4
/*-----------------------------------------
第一題
-------------------------------------------*/
voidDiamond(constchar*s,intn,intlen)
{
printf("%*s%-s\n",len,s+n-1,s+n);
if(n>1)
Diamond(s,n-1,len);
printf("%*s%-s\n",len,s+n,n==len?s+n:s+n+1);
}
/*-----------------------------------------
第二題
-------------------------------------------*/
typedefstruct
{
intgcd;
intlcm;
}pair;
voidGCD_LCM(inta,intb,pair*p)
{
inttmp;
intproct=a*b;
while(b%a)
{
tmp=a;
a=b%a;
b=tmp;
}
p->gcd=a;
p->lcm=proct/a;
}
/*-----------------------------------------
第三題
-------------------------------------------*/
voidSwap(int*lhs,int*rhs)
{
inttmp=*lhs;
*lhs=*rhs;
*rhs=tmp;
}
voidBubbleSort(int*beg,int*end)
{
for(;beg!=end;++beg)
for(int*p=end-1;p!=beg;--p)
if(*p<*(p-1))
Swap(p,p-1);
}
voidSelectSort(int*beg,int*end)
{
for(;beg!=end;++beg)
{
int*max=beg;
for(int*p=beg+1;p!=end;++p)
if(*max<*p)
max=p;
Swap(beg,max);
}
}
voidPrint(int*beg,int*end)
{
while(beg!=end)
printf("%d",*beg++);
putchar('\n');
}
intmain()
{
/*一*/
charpt[N+1]={0};
memset(pt,'*',N);
Diamond(pt,N,N);
/*二*/
pairp;
GCD_LCM(3,6,&p);
printf("%d%d\n",p.gcd,p.lcm);
/*三*/
inta[]={32,9,45,22,15,48,47,8,55,1};
Print(a,a+10);
BubbleSort(a,a+10);
Print(a,a+10);
SelectSort(a,a+10);
Print(a,a+10);
}