Code: Select all
int firstDiv(int &n, const VI &v)
{
if (n < 0)
{
cout << "-1";
n = -n;
return 0;
}
......
}
-2147483647 and 2147483647 can be hold in an "int", no?
sorry about my poor english
Moderator: Board moderators
Code: Select all
int firstDiv(int &n, const VI &v)
{
if (n < 0)
{
cout << "-1";
n = -n;
return 0;
}
......
}
then why it crashes?Jan wrote:(+-)2147483647 both fit into normal int.
this is not the code of 583 problem... but the function is similar:Jan wrote:Can you post your full code?
Code: Select all
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
int firstDiv(int &n, const VI &v)
{
if (n < 0)
{
cout << "-1";
n = -n;
return 0;
}
// here will be another portion of code
return 0;
}
int main()
{
int n;
cin >> n;
VI v(n);
cout << firstDiv (n, v) << endl;
}
Code: Select all
-2147483647
Code: Select all
VI v(n);
sorry sorry sorry!Jan wrote:Did you notice this?
You are giving -2147483647 as n. vector<int> v(-2147483647). That's why your program crashed.Code: Select all
VI v(n);
Code: Select all
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
int firstDiv(int &n, const VI &v)
{
if (n < 0)
{
cout << "-1";
n = -n;
return 0;
}
// here will be another portion of code
return 0;
}
int main()
{
int n;
cin >> n;
VI v(10000);
cout << firstDiv (n, v) << endl;
}
if so ...why my code isnt working for 2147483647??Jan wrote:(+-)2147483647 both fit into normal int.
Code: Select all
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i,minflag=0;
while(scanf("%d",&n))
{
if(n==0)break;
printf("%d = ",n);
if (n<0)
{
minflag=1;
printf("-1 x ");
n=abs(n);
}
for(i=2;i*i<=n;i++)
{
while(n%i==0)
{
printf("%d",i);
n/=i;
if(n>1)printf(" x ");
}
}
if(n!=1)
printf("%d",n);
printf("\n");
}
return 0;
}
Code: Select all
for(i=2;i*i<=n;i++)
Code: Select all
#include <cstdio>
int absValue(int a){
if(a<0)
return -a;
else
return a;
}
int main(){
int num,i;
bool s;
while(scanf("%d",&num) == 1 && num){
s = false;
if(absValue(num) == 1){
printf("%d = %d\n",num,num);
continue;
}
printf("%d =",num);
if(num < 0){
num = -num;
s = true;
}
if(s)
printf(" -1 x");
i = 2;
while(num/i){
if(num%i == 0){
while(num%i == 0){
num /= i;
if(num/i == 0)
printf(" %d",i);
else
printf(" %d x",i);
}
}
i++;
}
if(num>1){
printf("x %d",num);
}
puts("");
}
return 0;
}
newton wrote:why TLE plz help
whene i solved the problem after generating primes. But now if i dont handle only with primes the code gets TLE.But as i am setting a conditionCode: Select all
#include <cstdio> int absValue(int a){ if(a<0) return -a; else return a; } int main(){ int num,i; bool s; while(scanf("%d",&num) == 1 && num){ s = false; if(absValue(num) == 1){ printf("%d = %d\n",num,num); continue; } printf("%d =",num); if(num < 0){ num = -num; s = true; } if(s) printf(" -1 x"); i = 2; while(num/i){ if(num%i == 0){ while(num%i == 0){ num /= i; if(num/i == 0) printf(" %d",i); else printf(" %d x",i); } } i++; } if(num>1){ printf("x %d",num); } puts(""); } return 0; }
if(num%i == 0)
so this will process only the primes. So why still TLE?
Thanks
Code: Select all
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
typedef unsigned long inte;
void ptr(inte w){
inte n=w;
inte rez=0;
inte i=2;int flag=0;
while(i<=sqrt(w)+5){
if(i>=sqrt(w) && flag!=0){cout<<n;break;}
if(n%i==0){
flag++;
cout<<i;
n/=i;
if(n!=1)cout<<" x ";
else break;
}
else {
if(i==2)i++;
else i+=2;
}
}
if(flag==0)cout<<w;
}
int main()
{
while(1){
long n;cin>>n;
if(n==0)break;
cout<<n<<" = ";
if(n<0){cout<<" -1 x ";n*=-1;}
ptr(n);
cout<<endl;
}
return 0;
}