Digit Separation

Limits: 1s, 512 MB

Nowadays newly joined programmers of MU_ACM are doing incredible. Within few months they have learnt how to play with integers , arrays as well as strings . More or less of them are capable of separating all digits from an integer even big-integer is nothing to them.


This is a companion discussion topic for the original entry at https://toph.co/p/digit-separation

@hjr265
minor problems in the test cases.

I am looking at your second submission that yields Wrong Answer on sample and it seems you are not calling .strip() on the s.

Doesn’t looping over sys.stdin keep the newline character in the iteration variable? s actually has the newline character and you need to get rid of it by calling .strip() on it.

Sorry, Didn’t know the keeping '\n' fact.

1 Like

Wait, then why this yeilds WA ?

@hjr265
I think you have not noticed it.

Oh, right. Thanks for the reminder. I have added it to my to-dos. I will look into this.

1 Like

@hjr265

The input says “You will be given a number.”
But I’m given 2 numbers.

#include<bits/stdc++.h>
using namespace std;
int main(){
    stringstream ss;
    int i,sum=0,k;
    char s1;
    string s;
    while(getline(cin,s)){
        for(i=0;i<s.length();i++){
            if(s[i]=='-'){
                i++;
                s1=s[i];
                ss<<s1;
                ss>>k;
                k=k*-1;
            }
            else{
                s1=s[i];
                ss<<s1;
                ss>>k;
            }
            sum=sum+k;
            ss.clear();
        }
        cout<<sum<<endl;
        sum=0;
    }
}

getting wrong answer in test case 3

Your third submission now passes for this problem.

n=input()
s=0
for x in n:
    s=s+int(x)
print(s)

why it is showing wrong answer for 1

import java.util.Scanner;
public class aa {
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
String s=x.next();
int sum=0;

	for(int i=0;i<s.length();i++) {
		if(Character.isDigit(s.charAt(i))) 
		{sum=sum+Character.getNumericValue(s.charAt(i));
		}
		
	}
	System.out.println(sum);
}

}
why there is showing wrong answer for 1st case?