一个for语句的趣题

一个for语句的趣题

You should change one character to print “*” 42 times. You can replace, add or remove ONLY one character.

Still i cant figured it, i’ve tried over and over again.

#include 
main(){
    int i,n = 42;

   for(i = 0; i < n; i--){
       printf("*");
   }
}

Answer

1. 改变循环变量符号。

#include 
main(){
    int i,n = 42;

   for(i = 0; -i < n; i--){    //added a minus here
       printf("*");
   }
}

2. 改变循环条件

#include 
main(){
    int i,n = 42;

   for(i = 0; i + n; i--){
       printf("*");
   }
}

3. 改变循环次数变量n

for(i = 0; i < n; n--){        
    printf("*");    
}

三种方法都能解决问题,然而却是三种不通的思路。

Leave a Reply

Your email address will not be published. Required fields are marked *