由于这些代码也是我初学时写的代码,故其中的规范程度及简洁程度并不很好(此处我后来写的有可以参考一下->C语言代码规范),但是能很好的接近出初学者的水平,也更有参考价值!排版不易,喜欢就点个赞吧!如有问题,请勿吐槽,欢迎留言互相学习。
第6周编程题在线测试
代码实现:
#include
#include
long Fact(int n);
int main()
{
int a,b,c;
int m;
for(m = 100;m<=999;m++)
{
a = m/100,b = (m%100)/10,c = m%10;
if(Fact(a)+Fact(b)+Fact(c)==m)
printf("%d\n",m);
}
return 0;
}
long Fact(int n)
{
if(n == 0)
return 1;
else
return n*Fact(n-1);
}
代码实现:
#include
#include
int Func(int n);
int main()
{
int n;
printf("Input n:");
scanf("%d",&n);
if(n>1000000 || n<1000)
printf("Input error!\n");
else
printf("%d\n",Func(n));
return 0;
}
int Func(int n)
{
int a;
for(a=999;a>=100;a--)
{
if(n%a==0)
return a;
}
return 1;
}
代码实现:
#include
#include
int Gcd(int a, int b);
int main()
{
int m,n;
printf("Input m,n:");
scanf("%d,%d",&m,&n);
if(m<1 || n>10000 || n==0)
printf("Input error!\n");
else
printf("%d/%d\n",m/Gcd(m,n),n/Gcd(m,n));
return 0;
}
int Gcd(int a, int b)
{
int c;
while (b!=0)
{
c = a%b;
a = b;
b = c;
}
return a;
}
代码实现:
#include
#include
int IsPrime(int x);
int main()
{
int m,n,sum = 0;
printf("Input n:");
scanf("%d",&n);
for(m = 2; m <=n; m++)
{
if(IsPrime(m))
{
sum += m;
}
}
printf("sum=%d\n",sum);
return 0;
}
int IsPrime(int x)
{
int i;
for(i = 2; i <= sqrt(x); i++)
{
if(x%i == 0)return 0;
}
return 1;
}
第六周练兵区——编程题
代码实现:
#include
#include
int main()
{
int a,b,c,d;
char q;
printf("Please input a capital:\n");
scanf("%c",&q);
for(a=65;a<=q;a++)
{
for(b=q-a;b>=0;b--)
{
printf(" ");
}
for(c=65;c<=a-1;c++)
{
printf("%c",c);
}
printf("%c",a);
for(d=a-1;d>=65;d--)
{
printf("%c",d);
}
printf("\n");
}
return 0;
}
代码实现:
#include
#include
int main()
{
char s=70;
int a,b,c;
for(a=65;a<=s;a++)
{
c=s;
for(b=a-65;b>=0;b--)
{
printf("%c",c);
c--;
}
printf("\n");
}
return 0;
}
代码实现:
#include
#include
double fact(double n);
int main()
{
int i = 1,sign = -1;
double x,sum = 0,p,q;
printf("Input x:\n");
scanf("%lf",&x);
q = x;
do
{
sign = -sign;
p = q*sign/fact(2*i - 1);
sum += p;
q = q*x*x;
i++;
}while(fabs(p) >= 1e-5);
printf("sin(x)=%.3f,count=%d\n",(float)sum,i - 1);
return 0;
}
double fact(double n)
{
if(n == 0)return 1.0;
else return n*fact(n - 1);
}
代码实现:
#include
#include
int fun(int m);
int main()
{
int m,sum = 0;
for(m = 100; m <=200; m++)
{
if(fun(m))
{
sum += m;
}
}
printf("sum=%d\n",sum);
return 0;
}
int fun(int m)
{
int i;
for(i = 2; i <= sqrt(m); i++)
{
if(m%i == 0)return 0;
}
return 1;
}
代码实现:
#include
#include
int getint(int min, int max);
int main()
{
int min,max;
printf("Please enter min,max:\n");
scanf("%d,%d",&min,&max);
printf("The integer you have entered is:%d\n",getint(min,max));
return 0;
}
int getint(int min, int max)
{
int a;
do
{
printf( "Please enter an integer [%d..%d]:\n",min,max);
scanf("%d",&a);
}while(min>a || max<a);
return a;
}
#include
int main()
{
int score;
char grade;
printf("Please input score:");
scanf("%d", &score);
if (score < 0 || score > 100)
printf("Input error!\n");
else if (score >= 90)
grade = 'A’;
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'E';
printf("grade:%c\n", grade);
return 0;
}
代码实现:
#include
int main()
{
int score=66,a;
char grade;
printf("Please input score:\n");
a=scanf("%d", &score);
while(a==0 || score>100 || score<0)
{
if(a==0)
{
getchar();
}
printf("Input error!\n");
printf("Please input score:\n");
a=scanf("%d", &score);
}
switch(score/10)
{
case 10:
case 9:
grade = 'A';
printf("grade: %c\n", grade);
break;
case 8:
grade = 'B';
printf("grade: %c\n", grade);
break;
case 7:
grade = 'C';
printf("grade: %c\n", grade);
break;
case 6:
grade = 'D';
printf("grade: %c\n", grade);
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
grade = 'E';
printf("grade: %c\n", grade);
break;
}
return 0;
}
代码实现:
#include
#include
int main()
{
int a,n,d;
long c = 0,sum = 0;
printf("Input a,n:\n");
scanf( "%d,%d", &a, &n);
for(d = 1;d <= n; d++)
{
c = c*10 + a;
sum += c;}
printf("sum=%ld\n",sum);
return 0;
}
代码实现:
#include
#include
int main()
{
int n,a,b,c;
printf("Input n(27);
scanf("%d",&n);
for (a=0;a<=n/4;a++)
for (b=0;b<=n/3;b++)
for (c=0;c<=n*2;c++)
if(a+b+c==36 && c%2==0 && a*4+b*3+c/2==n)
{printf("men=%d,women=%d,children=%d\n",a,b,c);}
return 0;
}
代码实现:
#include
#include
#include
int main()
{
int year,month;
printf("Input year,month:\n");
scanf("%d,%d",&year,&month);
if(month>12 || month<0)
{
printf("Input error!\n");
goto END;
}
year = fabs(year);
if((year%400==0)||(year%4==0 && year%100!=0))
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("31 days\n");
break;
case 4:
case 6:
case 9:
case 11:
printf( "30 days\n");
break;
case 2:
printf( "29 days\n");
break;
}
}
else
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("31 days\n");
break;
case 4:
case 6:
case 9:
case 11:
printf( "30 days\n");
break;
case 2:
printf( "28 days\n");
break;
}
END: ;
return 0;
}
}