## 1225 - Digit Counting

Moderator: Board moderators

atul
New poster
Posts: 7
Joined: Thu Sep 27, 2012 4:21 pm

Problem no 1225 ,,,
I am getting WA.. plz help

this is my code

#include <stdio.h>
#include <stdlib.h>

int main()
{
int a[20],i,j,n,m,b,g,s;
scanf("%d",&n);
for(s=1;s<=n;s++)
{
for(i=0;i<20;i++)
{
a=0;
}
scanf("%d",&m);
for(i=1;i<=m;i++)
{
g=i;
if(i<10)
{
a=a+1;
}
else
{
while(g!=0)
{
b=g%10;
g=g/10;
a=a+1;
}
}
}

for(j=0;j<=9;j++)
{
printf("%d",a[j]);
printf(" ");
}
if(s!=n)
printf("\n");
}
return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Don't print a space at the end of a line.
Check input and AC output for thousands of problems on uDebug!

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

### 1225 - Digit Counting

Here's some input / output I found useful while testing / debugging.

There's no extra space after the 10th number in the output.

Input:

Code: Select all

``````4
9999
22
71
649``````
AC Output:

Code: Select all

``````2889 4000 4000 4000 4000 4000 4000 4000 4000 4000
2 13 6 2 2 2 2 2 2 2
7 18 17 17 17 17 17 9 7 7
124 235 235 235 235 225 175 125 125 125``````
Check input and AC output for over 7,500 problems on uDebug!

vsha041
New poster
Posts: 35
Joined: Wed Feb 12, 2014 10:04 am

### Re: 1225 - Digit Counting

Thanks. But the strange part is that for some of the problems we don't get presentation error even if we put an extra space/spaces after the last number.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 1225 - Digit Counting

On problems with a special judge you may be able to get away with extra spaces and still get AC.
Check input and AC output for thousands of problems on uDebug!

vsha041
New poster
Posts: 35
Joined: Wed Feb 12, 2014 10:04 am

### Re: 1225 - Digit Counting

brianfry713 wrote:On problems with a special judge you may be able to get away with extra spaces and still get AC.
oh okay, thanks brian, actually i have made a rule to not put spaces if they haven't asked for as this has caused lot of suffereing. That's because in some cases extra spaces give wrong answer instead of presentation errors.

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

### Re: 1225 - Digit Counting

vsha041 wrote:That's because in some cases extra spaces give wrong answer instead of presentation errors.
Yes. Don't count on extra spaces and / or newlines to give you a PE. More often than not, you'll get a WA (and this can be misleading).
Check input and AC output for over 7,500 problems on uDebug!

Mrsuit
New poster
Posts: 14
Joined: Mon May 05, 2014 8:41 pm

### Re: 1225 - Digit Counting

Hey, i have the same problem, first i got PE, then i fixed a little thing about the spaces, but now i got WA. And it's because that thing, when i put

Code: Select all

``````9999
22
71
649``````
i got

Code: Select all

``````2889 4000 4000 4000 4000 4000 4000 4000 4000
2 13 6 2 2 2 2 2 2
7 18 17 17 17 17 17 9 7
124 235 235 235 235 225 175 125 125``````
So, how can i fix this?.
Thanks

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 1225 - Digit Counting

Check input and AC output for thousands of problems on uDebug!

Mrsuit
New poster
Posts: 14
Joined: Mon May 05, 2014 8:41 pm

### Re: 1225 - Digit Counting

Code: Select all

``````import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;

public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
StringBuilder n = new StringBuilder();
StringBuilder sb= new StringBuilder();

while (i>0){
sb.delete(0,sb.length());
int [] numbers = new int [10];
for (int k = 0; k < numbers.length; k++) {
numbers[k]=0;
}
while (j>0){
sb.append(j);
j-=1;
}
String line=sb.toString();
String [] list= line.split("");

for (int k = 1; k < list.length; k++) {
if(Integer.parseInt(list[k])==0){
numbers[0]=numbers[0]+1;
}
if (Integer.parseInt(list[k])==1){
numbers[1]=numbers[1]+1;
}
if (Integer.parseInt(list[k])==2){
numbers[2]=numbers[2]+1;
}
if (Integer.parseInt(list[k])==3){
numbers[3]=numbers[3]+1;
}
if (Integer.parseInt(list[k])==4){
numbers[4]=numbers[4]+1;
}
if (Integer.parseInt(list[k])==5){
numbers[5]=numbers[5]+1;
}
if (Integer.parseInt(list[k])==6){
numbers[6]=numbers[6]+1;
}
if (Integer.parseInt(list[k])==7){
numbers[7]=numbers[7]+1;
}
if (Integer.parseInt(list[k])==8){
numbers[8]=numbers[8]+1;
}
if (Integer.parseInt(list[k])==9){
numbers[9]=numbers[9]+1;
}
}
for (int k = 0; k < numbers.length; k++) {
if (k<8){
n.append(numbers[k]);
}
if (k==9){
n.append(numbers[k]);
}
}
n.append("\n");

i-=1;
}
System.out.println(n.substring(0,n.length()-1));

}
}

``````
Thank you so much!

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 1225 - Digit Counting

For the sample input, you're printing:
011100000
162211111
0 1 1 1 0 0 0 0 0 0
1 6 2 2 1 1 1 1 1 1
Check input and AC output for thousands of problems on uDebug!

Mrsuit
New poster
Posts: 14
Joined: Mon May 05, 2014 8:41 pm

### Re: 1225 - Digit Counting

brianfry713 wrote:For the sample input, you're printing:
011100000
162211111
0 1 1 1 0 0 0 0 0 0
1 6 2 2 1 1 1 1 1 1
I already fixed it, and got AC. Thank you!.

minor_coder
New poster
Posts: 2
Joined: Tue Sep 15, 2009 9:46 am

### Re: 1225 - Digit Counting

Can't understand what is the problem? at first i got PE then fixing the spaces now WA. Please help

Code: Select all

``````#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n;
cin>>n;
for (int i = 0; i < n; i++)
{
int input;
cin>>input;
vector<int> result(10,0);
for (int j = 1; j <= input; j++)
{
string s = to_string(j);
for (int k = 0; k < s.length(); k++)
{
char ch = s[k];
int x = ch - 48;
result[x]++;
}
}
for (int j = 0; j < 10; j++)
{
if(j != 0 && i!= 9)
cout<<" ";
cout<<result[j];
}
if(i != n-1)
cout<<endl;
}
return 0;
}
``````

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

### Re: 1225 - Digit Counting

Change code to

Code: Select all

``````for (int j = 0; j < 10; j++)
{
if (j != 0)
cout << " ";

cout << result[j];
}
cout << endl;
``````
Don't forget to remove your code after getting accepted.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

xrenon
New poster
Posts: 10
Joined: Tue Sep 23, 2014 4:11 am

### Re: 1225 - Digit Counting

DELETED
Thank You
Last edited by xrenon on Sun Oct 26, 2014 11:17 am, edited 1 time in total.