I gave this solution at the codechef for the problem code : FCTRL.
I saw the compile time of others people using the same the language c ( i am using c++ gcc 4.8.1) is somewhat less,
mine is 0.46s while their is 0.23
Can somebody help me in reducing the time if possible?
#include<iostream>
using namespace std;
int main()
{
long int t,i,temp;
cin>>t;
long int n[t],a[t];
for(i=0;i<t;i++)
{
temp=1;
a[i]=0;
cin>>n[i];
while(temp)
{
temp=n[i]/5;
a[i]+=temp;
n[i]=n[i]/5;
}
}
for(i=0;i<t;i++)
cout<<a[i]<<"\n";
return(0);
}
From your description, as you are using c++ and they are using c, it might be due to how the compiler handles each instruction.
Also you may try replacing
temp=n[i]/5;
a[i]+=temp;
n[i]=n[i]/5;
By
temp=n[i]/5;
a[i]+=temp;
n[i]=temp; //why compute the value again
And see if some time reduces or not
You worst offense to C++ is using Variadic-Length arrays, those are non-standards.
And in fact, it turns out you absolutely do not need them. This problem can be solved on a line-per-line basis so using arrays to hold the input and output is useless.
Here is your program, simplified. I also noted that temp was useless within the loop (though it was likely optimized out anyway, it polluted the code).
#include <iostream>
int main()
{
size_t number = 0;
std::cin >> number;
for(size_t i = 0 ; i < number; ++i)
{
size_t a = 0, n = 0;
std::cin >> n;
while (n)
{
n /= 5;
a += n;
}
std::cout << a << '\n';
}
}
Is it possible to do better ? Oh yes! The main issues here is that the C++ streams are none too fast so you may get a good boost by switching to the C reading methods... however they are not as nice (and safe).
Related
Here is my code. I am trying to get the entire program to end if it goes into the second if statement inside the do-while loop. But every time I run it, it crashes. I am not sure what I am doing wrong.
#include <iostream>
using namespace std;
int main() {
int myData[10];
for(int i=0;i<10;i++){
myData[i] = 1;
cout<<myData[i];
}
do{
int i;
cout<<endl<<"Input index: ";
cin>> i;
int v;
cout<<endl<<"Input value: ";
cin>>v;
if(i>=0||i<10){
myData[i]=v;
for(int i=0;i<10;i++){
cout<<myData[i]<<" ";
}
}
if (i<0||i>=10){
cout<<"Index out of range. Exit.";
return 0;
}
}while(1);
}
if(i>=0||i<10){
Think about which numbers are either greater than zero or less than ten. I'm sure you realise that is true of all numbers. What you meant to write is
if(i>=0&&i<10){
This explains your crash, you are accessing the myData array with an index that is outside the array bounds.
It's very common for beginners to get && and || confused especially where there is negation involved as well.
Other people have told you the problem:
if(i>=0||i<10){
I'm going to explain why you hit this. You hit it because you didn't use any whitespace.
if (i >= 0 && i < 10) {
Please, please, please, for all that is good in the world, use whitespace liberally. You absolutely will have fewer bugs, because my version is far easier to read. Furthermore if you work with older developers (like me) we can actually read your code far more easily.
#include <iostream>
using namespace std;
int main() {
int i,t,km,sum=0;
std::cin >> t;
for( i=0;i<t;i++){
cin>>km;
}
for(i=0;i<t;i++){
if(km>300){
sum=km*10;
cout<<sum;
}
else if(km<=300){
sum=300*10;
cout<<sum;
}
else{
cout<<"wrong!";
}
}
return 0;
}
i'm not getting what's wrong with the code, when i'm entering number of test cases(t) as 1 it's running. but afterwars it's only executing the else block.
We'll start with a mini code review:
#include <iostream>
using namespace std; // Bad practice; avoid
/* Generally poor formatting throughout */
int main() {
int i,t,km,sum=0; // Prefer each variable declared on its own line; i is unnecessary
std::cin >> t;
for( i=0;i<t;i++){ // Overwrites `km` t times
cin>>km;
}
for(i=0;i<t;i++){ // Only does work on the last number entered
if(km>300){
sum=km*10; // You likely want to be adding on, not overwriting
cout<<sum;
}
else if(km<=300){
sum=300*10;
cout<<sum;
}
else{ // Impossible to reach given your two other conditions
cout<<"wrong!";
}
}
return 0;
}
The comments have spelled a lot of this out. You overwrite km t times. You only end up running your check on the last value of km that was entered, t times. You likely want to process t inputs, and the way to do this is with a single loop instead of the two that you have.
You overwrite your sum instead of (I assume) adding on to it. You wanted to sum += and not just sum =. The computer is actually pretty dumb, but it's very good at doing exactly what you told it to do. It is incapable of guessing your intent. That's supposed to be comforting, but it can be interpreted many ways.
I would also recommend taking the time to come up with better variable names. It looks like you're doing some calculations, possibly having to do with legs of a trip, but it's unclear. Making your code harder to read by choosing horrible names like t won't allow others to easily help you, and it will make your own code seem like a foreign language after just a couple days. Help yourself and others out by choosing good names.
As I stated, you might have been able to figure this out on your own if the code was indented properly. Consistent and proper formatting is extremely important for readability. If you don't want to be bothered doing it yourself, tools like clang-format exist.
Here's your code again, touched up a bit. I took a couple guesses at intended behavior.
#include <iostream>
int main() {
int t;
int km;
int sum = 0;
std::cin >> t;
for (int i = 0; i < t; i++) {
std::cin >> km;
if (km > 300) {
sum += km * 10;
std::cout << sum << '\n';
} else if (km <= 300 && km > 0) {
sum += 300 * 10;
std::cout << sum << '\n';
} else {
std::cout << "wrong!";
}
}
return 0;
}
i am stuck on a problem where, after taking input of an array and sorting it and not doing any operation on it at all, the output shows a different array?
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
int n;
cin>>n;
long long int c[n],h[n],a[n];
for(int i=0;i<n;i++){
cin>>c[i];
}
for(int i=0;i<n;i++){
cin>>h[i];
}
sort(h,h+n);
for(int i=0;i<n;i++){
a[i]=0;
}
int i=0;
int begin=(i+1)-c[i];
int end = (i+1)+c[i];
int j=begin;
while(i<n){
a[j-1]++;
j++;
if(j>end){
i++;
begin=(i+1)-c[i];
end= (i+1)+c[i];
j=begin;
}
}
sort(a,a+n);
for(int i=0;i<n;i++){
cout<<h[i]<<" ";
}
}
return 0;
}
input for h[]={8,8,8,8,8}..n=5
output h[]={10,10,9,9,8}
Here is a version of your code written in reasonably decent C++. I didn't touch the loop in the middle because I have no clue what it's doing. You're using obscure variable names and no comments and doing all kinds of bizarre things with indexes and mixing them up with user input.
Now, reading indexes from user input and using them isn't bad, though in a real program you'd want to be doing lots of bounds checking on that input to make sure people weren't feeding you bad data. But doing all that stuff with such poorly named variables with no explanation is going to leave anybody looking at it scratching their head. Don't do that.
Also, avoid the use of begin and end as variable names, especially if they hold indexes. In most cases it will confuse things terribly as begin and end are important identifiers in the standard library and always refer to iterators, which are sort of like indexes, but most definitely not indexes, adding greatly to the confusion. beginidx and endidx could be acceptable substitutes in this case.
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
using ::std::vector;
using ::std::sort;
using ::std::copy_n;
using ::std::copy;
using ::std::fill;
using ::std::istream_iterator;
using ::std::ostream_iterator;
using ::std::cin;
using ::std::cout;
int main() {
// your code goes here
using vec_el_t = long long int;
int t;
cin >> t;
while (t--) {
int const n = []() { int n; cin >> n; return n; }();
vector<vec_el_t> c{n}, h{n}, a{n};
copy_n(istream_iterator<vec_el_t>{cin}, n, c.begin());
copy_n(istream_iterator<vec_el_t>{cin}, n, h.begin());
// Suggested debugging code:
// cout << "h before sort: "
// copy(h.begin(), h.end(), ostream_iterator<vec_el_t>{cout, " "});
// cout << '\n';
sort(h.begin(), h.end());
// Suggested debugging code:
// cout << "h after sort: "
// copy(h.begin(), h.end(), ostream_iterator<vec_el_t>{cout, " "});
// cout << '\n';
fill(a.begin(), a.end(), 0);
// Weird, unexplained algorithm begins here
int i = 0;
int begin = (i + 1) - c[i];
int end = (i + 1) + c[i];
int j = begin;
while (i < n) {
a[j - 1]++;
j++;
if (j > end){
i++;
begin = (i + 1) - c[i];
end = (i + 1) + c[i];
j = begin;
}
}
// Weird unexplained algorithm ends here
sort(a.begin(), a.end());
copy(h.begin(), h.end(), ostream_iterator<vec_el_t>{cout, " "});
}
return 0;
}
Changes made... Use vector not a variable length array, which isn't valid C++ anyway and will only work in g++ and clang. Don't use explicit loops if there is an algorithm that will do the job. Try to make as many things const as you can so you can make sure that the compiler catches it if you try to change things you didn't mean to change. Avoid using std; and if you want to import names from ::std import exactly the ones you need. Don't use compiler or library implementation specific header files and use the ones from the standard instead (i.e. no bits/stdc++.h).
As for your problem, I have no idea. I suspect that the index manipulation combined with looping isn't doing what you expect. If you print out the arrays before and after sorting, you will discover that sort only alters order, and not content.
As a general rule, always suspect your own code first and make absolutely sure it's correct. And if you really think it's the library code, prove it beyond a shadow of a doubt before coming here to ask why the library isn't doing what it says it does.
The complicated code I didn't touch looks rife with opportunities for out-of-bounds access, and that results in undefined behavior, which means your program might do absolutely anything in that case. You might change uses of operator [] with calls to the at function (one of the many perks of using vector) instead. That way, attempts at out-of-bounds access will throw an exception.
Within these lines you are accessing a outside its limits:
int i=0;
int begin=(i+1)-c[i]; // begin = 1 - c[0]; <<-- this could be arbitrarily small!
int end = (i+1)+c[i]; // unrelated
int j=begin; // also equal to 1-c[0]
while(i<n){
a[j-1]++; // increment a[-c[0]] which is UB unless c[0]==0
This means undefined behavior (UB), i.e., it could do nothing, it could segfault, or (what apparently happened in your case) access elements of an adjacent data structure.
Question:Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.
(Because of this i am not able to submit answer onto SPOJ)
My code:
#include <stdio.h>
void main()
{
int a[5],i,j;
printf("Enter some numbers:");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
if(a[i]==42)
{
j=i;
break;
}
}
for(i=0;i<j;i++)
{
printf("\n%d",a[i]);
}
return 0;
}
j has not been initialized where it is defined. It will continue to be uninitialized if you never enter the block that has the line
j = i;
Initialize j to 5 at the start of the program to avoid using an uninitialized value, which leads to undefined behavior.
You can also change the final loop to use j as the loop counter and i as the stopping value.
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
if(a[i]==42)
{
++i;
break;
}
}
for(j=0;j<i;j++)
{
printf("\n%d",a[j]);
}
ideone use GCC compiler to compile C programs. Your code have obsolete syntax which is valid only in obsolete compiler like Turbo C++ or older version of GCC.
Change void main() to int main(void) and it will work.
Please i am stuck at this question for half an hour and can't find why the error comes?
Problem code : test
Life, Universe and Everything
#include<iostream>
using namespace std;
int main()
{
int a[20],i;
cin>>a[0];
for(i=1;a[i-1]!=42;i++)
{
cout<<a[i]<<"\n";
cin>>a[i];
}
return(0);
}
Your code tries to access non-existing array elements, which causes segfault. You should stop your loop before the array index gets larger than the length of the array minus 1:
int a[20];
for (i = 1; i < 20 && a[i - 1] != 42; i++)
{
// ...
}
Apart from limit problem, your printing elements without initializing them
//for i = 1, a[i] is uninitialized
cout<<a[i]<<"\n";
On accessing a local variable (like this), you're likely to get garbage value.
This might be better substitute for what you are trying to do:
int a[20],i;
for(i=0;i < 20;i++)
{
cin>>a[i];
if (a[i] == 42)
break;
cout<<a[i]<<"\n";
}
You are trying to print the uninitialized data...
#include<iostream>
using namespace std;
int main()
{
int a[20],i;
cin>>a[0]; // a[0] uninitialized
for(i=1;a[i-1]!=42;i++)
{
cout<<a[i]<<"\n";
cin>>a[i];
}
return(0);
}
In the for loop get the data first and then print it.Your array size is 20 but you are trying to write upto 42.
You use array values before initializing them. C++ doesn't initialize non-static arrays for you unless you tell it to, so $DEITY knows what's in there. And technically, whatever's in there could cause an exception...or any number of other things. (For ints, on an x86 machine, that's actually highly unlikely. But elsewhere, it's possible.)
The user can enter more than 20 numbers. That's really just a special case of the more general problem, though: You allow unknown number of entries, but aren't able to accept them all without crashing.
If you don't know beforehand how many objects there will be, use a vector.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
std::vector<int> a;
int entry;
cin>>entry;
// Oh, yeah. This. You really, *really* want to check `cin`.
// Otherwise, if the user decided to type "i like cheese", you'd loop
// forever getting zeros while cin tried to parse a number.
// With the array, that'd typically cause a segfault near instantly.
// With a vector, it'll just eat up all your CPU and memory. :P
while (cin && entry != 42) {
a.push_back(entry);
cout << entry << "\n";
cin >> entry;
}
return 0;
}