【csp报数-循环链表实现】

【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;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值