Fastest way to input integers [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What is the fastest way(code) to take integer input from user via terminal (not file...:P).
P.S 1: Integers are of small size( within size of int) but the total number of intergers is very large.
P.S 2: Scanf toooo... slow
P.S 3: Forget the human limits ,talk technical...plz

I think an approach based on scanf will be hard to beat. In any case, it will be easy to implement. So I'd start with that, if it's not sufficient, benchmark before trying anything else.
If the input consists of whitespace-separated integers:
scanf("%d ", &input)

for continuous input processing you can try this
while( scanf("%d ", &val) == 1)
{
// processing : do what you want
}
also you can use this for file inputs reading (fscanf)

Related

Upto how many numbers next_permutation (given in CPP <algorithm>)works? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am using next_permutation in cpp (algorithm library). What is the upper limit on the length of the string which it can permute? As i can permute a string of length 5 easily but for length 50 it's just do not stop. I know 50! is quite large so i want to know if there is some reasonable limit on it's usage.
Well it works for as many numbers as your memory will fit. It simply takes longer. And by longer I mean that for 50! you will have to wait about 96442456880115988215412887386050129516671872047 years on a contemporary computer or a lot of orders of magnitute longer then the universe is supposed to exist.

Palindrome without using extra space [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I know various ways to check if an integer is a palindrome or not by using string conversion, stack and number breaking, but here question is
"How can we check wether an integer is a palindrome or not, without using any extra space?"
You can revert a number with some code like:
int revert(int num) {
int reverted = 0;
while (num) {
reverted = reverted*10 + num%10;
num /= 10;
}
return reverted;
}
And now you only check if
num == revert(num)
That is all. Sorry for giving the exact solution instead of just a tip, but I don't think I could have given any tip without the solution itself.

Generate all Binary Combinations C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to write a program that you give an input (number of digits n) and you get as an output a vector of all possible binary grey code generated.
For example,
if n=2, results should be:
V= {00, 01, 10,11}
Actually I am interested in having them bit by bit and not whole integer. Meaning I want to have a 2 D array of integers where I have each word in rows and bits (as int) in cols
Hint: Gray code of num is (num>>1) ^ num. Go through all numbers of the 0..2^N-1 range, and compute their Gray code representation using this simple formula.
EDIT Another hint: the simplest way to convert an integer to binary is using bitset:
bitset<N>((num>>1) ^ num).to_string()
The set V equals { 0, ..., 2^n-1 }. Just compute this set.

How do I make a smaller range with two given integers? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
For example,
I have a range 14269-14274.
To conserve space on the screen my users want to have it display in the format 14269-74.
Another example would be a range of 14269-14529 which should output as 14269-529.
How would I achieve this?
Something like this should do the trick:
int a = 14269;
int b = 14529;
int endrange = b % pow(10, floor(log10(b - a) + 1));
You need to make sure that a < b though.
You can check the first digit that differs, output the first number and then the second one, starting at the first different digit.
This of course only makes sense if the two numbers have the same length.
Were you expecting the implementation?

print a series of numbers optimization part 2 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
earlyer i posted part 1 and got some interesting responces
print a series of numbers optimization part 1
here is another way you could have the program print a repeating series of numbers to the screen, the goal here is to make the most efficiant/fastest algorithm
int series[] = [2,3,4,5,6,7,8,9,1]
int i = 9;
while(true)
{
print(series[i])
i = series[i] - 1;
}
of course ignore any extra overhead created by actually printing the number because that is not the purpose of the problem
the one boolean conditional statement (while true) is required for the infinite loop is required no matter what solution you do, so you can ignore that too
this solution uses memory for 11 int variables, but otherwise it only does one simple computation and one variable assignment per iteration.
so would this be the most time efficiant way to solve the infiniate number series problem?
I would say it's not the most efficient way.
There's a multiplication involved in addressing the array. It's essentially
destinationAddress = baseAddressOfArray + indexRequested * sizeof(elementOfArray)
I think the most efficient way would be to cache the string of one iteration and simply spit out that string over and over again. I'm not up on my exact C++ syntax, it'd be something like
string s = "123456789";
while(true) {
print(s);
}