统计数字问题
2007-08-22 20:06
405 查看
一本书的页码从自然数1开始顺序编码直到自然数n.书的页码按照通常的习惯编排,每个页码都不含多YU的前导数字0.例如,第6页用数字6表示,而不是06或006等.数字计数问题要求对给定书的总页码n,计算出书的全部页码中分别用到多少次数字0,1,2...9.
给定表示书的总页码的十进制数n(1<=n<=10^9).
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Test
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static int c[]=new int[10];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void main(String[] args) throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str=bf.readLine();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
go(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<c.length;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(i+" "+c[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void go(String str)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int cout=-1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String s="1";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String tmp="";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
boolean flag=false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int n,m,r,sum,pre;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pre=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sum=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m=Integer.parseInt(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<str.length()-1;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s=s+"0";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
do...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
n=Integer.parseInt(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r=(m-pre)/n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(flag)...{tmp=tmp+(new Integer(pre)).toString().charAt(cout);flag=false;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<tmp.length();i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c[tmp.charAt(i)-48]+=r*n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<r;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c[i]=c[i]+(int)Math.pow(10,s.length()-1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<c.length;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c[i]=c[i]+r*(s.length()-1)*(int)Math.pow(10,s.length()-2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pre=r*n+pre;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(m-pre<n)...{flag=true;s=s.substring(0,s.length()-1);cout++;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}while(m-pre>10);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sum=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<str.length();i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sum = sum+(int)Math.pow(10,i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c[0]-=sum;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=pre;i<=m;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String st=""+i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int j=0;j<st.length();j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c[st.charAt(j)-48]++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
给定表示书的总页码的十进制数n(1<=n<=10^9).
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Test
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static int c[]=new int[10];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void main(String[] args) throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str=bf.readLine();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
go(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<c.length;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(i+" "+c[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void go(String str)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int cout=-1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String s="1";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String tmp="";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
boolean flag=false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int n,m,r,sum,pre;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pre=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sum=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m=Integer.parseInt(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<str.length()-1;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s=s+"0";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
do...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
n=Integer.parseInt(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r=(m-pre)/n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(flag)...{tmp=tmp+(new Integer(pre)).toString().charAt(cout);flag=false;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<tmp.length();i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c[tmp.charAt(i)-48]+=r*n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<r;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c[i]=c[i]+(int)Math.pow(10,s.length()-1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<c.length;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c[i]=c[i]+r*(s.length()-1)*(int)Math.pow(10,s.length()-2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pre=r*n+pre;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(m-pre<n)...{flag=true;s=s.substring(0,s.length()-1);cout++;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}while(m-pre>10);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sum=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<str.length();i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sum = sum+(int)Math.pow(10,i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c[0]-=sum;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=pre;i<=m;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String st=""+i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int j=0;j<st.length();j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
c[st.charAt(j)-48]++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}