Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi i am kind of new in c++. I was trying to optimize my code. My code includes two for loops with a if block inside the second loop. first loop will iterate for 10^14 times and inner for loop will iterate for 10^4 times. My code is as folows
#include<iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
#include<stdio.h>
#include<fstream>
using namespace std;
#include<math.h>
#include<thread>
using namespace std;
signed long long run,i,j;
int main()
{
run=0;
for (i=0;i<100000000000000;i++)
{
for (j=0;j<10000;j++)
{
run=run+1;
}
}
cout<<run<<"\n";
}
time it is takling to complete is around 1 day. So I was using thread in my code to make it first. But it is showing to include -std=c++0x. So where to include this?
Is there anyone who would like to help me out?
I'd optimise it as follows, but good compilers might do that anyway:
#include<iostream>
signed long long run,i,j;
int main()
{
i = 100000000000000;
j = 10000;
run = i * j;
std::cout << run << '\n';
}
-std=c++0x is a compiling option, and it tells the compiler you are using C++11 standard.
You might want to take a look at a link like this one http://www.cs.cf.ac.uk/Dave/C/node3.html regarding compiling.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 days ago.
Improve this question
The programe takes n number of test cases and each test case takes takes 4 values a,b,c,d from user and these values are used to calculate the value of max.
#include<bits/stdc++.h>
using namespace std;
void numofjok(int ar[3], int arr[4]);
int main(){
cout << "Enter the no of test cases:";
int n=0,max=0,al=0,bo=0;
int a,b,c,d;\\user input
int arr[4] ={a,b,c,d}, ar[3]= {al,bo,max};
cin>>n;\\test_cases
while(n--){
cin>>a>>b>>c>>d;\\during runtime it takes infinte no of values.
numofjok(ar,arr);
}
return 0;
}
}
I have tried the taking input using for loop but the problem presist.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Could somebody explain the solution to this problem in cpp and how it works
Can this be made more efficient using STL?
http://www.codechef.com/problems/H1
It would very helpful if you could explain this particular logic along with its time complexity.
#include <cstdio>
using namespace std;
int board[9],q[178000]={123456789},powers[9]={100000000,10000000,1000000,100000,10000,1000,100,10,1};
int posn,fromposn,front=-1,back=1;
char found[98765435]={0};
bool prime[18]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1};
void moveit(int p1,int p2){
int diff=board[p2]-board[p1];
posn=fromposn+diff*powers[p1]-diff*powers[p2];
int posndiv10=posn/10;
if(!found[posndiv10]){
found[posndiv10]=found[fromposn/10]+1;
q[back++]=posn;
}
}
int main(){
int t,i,d;
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
found[12345678]=1;
while(++front<back){
for(posn=fromposn=q[front],i=8;i>=0;i--){
board[i]=posn%10;
posn/=10;
}
if(prime[board[0]+board[1]]) moveit(0,1);
if(prime[board[0]+board[3]]) moveit(0,3);
if(prime[board[1]+board[2]]) moveit(1,2);
if(prime[board[1]+board[4]]) moveit(1,4);
if(prime[board[2]+board[5]]) moveit(2,5);
if(prime[board[3]+board[4]]) moveit(3,4);
if(prime[board[3]+board[6]]) moveit(3,6);
if(prime[board[4]+board[5]]) moveit(4,5);
if(prime[board[4]+board[7]]) moveit(4,7);
if(prime[board[5]+board[8]]) moveit(5,8);
if(prime[board[6]+board[7]]) moveit(6,7);
if(prime[board[7]+board[8]]) moveit(7,8);
}
scanf("%d",&t);
while(t--){
for(posn=i=0;i<8;i++){
scanf("%d",&d);
posn=posn*10+d;
}
scanf("%d",&d);
if(found[posn]) printf("%d\n",found[posn]-1);
else puts("-1");
}
return 0;
}
It first builds a table of all the solvable boards, represented as numbers, that says how many steps from the solution that board is.
Then it solves each test case by looking it up in that table.
The time complexity per test case is constant.
I doubt that there is anything in the standard library that could make it more efficient.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I was trying to solve a really simple problem on UVa online judge. The problem code is: 10071. You can find the problem here: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94
My code looks like this:
#include<iostream>
using namespace std;
int main(){
int v,t,a,d;
cin >> v >> t;
t = t*2;
d = (v)*t;
cout << d << endl;
}
But it says wrong answer. What went wrong and how to solve it?
You have not read the complete question.
Correct solution is as follows:
#include <stdio.h>
int main()
{
int a,b,c;
while(scanf("%d%d",&a,&b)==2)
{
printf("%d\n",(a*b)*2);
}
return 0;
}
As you may notice above, there can be multiple test cases. You have to account for it. So I have a while loop for it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I use MS Visual Studio and I am new to C++, so I am just wondering if there is an faster more efficient way to do multiple test cases instead of keep clicking CTRL+F5 and re-opening the console many times.
Like for example if I have this code
#include <iostream>
using namespace std;
void main ()
{
int x;
cout<<"Enter a number"<<endl;
cin>>x;
cout<<x*2<<endl;
}
Is there a way I could try different values of x at once and getting the results together?
Thanks
Simple work around:
while(terminating_condition_is_not_met)
{
execute_what_you_want
}
terminating condition can be EOF or max_no_of_iterations or some_sentinel_value
And for your code, I used -1 as a sentinel.
#include <iostream>
using namespace std;
void main ()
{
int x;
while(1)
{
cout<<"Enter a number"<<endl;
cin>>x;
if(x==-1)
break;
cout<<x*2<<endl;
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
My console app looks like that.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a, b;
cin>>a>>b;
cout<<"% "<<a%b<<endl<<"fmod "<<fmod(a,b)<<endl;
system("pause");
return 0;
}
I'm newbie to C++ and I got 2 questions:
Writing this application on VS. Why do I need to include "stdafx.h"? Is there any requirement? What is this?
Is there any difference between fmod and % ? Getting exactly same results for them:
Thx in advance..
Writing this application on VS. Why do I need to include "stdafx.h"? Is there any requirement? What is this?
Because the default project setting says you need precompiled header (See this).
You can disable this manually. Select Not Using Precompiled Headers as shown in the image below:
Is there any difference between fmod and % ? Getting exactly same results for them:
Yes. % cannot operate on floating-pointer numbers, while fmod can. f in fmod indicates floating-point.
Try this:
float a, b;
std::cin>>a>>b;
std::cout << (a%b) << std::endl; //it will give compilation error.
fmod( a , b ) will cast the int variables a and b to floats when the parameters are passed. Depending on the type of a and b (for instance, if you use std::uint64_t) you might lose precision during the cast and get something incorrect returned. The return type will also be a float and will need to be cast again if you're using the function with int types. You should stick to % for int types. Using fmod is less efficient.