无法解决有关此代码的难题

发布于 2024-09-11 01:10:03 字数 186 浏览 3 评论 0原文


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

我绞尽脑汁却没能解决这个问题。

从上面的代码中删除任何单个字符或运算符,程序应该打印“-”20次,

请帮忙!


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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

装迷糊 2024-09-18 01:10:03

我不认为你可以通过删除一个角色来做到这一点,但我有三种替换解决方案(好吧,其中一个添加了一个角色,但只是因为你有程序中没有空格。如果有空格,它将替换空格)。

解决方案1

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

解决方案2

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

解决方案3

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

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("-");
老街孤人 2024-09-18 01:10:03

我在 C Puzzles 上找到了对该问题的引用。 (这是在评论中,所以它肯定不是原始来源。)

下面是一段C代码,其目的是打印一个减号20次。但你可以注意到,它不起作用。

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

修复上面的代码很简单。为了使问题变得有趣,您必须通过仅更改一个字符来修复上述代码。存在三种已知的解决方案。看看你是否能同时获得这三个。

请注意,说明说:

...您必须通过更改一个字符来修复上述代码。

一种解决方案是在 for 循环头中将 i-- 更改为 n--

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.

躲猫猫 2024-09-18 01:10:03

如前所述,该问题没有解决方案。无论是你还是任何向你提出这个问题的人都错误地表述了这一问题。

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

权谋诡计 2024-09-18 01:10:03
int i,n=20;
for(i=0;i<n;n--)
printf("-");

我不知道替换是否可以,但将 i-- 更改为 n-- 应该可以解决问题

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

简单爱 2024-09-18 01:10:03

该谜题应该允许“改变一个角色”。

解决方案是改变<改为 +,将 i 改为 n,或将 for 循环中间 i 之前的空格改为 - (应该有空格。)

你的朋友没有明白这个问题。 :-)

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. :-)

凹づ凸ル 2024-09-18 01:10:03
int i,n=20;
for(i=0;i<n;i--) //change i-- to i++
printf("-");

编辑:您使用的是减量运算符而不是增量。所以你希望它继续递增 i 直到达到 20。此时它将停止,因为那时 i 将不再小于 20 而是等于。

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.

念三年u 2024-09-18 01:10:03

该程序已经打印了- 20次——然后它继续打印更多次。该谜题并没有说需要将其打印恰好 20 次。

如果您确实必须删除某些内容,那么您可以通过删除 -- 运算符来获得类似的行为。

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

其他可供删除的候选字符是换行符。

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.

蘑菇王子 2024-09-18 01:10:03

我可以通过添加单个字符来做到这一点:

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

I can do it by adding a single character:

int i,n=20;
for(i=0; - i <n;i--)
    printf("-");
長街聽風 2024-09-18 01:10:03

更改

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

为:

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

但我不明白如何只能删除字符或运算符...您必须修改运算符或字符。

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.

踏月而来 2024-09-18 01:10:03

我已经好几年没有做过c了,我很迂腐,所以请原谅我,但是……程序不是已经打印了20次“-”了吗?然后还有一些?

如果把“printf”中的“f”删掉,不是会继续打印“-”20次吗?至少?

如果这是一个技巧问题,也许这就是技巧......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文