Hi, I wanted do this exercise https://www.spoj.com/problems/ALCATRAZ1/ without string. Why i get “wrong answer”? this is my code:
#include <iostream> using namespace std; int main() { int T; long long int N; cin>>T; for(int i=1;i<=T;++i) { cin>>N; long int s=0; for(;;) { s=s+N%10; N=(N-N%10)/10; if(N==0) break; } cout<<s<<endl; } return 0;
Compare the maximum number that a long long int can hold, with the maximum number that can be given in the test cases. See the problem?
you don’t need to N=(N-N%10)/10; this line. Because if you divide two integer result will be integer. so u can write this way N /= 10;
N=(N-N%10)/10;
N /= 10;