当前位置:网站首页>6-9 statistics of single digits (15 points)
6-9 statistics of single digits (15 points)
2022-07-03 14:10:00 【Big fish】
6-9 Statistics are in single digits (15 branch )
This question requests to realize a function , It can count the number of occurrences of a bit in any integer . for example -21252 in ,2 There is 3 Time , The function should return 3.
Function interface definition :
int Count_Digit ( const int N, const int D );
among N and D All parameters passed in by the user .N The value of is not more than int The scope of the ;
D yes [0, 9] Single digit in interval . Function must return N in D Number of occurrences .
sample input :
-21252 2
sample output :
3
Ideas
Pay attention to find the absolute value first , Otherwise, the remainder function cannot be calculated correctly
Sample referee test procedure :
#include <stdio.h>
int Count_Digit ( const int N, const int D );
int main()
{
int N, D;
scanf("%d %d", &N, &D);
printf("%d\n", Count_Digit(N, D));
return 0;
}
/* Your code will be embedded here */
Test point Tips result Time consuming Memory
0 sample, Numbers <0 And appear discontinuously The answer right 2 ms 384 KB
1 Numbers >0, Continuous occurrence The answer right 2 ms 256 KB
2 N=0, And output 1 The answer right 2 ms 280 KB
3 Output is 0 The answer right 2 ms 364 KB
4 Statistics 0 The number of times , There may be more cycles 1 The answer right 2 ms 256 KB
Before optimization
int Count_Digit ( const int N, const int D ){
int tmp;
int res;
int arr[10]={0};
if(N>=0){
res=N;
}else{
res=N*(-1);
}
if(res==D)return 1;// Handle N=0 And D=0 In special circumstances , By the way |N|==D The situation of
while(res!=0){
tmp=res%10;
if(D==tmp){
arr[tmp]++;
}
res=res/10;
}
for(int i=0;i<10;i++){
if(arr[i]>=1){
tmp=arr[i];
return tmp;
}
}
return 0;
}
After optimization
int Count_Digit ( const int N, const int D ){
int temp=N;
if(N<0)temp*=-1;
if(temp==D)return 1;// Handle N=0 And D=0 In special circumstances , By the way |N|==D The situation of
int times=0;// Number of occurrences
for(int i=temp;i>0;i/=10){
if(i%10==D)times++;
}
return times;
}
边栏推荐
- Collection of mobile adaptation related articles
- Go: send the get request and parse the return JSON (go1.16.4)
- Article content typesetting and code highlighting
- Exercise 9-1 time conversion
- Scroll detection, so that the content in the lower right corner is not displayed at the top of the page, but is displayed as the mouse slides
- page owner特性浅析
- Vite project commissioning
- How to promote the progress of project collaboration | community essay solicitation
- Summary of common error reporting problems and positioning methods of thrift
- 7-11 calculation of residential water charges by sections
猜你喜欢
Multi person collaborative data annotation based on Baidu brain easydata from scratch
GoLand 2021.2 configure go (go1.17.6)
FPGA测试方法以Mentor工具为例
JS input number and standard digit number are compared. The problem of adding 0 to 0
好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录
JVM object lifecycle
[Jilin University] information sharing of postgraduate entrance examination and re examination
JS Part 2
Redis:字符串類型數據的操作命令
Go language web development series 30: gin: grouping by version for routing
随机推荐
Implementation of Muduo asynchronous logging
QT learning 25 layout manager (4)
GoLand 2021.1: rename the go project
Exercise 6-1 classify and count the number of characters
核酸修饰的金属有机框架药物载体|PCN-223金属有机骨架包载Ad金刚烷|ZIF-8包裹阿霉素(DOX)
Current situation, analysis and prediction of information and innovation industry
JVM class loading
Uniapp tips - scrolling components
Qt学习23 布局管理器(二)
Dynamic programming 01 knapsack and complete knapsack
Global event bus
How to delete an attribute or method of an object
[ACNOI2022]猜数
Rasp implementation of PHP
Similarities and differences of sessionstorage, localstorage and cookies
[acnoi2022] guess numbers
Scroll detection, so that the content in the lower right corner is not displayed at the top of the page, but is displayed as the mouse slides
QT learning 21 standard dialog box in QT (Part 2)
Webpage connection database ~ simple implementation of addition, deletion, modification and query complete code
Cross linked cyclodextrin metal organic framework loaded methotrexate slow-release particles | metal organic porous material uio-66 loaded with flavonoid glycosides | Qiyue