机试刷题记录
一、刷题
L1-007 念数字
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805136889593856?type=7&page=0
错误答案:
#include<stdio.h>
#include<math.h>
int x,y;
char str[10][5]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
void dayin(int num)
{
x=num/10;
y=num%10;
dayin(x);
printf("%s ",str[y]);
}
int main()
{
int num;
scanf("%d",&num);
if(num<0) printf("fu ");
dayin(num);
printf("%s ",str[y]);
return 0;
}
正确答案
#include<stdio.h>
#include<math.h>
char nums[10][5] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
void dayin(int num)
{
int s_num = num%10;
num = num/10;
if(num!=0)
{
dayin(num);
printf(" %s",nums[s_num]);
}
else{
printf("%s",nums[s_num]);
}
}
int main()
{
int num;
scanf("%d",&num);
if(num < 0) {
num = -num;
printf("fu ");
}
dayin(num);
return 0;
}
L1-017 到底有多二
题目链接:https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805121500692480?type=7&page=0
#include<stdio.h>
#include<math.h>
#include<string.h>
char num[55];
int main()
{
scanf("%s",num);
int len = strlen(num);
int is_even = 0, is_negtive = 0, digit_count = len, two_count = 0;
if(num[0]=='-') is_negtive = 1, digit_count -= 1;
if((num[len-1]-'0')%2==0) is_even = 1;
int i;
for(i=0;i<len;++i)
{
if(num[i]=='2') two_count += 1;
}
double ans = (double)two_count/(double)digit_count;
if(is_negtive) ans *= 1.5;
if(is_even) ans *= 2;
ans *= 100;
printf("%.2f%%",ans);
return 0;
}