从输入读取两个值并将它们相加,仅使用一个变量..可能吗?

发布于 2024-10-20 11:53:44 字数 182 浏览 1 评论 0 原文

致敬..

让我们看一下这个例子:

int x,y,s;
cin>>x>>y;
s=x+y;

这里我们有三个变量用于添加两个值。.

我们可以只用一个变量来做到这一点吗?

谢谢。

Salute..

Let's see this example:

int x,y,s;
cin>>x>>y;
s=x+y;

here we have three variables for adding two values..

Can we do this just with one variable?

thanks.

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

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

发布评论

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

评论(8

拔了角的鹿 2024-10-27 11:53:44

零变量怎么样?

#include <numeric>
#include <iterator>
#include <iostream>

int main()
{
  std::cout <<
    std::accumulate(
      std::istream_iterator<int>(std::cin),
      std::istream_iterator<int>(),
      0) <<
    "\n";
}

How about zero variables?

#include <numeric>
#include <iterator>
#include <iostream>

int main()
{
  std::cout <<
    std::accumulate(
      std::istream_iterator<int>(std::cin),
      std::istream_iterator<int>(),
      0) <<
    "\n";
}
山人契 2024-10-27 11:53:44
struct Accumulator {
    int value;
    Accumulator(): value(0) {}
    friend std::istream& operator>>(std::istream& ss, Accumulator& acc)
    { int x; ss >> x; acc.value += x; return ss; }
};

int main() {
    Accumulator a;
    std::cin >> a >> a;
    std::cout << "Total is " << a.value << "\n";
    return 0;
}

看看抽象有多有用吗?

struct Accumulator {
    int value;
    Accumulator(): value(0) {}
    friend std::istream& operator>>(std::istream& ss, Accumulator& acc)
    { int x; ss >> x; acc.value += x; return ss; }
};

int main() {
    Accumulator a;
    std::cin >> a >> a;
    std::cout << "Total is " << a.value << "\n";
    return 0;
}

See how useful abstraction is?

别低头,皇冠会掉 2024-10-27 11:53:44

您可以通过使用提取运算符两次来剪切一个。

int x, s = 0;
cin >> x;
s += x;
cin >> x;
s += x;

您可以通过使用大小为 int 两倍的单个变量来进一步减少该量。我不敢相信我正在输入这样的内容:

long long s;
assert(sizeof(int)*2 == sizeof(long long));
cin >> *(int*)(&s);
cin >> *((int*)(&s)+1);
s = (s & 0xffffffff) + ((s >> 32) & 0xffffffff);

只有当您绝对需要执行某些操作(例如出于神秘的性能原因将两个 32 位值存储在 64 位寄存器中)时,您才可以执行此类操作,否则诸神将会惩罚您你。在这种情况下,您可能无论如何都不会使用 iostream 库,但就这样吧。我要去洗个澡,把代码的味道洗掉。我可能需要一些碱液。

You can cut out one by using the extraction operator twice.

int x, s = 0;
cin >> x;
s += x;
cin >> x;
s += x;

You could cut that down even more by using a single variable that's twice the size of int. I can't believe I am typing this:

long long s;
assert(sizeof(int)*2 == sizeof(long long));
cin >> *(int*)(&s);
cin >> *((int*)(&s)+1);
s = (s & 0xffffffff) + ((s >> 32) & 0xffffffff);

You're only allowed to do things like this when you absolutely need to do something like store two 32-bit values in a 64-bit register for arcane performance reasons, or the gods will smite you. In such a case you are likely not using the iostream library anyway, but there you go. I'm going to go take a shower to wash the code smell off. I might need some lye.

夢归不見 2024-10-27 11:53:44

这是可能的。

非常重要的注意事项,int 是 16 位。代码非常非常长。有很多常数。像这样的东西。

int x;
cin >> x;

if (x == -32768) {
    cin >> x;
    x = x - 32768; 
} else if (x == -32767) {
    cin >> x;
    x = x - 32767; 
} else ...
...
} else if (x == -1) {
    cin >> x;
    x = x - 1;
} else if (x == 0) {
    cin >> x;
    x = x;
} else if (x == 1) {
    cin >> x;
    x = x + 1;
} else ...
...
} else if (x == 32766) {
    cin >> x;
    x = x + 32766;
} else {
    cin >> x;
    x = x + 32767;
}
cout << x << endl;
return 0;

It is possible.

Very important note, int is 16 bit. The code is very, very long. There is a lot of constants. Something like this.

int x;
cin >> x;

if (x == -32768) {
    cin >> x;
    x = x - 32768; 
} else if (x == -32767) {
    cin >> x;
    x = x - 32767; 
} else ...
...
} else if (x == -1) {
    cin >> x;
    x = x - 1;
} else if (x == 0) {
    cin >> x;
    x = x;
} else if (x == 1) {
    cin >> x;
    x = x + 1;
} else ...
...
} else if (x == 32766) {
    cin >> x;
    x = x + 32766;
} else {
    cin >> x;
    x = x + 32767;
}
cout << x << endl;
return 0;
御守 2024-10-27 11:53:44

我知道你可以使用 x+=y 将其减少为 2 个变量,

你不能做任何像 cin< 这样的事情这是我知道如何将其减少为单个变量的唯一方法。

I know you can cut it down to 2 variables by using x+=y

You're not allowed to do anything like cin<<x<<x+=y which would be the only way I would know how to cut it down to a single variable.

甜`诱少女 2024-10-27 11:53:44

只要该变量是 int 就不能。但你为什么还要这么做呢?

当然,你可以通过x += y去掉s

You can't as long as that variable is an int. But why do you want to anyway?

Of course, you can get rid of s by x += y.

猫瑾少女 2024-10-27 11:53:44
int main() {
        typedef long long int64;

        int64 x;
        cin >> ((int*)(&x))[0] >> ((int*)(&x))[1];
        x = ((int*)(&x))[0] + ((int*)(&x))[1];
        cout << x << endl;
        return 0;
}

输入:

100 250

输出

350

在 ideone 中查看自己: http://ideone.com/2AmK0

注意: 我不知道这个解决方案的便携性如何。但这适用于gcc-4.3.4

int main() {
        typedef long long int64;

        int64 x;
        cin >> ((int*)(&x))[0] >> ((int*)(&x))[1];
        x = ((int*)(&x))[0] + ((int*)(&x))[1];
        cout << x << endl;
        return 0;
}

Input:

100 250

Output

350

See yourself at ideone : http://ideone.com/2AmK0

Note: I don't know how portable this solution is. But this works with gcc-4.3.4.

情释 2024-10-27 11:53:44

我认为下面的一个可以工作......

#include<stdio.h>
int x=0;
int getno()
   {
     scanf("%d",&x);
     return x;
   }
int main()
   {
     x=getno()+getno();
     printf("addition of the number is = %d",x);
   }

I think the below one would work.....

#include<stdio.h>
int x=0;
int getno()
   {
     scanf("%d",&x);
     return x;
   }
int main()
   {
     x=getno()+getno();
     printf("addition of the number is = %d",x);
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文