內容
題目會給一個數字,從1開始到這個數字,加總全部數字出現的次數。
Input
The input file consists of several data sets. The first line of the input file contains the number of datasets which is a positive integer and is not bigger than 20. The following lines describe the data sets.For each test case, there is one single line containing the number N.
第一行是測資數量,不超過二十,接下來每個數字各一行。
Output
For each test case, write sequentially in one line the number of digit 0, 1, . . . 9 separated by a space.
輸出0到9出現的次數,中間以空格分開
Sample Input
2
3
13
Sample Output
0 1 1 1 0 0 0 0 0 0
1 6 2 2 1 1 1 1 1 1
C++
#include<iostream>
using namespace std;
int n;
int main()
{
while(cin>>n)//測資數量
{
while(n--)
{
int number,count[11]={0},re;
cin>>number;//輸入數字
//計算出現數字次數
for(int i =1;i<=number;i++)// i 是每個數字
{
int e=i;//e拿來計算
while(e)//計算該數出現的數字的次數
{
re=e%10;//re是該數最右邊的數字
count[re]++;
e/=10;//e除以10以計算下一個數字
}
}
for(int i=0;i<=9;i++)//輸出
{
cout<<count[i];
if(i!=9)
{
cout<<" ";
}
}
cout<<"\n";
}
}
return 0;
}
python
x = int(input())
for k in range(x):
n = int(input())
count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for i in range(1, n+1):
s = str(i)
for j in range(len(s)):
count[int(s[j])] = count[int(s[j])]+1
print(count)
請先 登入 以發表留言。