Quantcast
Channel: Online Judge System - SPOJ Discussion board
Viewing all 2136 articles
Browse latest View live

Two circles efficiency

$
0
0

@godzila555 wrote:

i have come across lots of java codes for some problems which seem easy to solve, but some people write codes which include additional classes and the program runs faster. for example there is an easy task: http://www.spoj.com/problems/SMPCIRC/
i saw some kinds of solutions of this problem. one of them is this:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
    static class CircleCoord {
        int cX;
        int cY;
        int r;
 
        public CircleCoord(int cx, int cy, int r) {
            this.cX = cx;
            this.cY = cy;
            this.r = r;
        }
 
        public int getXTerm(int x) {
            return (x - cX) * (x - cX);
        }
 
        public int getYTerm(int y) {
            return (y - cY) * (y - cY);
        }
    }
 
    static class TestCase {
        CircleCoord o1;
        CircleCoord o2;
 
        public TestCase(CircleCoord o1, CircleCoord o2) {
            this.o1 = o1;
            this.o2 = o2;
        }
 
        public CircleCoord getO1() {
            return o1;
        }
 
        public CircleCoord getO2() {
            return o2;
        }
    }
 
    public static void main(String[] args) {
        TestCase[] testCases = readTestCases();
 
        if (testCases == null) {
            testCases = new TestCase[0];
        }
 
        for (TestCase testCase : testCases) {
            CircleCoord o1 = testCase.getO1();
            CircleCoord o2 = testCase.getO2();
 
            double distance = Math.sqrt(Math.pow((o1.cX - o2.cX), 2) + Math.pow(o1.cY - o2.cY, 2));
 
            if (distance == Math.abs(o1.r - o2.r)) {
                System.out.println('E');
            } else if (distance < Math.abs(o1.r - o2.r)) {
                System.out.println('I');
            } else {
                System.out.println('O');
            }
        }
    }
 
    private static TestCase[] readTestCases() {
        TestCase[] testCases = new TestCase[0];
 
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
 
        try {
            int n = Integer.parseInt(reader.readLine().trim());
            testCases = new TestCase[n];
 
            int index = 0;
            while (index < n) {
                String[] coord = reader.readLine().trim().split(" ");
                testCases[index++] = new TestCase(new CircleCoord(Integer.parseInt(coord[0]), Integer.parseInt(coord[1]), Integer.parseInt(coord[2])), new CircleCoord(Integer.parseInt(coord[3]), Integer.parseInt(coord[4]), Integer.parseInt(coord[5])));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
                reader = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        return testCases;
    }
 
}

which uses additional classes but is faster than this simple solution:

import java.util.Scanner;
 
 
public class Main{
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scan = new Scanner (System.in);
 
		int cases= scan.nextInt(); 
 
		for (int i = 0; i < cases; i++){
			
				double x1= scan.nextInt();
				double y1= scan.nextInt();
				double r1= scan.nextInt();
				double x2= scan.nextInt();
				double y2= scan.nextInt();
				double r2= scan.nextInt();
 
				double d= Math.sqrt((Math.pow(x1-x2,2)+Math.pow(y1-y2,2)));
 
				if (d==Math.abs(r1-r2)){
					System.out.println("E");
					
				}
 
				else if(d<=Math.abs(r1-r2)){
					System.out.println("I");
				}
				
				else {
					System.out.println("O");
				}
 
			}
 
		}
	}

i wonder how this happens. There is an additional class, therefore additional objects in the first solution and some test case, whereas the second one has only the test case. could you explain this to me? since this is not the first time i see such long solutions with additional classes which runs faster than simply written programs. Please, explain what slows down the second solution.

Posts: 1

Participants: 1

Read full topic


ACODE problem

$
0
0

@amit_yadav wrote:

I am getting Wrong Answer.Can somebody tell where i am wrong?

