简体   繁体   English

简短的编程:需要帮助的指针

[英]Short programming: need help with pointers

#include<stdio.h>    
int main()
{
    int i=10,a;
    while(i>0)
    {
        scanf("%d",&a);
        printf("%d\n",(a+a*a)/2);
        i--;
    }
}

Now what i want to ask is how to make it shorter? 现在我想问的是如何使其更短? Maybe some way using pointers instead of variables? 也许以某种方式使用指针而不是变量? Maybe this: (a+a*a)/2 can be done better? 也许这样: (a+a*a)/2可以做得更好?

Beware what you ask for... 当心你的要求...

#include <iostream>

int main() 
{
    int a; 
    for (int i = 10;
         i-- && (std::cin >> a) && (std::cout << (a + a * a) / 2); )
        ;
} 

(this also includes error checking, which you didn't bother with) (这还包括错误检查,您无需理会)

EDIT: Chris's comments below reveal this to be a terse-programming challenge (and not about legibility, robustness etc.). 编辑:克里斯下面的评论表明这是一个简短的编程挑战(而不是关于易读性,鲁棒性等)。 His teacher claims a 54 character implementation. 他的老师要求实现54个字符。
Have to wonder whether that include spaces and newlines? 必须怀疑是否包括空格和换行符?

The tightest implementation I've got so far is: 到目前为止,我最严格的实现是:

#include<iostream.h>
int main(){for(int i=10,a;i--;)cin>>a,cout<<(a+a*a)/2;}

This uses the deprecated header iostream.h to remove the need to explicitly find cin and cout in std:: . 这使用了不赞成使用的标头iostream.h来消除在std::显式查找cincout的需要。 It's still 74 characters (LF newlines on both lines). 仍然是74个字符(两行都是LF换行符)。

As a thought experiment, let's assume we had a header "x" that declared functions int g() (get an int from stdin) and void p(int) (write an int to stdin). 作为一个思想实验,假设我们有一个标头“ x”,它声明了函数int g() (从stdin中获取一个int)和void p(int) (将一个int写入stdin)。 The implementation would then be... 然后实现将是...

#include<x>
int main(){for(int i=10,a;i--;)a=g(),p((a+a*a)/2);}

...which is still 64 characters. ...仍然是64个字符。

Another technique commonly used to shorten code is preprocessor tricks, but the obvious one here - loop unrolling - doesn't look promising. 通常用于缩短代码的另一种技术是预处理器技巧,但此处显而易见的一种方法-循环展开-看起来没有希望。 It could look like... 看起来像...

#define X ...
int main(){X X X X X X X X X X}

...but if you compare for loop code to the #define -necessitated code... ...但如果你比较for循环代码到#define -necessitated代码...

for(int i=10,a;i--;)
#define X X X X X X X X X X X

...we're clearly going backwards. ...我们显然正在倒退。

Summarily, I simply can not imagine any approach shaving 10 characters off the <iostream.h> version above. 综上所述,我简直无法想象有什么办法可以减少上述<iostream.h>版本中的10个字符。 If he has a 54-character implementation, I think the requirements must have been miscommunicated in the question. 如果他有一个54个字符的实现,我认为问题中的要求一定被误解了。 If not, I do hope Chris will post it later :-). 如果没有,我希望克里斯稍后再发布:-)。

One way you can shorten this is to use a for loop instead of a while loop. 可以缩短此时间的一种方法是使用for循环而不是while循环。 Try using 尝试使用

for (i = 10; i > 0; --i) {

Rather than your current while statement. 而不是您当前的while语句。 This eliminates the need for the i--; 这消除了对i--;的需要i--; line. 线。

I'm not sure what you mean by making this code shorter "using pointers" though. 我不确定通过“使用指针”使这段代码更短意味着什么。 Adding pointers won't help here. 添加指针在这里无济于事。

Using this for loop, your code would look like: 使用此for循环,您的代码将如下所示:

#include<stdio.h>    
int main()
{
    int i, a;
    for (i = 10; i > 0; --i)
    {
        scanf("%d",&a);
        printf("%d\n",(a+a*a)/2);
    }
}

This removes one line. 这将删除一行。 The only way of removing more lines would be to make this code less readable, or by changing your code style (which is completely unnecessary). 删除更多行的唯一方法是使该代码的可读性降低,或者通过更改代码样式(完全没有必要)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM