1.a.用//在同一行中进行注释。用/*和*/在一段中进行注释。
b.用for,while等即使只有一行代码也尽量使用{}
c.合理利用缩进,让代码看起来更清晰
d.定义变量名尽量清晰
2.a.for
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n;
scanf("%d",&n);
int i;
int sum;
for(i=1;i<=n;i++){
sum=sum*i;
}
printf("n!=%d",sum);
return 0;
}
b.while
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n;
scanf("%d",&n);
int i=1;
int sum=1;
while(i<=n){
sum=sum*i;
i++;
}
printf("n!=%d",sum);
return 0;
}
c.do-while
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n;
scanf("%d",&n);
int i=1;
int sum=1;
do
{
sum=sum*i;
i++;
}while(i<=n);
printf("n!=%d",sum);
return 0;
}
3.
#include <stdio.h>
int main() {
int k=1;
double i;
double sum=1;
int a;
for(a=-1;i>= 1e-7||i<=1e-7;a=-a){
int b;
b=2k+1;
i=a*k/b;
sum=sum+i;
k++;
}
printf("%f",sum*4);
return 0;
}
4.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=1,a=0,b=1,c;
printf("0,1");
while(i<19){
c=a+b;
a=b;
b=c;
printf(",%d",c);
i++;
}
return 0;
}
5.a.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n,b;
scanf("%d",&n);
while(n>0){
b=n%10;
printf("%d",b);
n=n/10;
}
return 0;
}
b.