int main() {
vector result;
string str;
cin >> str;
while(str[0]!='0')
{
int len=str.length();
long long dp[len+1];
/*Initializing dp to 0*/
for(int i=0; i

  /*base case*/
  dp[0]=1;

  int num;
  int i=1; //i for index

  while(i<len)
  {
     num=(str[i-1]-'0')*10 + (str[i]-'0');
     
     if(str[i]-'0'!=0 && (num>=10 && num<=26) )    
     {
        dp[i]=dp[i-1] + dp[(i-2)<0?0:i-2];
     }	
     else{
        dp[i] = dp[i-1];
     }
     
     i++;	
  }
  result.push_back(dp[len-1]);
  cin >> str;

}

for(int i=0; i cout << result[i] << endl;
}
return 0;
}

Posts: 2

Participants: 2

Read full topic

WA in EKO

Character Patterns (Act 5)

$
0
0

@adi_tdkr wrote:

Anyone can please give me solution or any hint about how to solve Character Patterns (Act 5) problem.

Posts: 2

Participants: 1

Read full topic

Java submission for SUMSUM

$
0
0

@amit_gh wrote:

The java solution for SUMSUM problem does not seem to be getting accepted due to some sort of limit on the output length. I made by own test case with N=2000 and Q=5000 and ran it successfully on eclipse. But the solution when run on ideone gives answer 0 after 1596 test case of type 2. In a separate program (http://ideone.com/RynAqs) I tried printing long numbers (~10^15) in a loop and found that after 2896 outputs, the output stops. Is this some sort of buffer limit on ideone console output and how will bigger test cases pass for Java? My code, due to this reason, gives wrong answer.
Other languages like C++ have a higher limit and ideone shows this weird behavior after many outputs which is why those solutions pass. I don't want to convert my code to C++. Any idea what could be the solution...

Posts: 2

Participants: 1

Read full topic

Expedi w/a

$
0
0

@mirrorcraze wrote:

I don't know which test case I get WA.

My algorithm is to check fuel station where car can go, then get the max fuel. Does it wrong?

include

struct fuelstation{
int pos;
int fuel;
};
void swapp(fuelstation &a,fuelstation &b)
{
fuelstation temp=a;
a=b;
b=temp;
}
void upheap(fuelstation A[], int index)
{
if(index !=1 && A[index].pos > A[index/2].pos)
{
swapp(A[index],A[index/2]);
upheap(A,index/2);
}
else return;
}
void downheap(fuelstation A[],int index,int counter)
{
int doubleindex = 2*index;
if(doubleindex > counter)
return;
if(doubleindex+1>counter)
{
if(A[doubleindex].pos > A[index].pos)
{
swapp(A[index],A[doubleindex]);
downheap(A,doubleindex,counter);
}
else return;

}
if(A[doubleindex].pos > A[index].pos)
{
    if(A[doubleindex].pos < A[doubleindex+1].pos)
    {
        swapp(A[index],A[doubleindex+1]);
        downheap(A,doubleindex+1,counter);
    }
    else
    {
        swapp(A[index],A[doubleindex]);
        downheap(A,doubleindex,counter);
    }
}
else
{
    if(A[doubleindex+1].pos >A[index].pos)
    {
        swapp(A[index],A[doubleindex+1]);
        downheap(A,doubleindex+1,counter);
    }
    else return;
}

}
fuelstation getmax(fuelstation A[])
{
return A[1];
}
void popmax(fuelstation A[],int &counter)
{
A[1]=A[counter];
counter--;
if(counter == 0)
return;
downheap(A,1,counter);
}
void buildheap(fuelstation A[],int counter)
{
for(int i=counter/2;i>=1;i--)
downheap(A,i,counter);
}
main()
{
int t;
scanf("%d",&t);
if(t==0)
{
return 0;
}
for(int k=0;k {
int n;
scanf("%d",&n);
fuelstation A[10010];
for(int i=1;i<=10000;i++)
{
A[i].pos=0;
A[i].fuel = 0;
}
for(int i=1;i<=n;i++)
{
scanf("%d %d",&A[i].pos,&A[i].fuel);
}
fuelstation current;
scanf("%d %d",&current.pos,&current.fuel);
buildheap(A,n);
int counter = n;
int fillfuel = 0;
bool arrive=true;
while(current.pos > current.fuel)
{
int maxfuel=0;
int maxpos=0;
if(counter==0)
{
printf("-1\n");
arrive = false;
break;
}
else if(current.pos - getmax(A).pos > current.fuel)
{
printf("-1\n");
arrive = false;
break;
}
while(current.pos - getmax(A).pos <= current.fuel && counter>0)
{
if(getmax(A).fuel>maxfuel)
{
maxfuel = getmax(A).fuel;
maxpos = getmax(A).pos;
}
popmax(A,counter);
//printf("%d %d\n",maxfuel,maxpos);
}
current.fuel = current.fuel -current.pos+maxpos + maxfuel;
current.pos = maxpos;
fillfuel++;
//printf("Pos : %d Fuel : %d\n",current.pos,current.fuel);
//printf("Counter :%d\n",counter);
}
if(arrive)
printf("%d\n",fillfuel);
}
return 0;
}

Posts: 1

Participants: 1

Read full topic

BOOK problem

$
0
0

@s7b5en wrote:

Hey guys.
I've been trying to solve a problem called Booklets for past hours.
It's idea isn't that difficult, you can read it here
But I am getting WA.
I've tested it many times, but still can't find the mistake.
Hope, you'll be able to help me.
Here is the JAVA code:


//spoj.com/problems/BOOK
import java.util.*;

public class BOOK {
static Scanner input = new Scanner( System.in );
public static void main(String[] args)
{
int numCases = input.nextInt();
for(int i = 0; i < numCases; i++)
{
System.out.println(solution());
}
} // end of main

public static int solution()
{
	int schools = input.nextInt(); // num of schools
	int specifiedSchool = input.nextInt(); // a schools specified by boss(start counting from 0)
	int booklets = input.nextInt(); // num of booklets
	List<Integer> initialPages = new ArrayList<Integer>(); // pages in each booklet
	for(int i = 0; i < booklets; i++){
		initialPages.add(input.nextInt());
	} // filling the number of pages in each booklet
	List<Integer> pages = new ArrayList<Integer>(initialPages);
	Collections.sort(pages);// Pages(A) < Pages(B) < Pages(C) (LOOK AT THE SPOJ PAGE)
	//System.out.println("Uip is: " + uip + "\n" + "Lip is: " + lip);
	int specifiedCounter = 0; // to count the number where to give output

	while(schools > 0)
	{	
		int uip = (int) Math.ceil((double) booklets/schools);  // Upper integral part e.g 10/3 = 4
		int lip = (int) Math.floor((double) booklets/schools); // Lower integral part e.g 10/3 = 3
		if (specifiedCounter == specifiedSchool){
			int first = pages.get(0);
			for(int i : pages){
				if ( initialPages.indexOf(i) < initialPages.indexOf(first) ) 
					{
						first = i;
					}
			}
			return first;//print out the required number of pages
			//in the first booklet of the specified school.
		}
		if ( ( (float) booklets/schools ) > lip ){
			//System.out.println((float) booklets/schools);
			//System.out.println("UIP");
			schools -= 1;
			booklets -= uip;
			pages = pages.subList(uip, pages.size());
		}

		else{
			schools -= 1;
			booklets -= lip;
			pages = pages.subList(lip, pages.size());
		}
		specifiedCounter += 1;


	}// end of while loop
	 return 0;
}

}

Posts: 2

Participants: 1

Read full topic

Average!

$
0
0

@namanjain2050 wrote:

Average of two numbers not printing properly..
#include iostream
#include iomanip
using namespace std;
float avg(int a,int b)
{
float temp = static_cast((a+b)/2);
return temp;
}
int main() {
float m;
m = avg(1,2);
cout< return 0;
}

Posts: 1

Participants: 1

Read full topic


WA in ONP (Transform the expression) in java

$
0
0

@nishu94 wrote:

I am repeatedly getting WA though I have checked each possible corner case.
import java.util.*;
import java.lang.*;

class Main
{
	char ar[];
	int top,tmp;
	
	public Main()
	{
		ar=new char[401];
		top=-1;
		tmp=-1;
	}
	
	void push(char c)
	{
		ar[++top]=c;
		
	}
	
	char pop()
	{
		if(top==-1)
			return '0';
	 tmp=top;
	 top--;
	 
		 return ar[tmp];
		
	}
	
	char top()
	{
		if(top==-1)
			 return '0';
		
		return ar[top];			 
	}
	
	public  static boolean isOperator(char c)
	{
		if((c=='+')||(c=='-')||(c=='*')||(c=='/')||(c=='^'))
			return true;
	   
		return false;
	}
	
	public  int prio(char c)
	{
		switch(c)
		{
		
		case '+' : return 1;
		           
		case '-' : return 2;
		           
		case '*' : return 3;
		           
		case '/' : return 4;
		           
		case '^' : return 5;
		           
		    
		}
		
		return -1;
	}

	public static void main (String[] args) throws java.lang.Exception
	{
		int i,k;
		Main n = new Main();
		Scanner in = new Scanner(System.in);
		int t=in.nextInt();
		char exp[],res[];
		
		while(t!=0)
		{
			k=0;
			exp=in.next().toCharArray();
			res=new char[exp.length];

			i=0;
			while(i<exp.length)
			 {
		 		if(exp[i]=='(')
		 		{ n.push(exp[i]); i++;}
		 		else if(exp[i]==')')
		 		{ 
		 			while(n.top()!='0'&&n.top()!='(') 
		 			{ res[k++]=n.top();
		 			  n.pop();
		 			
		 			}
		 		 
		 			if(n.top()=='(')
		 				n.pop(); 
		 		 
		 			i++;
		 		}
		 		else if(!isOperator(exp[i]))
		 			{res[k++]=exp[i]; i++;}
		 		else
		 		{
		 			if(n.prio(exp[i])<(n.prio(n.top())))
		 			{
		 				res[k++]=n.pop();
		 			}
		 			else if(n.prio(exp[i])==(n.prio(n.top())))
		 			{
		 				res[k++]=n.pop();
		 				i++;
		 				n.push(exp[i]);
		 			}
		 			else 
		 			{
		 				n.push(exp[i]);
		 				i++;
		 			}
		 			
		 			
		 			
		 		}
		 		
		 	   	
		 		
		     } 
			
			//System.out.println(exp.length);
			t--;
			while(n.top()!='0')
				  res[k++]=n.pop();	
			res[k]='\0';
			System.out.println(res);
		}
		

		
	}
}

Posts: 1

Participants: 1

Read full topic

Solution to Character Patterns (Act 5)

$
0
0

@adi_tdkr wrote:

My code is perfect and it cannot be wrong I have checked all possible test cases but still it is giving me wrong answer why?
#include
#include

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
int r,c,s,i,j,k,m,n,l,x,y,p,q;
scanf("%d%d%d",&r,&c,&s);
for(x=0;x<1;x++){
    for(y=0;y<(s*c)+(c+1);y++){
        printf("*");
    }
}
printf("\n");
for(p=1;p<=r;p++){
for(i=1;i<=s;i++){
        printf("*");
     for(k=1;k<=c;k++){
            if(k%2==0){
    for(j=s;j>=1;j--){
        if(i==j){
            printf("/");
        }else{
        printf(".");
        }
    }
    printf("*");
}else{
for(j=1;j<=s;j++){
    if(i==j){
            printf("\\");
        }else{
        printf(".");
        }
    }
    printf("*");
}
}
printf("\n");
}
for(x=0;x<1;x++){
    for(y=0;y<(s*c)+(c+1);y++){
        printf("*");
    }
}
printf("\n");
}
    }
    return 0;
}

Posts: 3

Participants: 2

Read full topic

What is wrong with the following LCA Code

$
0
0

@ps06756 wrote:

Hello , I want to solve the following problem http://www.spoj.com/problems/LCASQ/ .
I am using the square root approach described in the Topcoder tutorial here:-https://www.topcoder.com/community/data-science/data-science-tutorials/range-minimum-query-and-lowest-common-ancestor/

My code is here :- http://ideone.com/wyirYk
I have tried a lot of test cases, but I am unable to understand on which test case is my code failing. Can someone please tell what is wrong with the code ?

Posts: 1

Participants: 1

Read full topic

Minimum Average Waiting Time

WA in SUBSN

$
0
0

@bluefa wrote:

Can you help me?
I don't know what's wrong in this code.

please help me.

include

using namespace std;
const int N = 1e5+2;
const int M = 205;
//char s[N],t[M];
string s,t;
vector a[30];
int main(){
int l,T,i,j,n,k;
scanf("%d",&T);
getchar();
for(k=1;k<=T;++k){
getline(cin,s);
n = s.size();
for(i=0;i<26;++i)
a[i].clear();
for(i=0;i a[s[i]-'a'].push_back(i);
int q,ql,pos,c,nex;
printf("Case %d:\n",k);
scanf("%d",&q);
getchar();
while(q--){
getline(cin,t);
ql = t.size();
if(ql<=0){
cout<<"YES\n";continue;
}
pos = 0;
bool go=1;
for(i=0;i c = t[i]-'a';
if(a[c].size()==0){
go=0;break;
}
nex = lower_bound(a[c].begin(),a[c].end(),pos)-a[c].begin();
if(nex>=a[c].size() ){
go=0;break;
}
if(a[c][nex] go=0;break;
}
pos = a[c][nex];
}
if(go)
printf("YES\n");
else
printf("NO\n");
}
}
}

Posts: 1

Participants: 1

Read full topic

I Need Help For The IWGBS - 0110SS Problem

$
0
0

@shrikantbadri wrote:

I don't understand why am getting wrong answer in this question even though my code works fine for all the constraints. Link for my code is: http://ideone.com/9BeiZP
Even it works for N=10000, it generates nearly 2020 digits, which I consider is right. Please look into my solution, & correct me. :mask:

Posts: 3

Participants: 2

Read full topic

Runtime error in java for MTREE

$
0
0

@amit_gh wrote:

My recursive solution for MTREE in Java is giving runtime error. For a linear tree input of 4500 nodes, it gives runtime error on ideone whereas the same input on a recursive c++ solution gives answer. What should I do as a java programmer? Should I write difficult iterative code or just change my language for graph based questions?

There are only 2 java submissions as of now.

Posts: 1

Participants: 1

Read full topic


Getting WA in COINS

$
0
0

@flyingduchman_ wrote:

Can someone give me a test case for which my solution is wrong:

#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>

using namespace std;
long long unsigned f(long long unsigned n);
long long unsigned temp=0;
map<long long unsigned,int>a;

int main()
{
    long long unsigned n,ans=0;
    while(scanf("%lld",&n)==1)
    {
        a.clear();
        temp=0;
        ans=f(n);
        printf("%lld\n",ans);

    }
    return 0;
}


long long unsigned f(long long unsigned n)
{
    if(n==0)
    {
        return 0;
    }
    if(a[n] != 0)
    {
        return a[n];
    }
    else
    {
        if(a[n]==0)
        {
            temp=f(n/2)+f(n/3)+f(n/4);
        }
    }
    if(n>temp)
    {
        a[n]=n;
    }
    else
    {
        a[n]=temp;
    }
}

Posts: 3

Participants: 3

Read full topic

Candy 1

$
0
0

@amitking wrote:

What's wrong with my code ?

Problem : http://www.spoj.com/problems/CANDY/

#include<iostream>
using namespace std;
int main()
{
int b,n[10000],sum,mov,c,avg;
while(cin>>b)
{
if(b!=-1)
{
sum=0,mov=0,c=0;
for(int i=0;i<b;i++)
{
cin>>n[i];
sum+=n[i];
avg=sum/b;
if((sum%b==0)&&(n[i]>avg))
{
c=n[i]-avg;
}
mov+=c;
}
if(mov==0)
cout<<"-1\n";
else
cout<<mov<<"\n";
}
else
return 0;
}
return 0;
}

Posts: 2

Participants: 2

Read full topic

Julka wrong answer

$
0
0

@godzila555 wrote:

hello, please help me to find out why this solution gets WA. here is the problem: http://www.spoj.com/problems/JULKA/
please check out for some values. it seems right to me

include

include

using namespace std;

int main() {
for(int w=0; w<10; ++w){
string result="";
string sum;
string more;
cin>>sum;
cin>>more;
int carry=0;
for(int j=0; j if(sum[sum.length()-1-j]-more[more.length()-1-j]-carry<0){
result=(char)(sum[sum.length()-1-j]-more[more.length()-1-j]-carry+10+'0')+result;
carry=1;
}
else{
result=(char)(sum[sum.length()-1-j]-more[more.length()-1-j]-carry+'0')+result;
carry=0;
}
}
for(int k=sum.length()-more.length()-1; k>=0; --k){
if(sum[k]-carry-'0'<0){
result=(char)(sum[k]-carry+10)+result;
carry=1;
}
else{
result=(char)(sum[k]-carry)+result;
carry=0;
}
}
int k=0;
while(true){
if(result.length()==1 || result[k]!='0')break;
else result.erase(k, 1);
++k;
}
string candies1="";
bool isremainder=false;
for(int i=0; i if((result[i]-'0')%2==0){
if(!isremainder){
candies1=candies1+(char)((result[i]-'0')/2+'0');
isremainder=false;
}
else{
candies1=candies1+(char)((10+result[i]-'0')/2+'0');
isremainder=false;
}
}
else{
if(!isremainder){
if((10*(result[i]-'0')+result[i+1]-'0')/20!=0)candies1=candies1+(char)((10*(result[i]-'0')+result[i+1]-'0')/20+'0')+(char)(((10*(result[i]-'0')+result[i+1]-'0')/2)%10+'0');
else candies1=candies1+(char)(((10*(result[i]-'0')+result[i+1]-'0')/2)%10+'0');
++i;
if((result[i+1]-'0')%2==1)isremainder=true;
}
else{
candies1=candies1+(char)(((10+result[i]-'0')/2)%10+'0');
if((result[i]-'0')%2==0){
isremainder=false;
}
}
}
}
k=0;
while(true){
if(candies1.length()==1 || candies1[k]!='0')break;
else candies1.erase(k, 1);
++k;
}
string candies2="";
int carry2=0;
for(int i=0; i if((int)(candies1.length()-1-i)>=0 && (int)(more.length()-1-i)>=0){
if(candies1[candies1.length()-1-i]-'0'+more[more.length()-1-i]-'0'+carry2>=10){
candies2=(char)((candies1[candies1.length()-1-i]-'0'+more[more.length()-1-i]-'0'+carry2)%10+'0')+candies2;
carry2=1;
}
else{
candies2=(char)((candies1[candies1.length()-1-i]-'0'+more[more.length()-1-i]-'0'+carry2)%10+'0')+candies2;
carry2=0;
}
}else if((int)(candies1.length()-1-i)>=0 && (int)(more.length()-1-i)<0){
if(candies1[candies1.length()-1-i]-'0'+carry2>=10){
candies2=char((candies1[candies1.length()-1-i]-'0'+carry2)%10+'0')+candies2;
carry2=1;
}
else{
candies2=char((candies1[candies1.length()-1-i]-'0'+carry2)%10+'0')+candies2;
carry2=0;
}
}else if((int)(candies1.length()-1-i)<0 && (int)(more.length()-1-i)>=0){
if(more[more.length()-1-i]-'0'+carry2>=10){
candies2=char((more[more.length()-1-i]-'0'+carry2)%10+'0')+candies2;
carry2=1;
}
else{
candies2=char((more[more.length()-1-i]-'0'+carry2)%10+'0')+candies2;
carry2=0;
}
}
}
if(carry2==1)candies2=string("1")+candies2;

	cout<<candies2<<endl;
	cout<<candies1<<endl;
	}
return 0;

}

Posts: 1

Participants: 1

Read full topic

PRIME 1 TLE (EOFError)

$
0
0

@jame wrote:

I submitted this following code for prime 1 it works fine on my pycharms but here judge shows tle can someone point out the problem in the code `

import sys
 def primes_sieve(limit):
    limitn = limit+1
    not_prime = set()
    primes = []

   for i in range(2, limitn):
       if i in not_prime:
           continue

      for f in range(i*2, limitn, i):
           not_prime.add(f)

      primes.append(i)

   return primes

for i in range(int((raw_input()))):
    num1,num2=map(int,raw_input().split())
    primes=primes_sieve(num2)
    for i in primes:
        if i in xrange(num1,num2+1):
              print i
sys.exit(0)

Posts: 2

Participants: 2

Read full topic

fr_03_11 - modulo 10

$
0
0

@xk404 wrote:

Mam problem z zadaniem polegającym na sprawdzeniu podzielności liczby dwójkowej przez 10(tj 1010 binarnie)
Teoretycznie jest to dość proste - sprawdzić podzielności przez 2(10 binarnie) i 5 (101 binarnie), a w praktyce - WA
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10division
{
class Program
{
static void Main(string[] args)
{
int cases = int.Parse(Console.ReadLine());
for (int i = 0; i < cases; i++)
{
bool ans = false;
char[] num = Console.ReadLine().ToCharArray();
if (num[num.Length - 1] == '0')
{
int odd = 0;
int even = 0;
int j = num.Length - 2;
while (j > 2)
{
odd += num[j] - 48;
j--;
odd += (num[j] - 48) * 2;
j--;
even += num[j] - 48;
j--;
even += (num[j] - 48) * 2;
j--;
}
switch (j)
{
case 0:
odd += num[j] - 48;
break;
case 1:
odd += num[j] - 48;
j--;
odd += (num[j] - 48) * 2;
break;
case 2:
odd += num[j] - 48;
j--;
odd += (num[j] - 48) * 2;
j--;
even += num[j] - 48;
break;
}
if((odd-even)%5==0)
{
ans = true;
}
}
if (ans)
{
Console.WriteLine("TAK");
}
else
{
Console.WriteLine("NIE");
}
}
}
}
}

Czy ktoś może mi wskazać, gdzie popełniam błąd?

Posts: 1

Participants: 1

Read full topic

Viewing all 2136 articles
Browse latest View live