Not able to solve the puzzle regarding this code

安稳与你 提交于 2019-12-19 03:16:40

问题



int i,n=20;
for(i=0;i<n;i--)
printf("-");

I have been rattling my brain but have not been able to solve this.

Delete any single character or operator from above code and the program should print "-" 20 times

Please help!


回答1:


I don't think you can do it by deleting a character, but I have three solutions that replace (well, one of them adds a character, but only because you have no whitespace in your program. If you had whitespace, it would replace a space).

Solution 1

int i,n=20;
for(i=0;-i<n;i--) // -i < n 
    printf("-");

Solution 2

int i,n=20;
for(i=0;i<n;n--) // n-- 
    printf("-");

Solution 3

int i,n=20;
for(i=0;i+n;i--) // while i + n is not zero 
    printf("-");



回答2:


I found a reference to the problem on C Puzzles. (It's in a comment, so it's surely not the original source.)

The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work.

#include <stdio.h> 
int main() 
{ 
int i; 
int n = 20; 
for( i = 0; i < n; i-- ) 
printf("-"); 
return 0; 
}

Well fixing the above code is straight-forward. To make the problem interesting, you have to fix the above code, by changing exactly one character. There are three known solutions. See if you can get all those three.

Note that the instructions say:

...you have to fix the above code, by changing exactly one character.

One solution is to change i-- to n-- in the header of the for loop.




回答3:


The problem, as stated, has no solution. Either you or whoever gave that problem to you have stated it incorrectly.




回答4:


int i,n=20;
for(i=0;i<n;n--)
printf("-");

I don't know if replacing is ok but changing i-- to n-- should do the trick




回答5:


The puzzle is supposed to allow for "changing one character".

The solutions are to change < to +, change i to n, or change the space before i in the middle of the for loop to a - (there's supposed to be spaces.)

Your friend doesn't get the question. :-)




回答6:


int i,n=20;
for(i=0;i<n;i--) //change i-- to i++
printf("-");

EDIT: You were using a decrement operator instead of increment. So you want it to keep incrementing i till it reaches 20. At which point it will stop because then i would no longer be less than 20 but equal to.




回答7:


The program already prints - 20 times — and then it continues to print it a lot more afterward. The puzzle didn't say it needed to print it exactly 20 times.

If you really must delete something, then you can get similar behavior by deleting the -- operator.

int i,n=20;
for(i=0;i<n;i) // no more decrement
printf("-");

Other characters that are candidates for deletion are the line breaks.




回答8:


I can do it by adding a single character:

int i,n=20;
for(i=0; - i <n;i--)
    printf("-");



回答9:


Change:

for(i=0;i<n;i--)

to:

for(i=0;i<n;n--)

But I don't see how you can only delete a char or operator... You have to modify an operator or character.




回答10:


It's been years since I did c, and I'm pedantic, so please forgive me, but... doesn't the program print "-" 20 times already? And then some?

If you delete the "f" from "printf", doesn't it continue to print "-" 20 times? At least?

If it's a trick question, maybe this is the trick...



来源:https://stackoverflow.com/questions/3321976/not-able-to-solve-the-puzzle-regarding-this-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!