试题编号: | 201912-1 |
试题名称: | 报数 |
时间限制: | 1.0s |
内存限制: | 512.0MB |
ccf真题及答案,属于简单的模拟题,用数组表示甲乙丙丁对应的跳过次数
设置变量 i 从1开始报数,直到cnt计数为n个时退出循环。
每次对 i 进行判断,如果是7的倍数或是含有7则“跳过” :cnt不变,数组对应位置+1
最后按顺序输出即可。
ccf2021答案。
#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
int n,ans[4];//用数组分别表示甲乙丙丁跳过的次数
bool judge(int x){ //判断是否是7的倍数或是含有7if(x % 7 == 0) return true; //true表示需要跳过while(x != 0){int a = x % 10;if(a == 7) return true;x /= 10;}return false;
}
int main(){scanf("%d",&n);int cnt = 0; //报数的总个数for(int i = 1;;i++){ //用 i 表示当前的数字if(judge(i)) ans[(i-1)%4]++; //因为数字是从1开始 所以减去1再余4else cnt++;if(cnt == n) break;}printf("%d\n%d\n%d\n%d",ans[0],ans[1],ans[2],ans[3]);return 0;
}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态