CSP-201912-1-报数
知识点总结
- 整数转化为字符串
#include <string> string str_num = to_string(num);
- 字符串中查找是否包含字符‘7’:
str_num.find('7')
- 未找到返回-1
- 找到返回返回该字符在字符串中的位置(即第一次出现的索引位置)
#include<iostream>
#include <string>
using namespace std;
int aws[4];
int main() {int n, num = 1;cin >> n;for (int i = 0; i < n;){string str_num = to_string(num);if (num % 7 == 0 || str_num.find('7') != -1) aws[(num - 1) % 4]++; // 不报数else i++; // 报数num++;}for (int i = 0; i < 4; i++){cout << aws[i] << endl;}return 0;
}