当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;
信创产业现状、分析与预测
Multi person collaborative data annotation based on Baidu brain easydata from scratch
Exercise 6-2 using functions to sum special A-string sequences
Exercise 10-1 judge the three digits that meet the conditions
Article content typesetting and code highlighting
JVM object lifecycle
Qt学习21 Qt 中的标准对话框(下)
[email protected])"/>
金属有机骨架(MOFs)抗肿瘤药载体|PCN-223装载甲硝唑|UiO-66包载盐酸环丙沙星([email protected])
Summary of common error reporting problems and positioning methods of thrift
随机推荐
Qt学习19 Qt 中的标准对话框(上)
Exercise 7-6 count capital consonants
Metal organic framework (MOFs) antitumor drug carrier | pcn-223 loaded with metronidazole | uio-66 loaded with ciprofloxacin hydrochloride(
The small project (servlet+jsp+mysql+el+jstl) completes a servlet with login function, with the operation of adding, deleting, modifying and querying. Realize login authentication, prevent illegal log
QT learning 21 standard dialog box in QT (Part 2)
RocksDB LRUCache
JVM runtime data area
Exercise 6-1 classify and count the number of characters
[ACNOI2022]猜数
叶酸修饰的金属-有机骨架(ZIF-8)载黄芩苷|金属有机骨架复合磁性材料([email protected])|制备路线
selenium 浏览器(1)
核酸修饰的金属有机框架药物载体|PCN-223金属有机骨架包载Ad金刚烷|ZIF-8包裹阿霉素(DOX)
How to promote the progress of project collaboration | community essay solicitation
Go 1.16.4: purpose of go mod tidy
Exercise 10-1 judge the three digits that meet the conditions
“又土又穷”的草根高校,凭什么被称为“东北小清华”?
Qt学习21 Qt 中的标准对话框(下)
JS new challenges
Qt学习20 Qt 中的标准对话框(中)
Onmenusharetimeline custom shared content is invalid, and the title and icon are not displayed