36 1234
发新话题
打印

一道程序题,测试下大家的能力

是可以在编程中养成一种好的习惯,就是把(n==1)这样的形式,写成(1==n)
一旦这样写的话,就算你无意中少写了一个等号,在编译的时候就能马上发现错误,(编译器能提示)
如果是n==1的话,如果少写一个等号,那么在语法上没有错误,但是在逻辑上出现了错误,这样编译器就无法提示错误了。
这个习惯我也是在一份报纸上看到的
引用:
原帖由 tangguyan 于 2008-3-31 12:29 发表
是可以在编程中养成一种好的习惯,就是把(n==1)这样的形式,写成(1==n)
一旦这样写的话,就算你无意中少写了一个等号,在编译的时候就能马上发现错误,(编译器能提示)
如果是n==1的话,如果少写一个等号 ...
http://man.lupaworld.com/content/develop/c&c++/c/c.htm
里面有很多很好的编程习惯。
#include "iostream.h"
void main()
{ int year=0;
  cout<<"Input the year:";
  cin>>year;
  if(year==1) cout<<"The area is 305!"<<endl;
  else if(year>1) cout<<"The area is"<<305+3*(year-1)<<endl;
        else cout<<"The input is wrong!"<<endl;
}   //C++实线了  其实不用for循环的  可以用乘法嘛

#include "stdio.h"
void main()
{ int year=0;
  printf("Input the year:");
  scanf("%d",&year);
  if(year==1) printf("The area is 305!\n");
  else if(year>1) printf("The area is %d\n!",305+3*(year-1));
        else printf("The input is wrong!\n");
}   //C实线了  其实不用for循环的  可以用乘法嘛

#include "stdio.h"
void main()
{ int year=0,i=0,count=305;
  printf("Input the year:");
  scanf("%d",&year);
  if(year==1) printf("The area is 305!\n");
  else if(year>1)
{ for(;i<year;i++)
   count=count+3;
    printf("The area is %d\n!",305+3*(year-1));
   }
     else printf("The input is wrong!\n");
}   //C实线了  用for循环的

#include "stdio.h"
void main()
{ int year=0,i=0,count=305;
  printf("Input the year:");
  scanf("%d",&year);
  if(year==1) printf("The area is 305!\n");
  else if(year>1)
{do{count=count+3;i++;}while(i>=year);
    printf("The area is %d\n!",305+3*(year-1));
   }
     else printf("The input is wrong!\n");
}   //C实线了  用while循环的
大家可以仔细比对比对  有好处的
int main(){
  int i,n,m=305;
  scanf(%d,&n);
if(n==1)
    ;
else
      for(i=2;i<=n;i++)
        m+=1;
  printf("%d",m);
}

[ 本帖最后由 ddddddz 于 2008-4-2 09:53 编辑 ]

i != n

int main(){
  int i,n,m;
  scanf(%d,&n);
  for(i=1;i<=n;i++)
    if (n=1)
      m=300+5;
    if(n>=2)
      m+=1;
  printf("%d",m);
}

是i,不是n,晕倒,这个和for有什么关系呀?!
引用:
原帖由 infohunter 于 2007-4-15 23:57 发表


呵呵
if(n == 1)
编程的时候 判断 最好 把数字放在变量的前面
就不会错误了 嘿嘿
开源:人之所需,时之所趋,共进之模式!
http://mathbox.lupaworld.com
 36 1234
发新话题