开发者

Read from input two values and add them, just using one variable.. possible?

开发者 https://www.devze.com 2023-02-15 14:02 出处:网络
Salute.. Let\'s see this example: int x,y,s; cin>>x>>y; s=x+y; here we hav开发者_如何学编程e three variables for adding two values..

Salute..

Let's see this example:

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

here we hav开发者_如何学编程e three variables for adding two values..

Can we do this just with one variable?

thanks.


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";
}


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?


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.


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;


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.


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.


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.


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);
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消