【csp报数-循环链表实现】 #include <iostream> #include<vector> #include<stack> #include<unordered_map> #include<string> using namespace std; struct people { int name; int num = 0; people* next; }; people *head; class Solution { public: bool isseven(int m) {//判断数字是否含7 while (m !=0) { if (m % 10 == 7) { return 1; } m = m / 10; } return 0; } }; int main() {//循环链表的建立思路很简单:先建立单链表,然后将最后的节点指向最开始的节点 head = new people; head->name = 1; people *ptemp = head; for (int i = 2;i<=4;i++) { people* node; node = new people; node->name = i; ptemp->next = node; ptemp = node; } ptemp->next = head;//循环链表建立完成 people*first = head;//循环链表的移动指针 Solution s; int count = 1; int n; cin >> n; for (int i = 0;i < n;i++) {//模拟过程 if (count % 7 == 0 || s.isseven(count)) { first->num++; i--; } first = first->next; count++; } for (int i = 0;i < 4;i++) {//注意不要使用移动指针first遍历, //因为在上面的循环模拟中,first最终不一定停留在甲位置 cout << head->num<<endl; head = head->next; } }