#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,count=0;
    cin>>n;
    string s;
    for(int i=0;i<n;i++){
        cin>>s[i];
    }
    for(int i=0;i<n;i++){
        int predir=s[(i-1+n)%n]-'0';
        int nextdir=s[(i+1)%n]-'0';
        if(predir!=nextdir){
            continue;
        }
        else{
            if(predir==0&amp;&amp;s[i]=='0'){s[i]=1+'0';count++;}
            if(predir==1&amp;&amp;s[i]=='1') {s[i]='0';count++;}
            
        }
    }
    cout<<count<<endl;
}

#include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

typedef struct LNode{
int data;
LNode* next;
}LNode, *LinkList;

//bool Initlink(LNode&amp; l) {
//l = (LNode*)malloc(sizeof(LNode));
//l->next = NULL;
//return true;
//}

LinkList tailLink(int n) {  //尾插法
LinkList L = new LNode;
L->data = 9999;
L->next = NULL;
LNode* p, * r;
int number;
r = L;
for (int i = 0; i < n; i++) {
p = new LNode;
cin >> number;
p->data = number;
p->next = NULL;
r->next = p;
r = p;
}
return L;
}

LinkList headLink(int n) { //头插法
LinkList L = new LNode;
L->data = 9999;
L->next = NULL;
LNode* p;
int number;
for (int i = 0; i < n; i++) {
p = new LNode;
cin >> number;
p->data = number;
p->next = L->next;
L->next = p;
}
return L;
}

void printList(LinkList L) {
LNode* p;
p = L->next; //跳过头节点
while (p) {
cout << p->data << &quot; &quot;;
p = p->next;
}
cout << endl;
}

void reverseList(LinkList L) { //原地翻转
if (L == NULL) return;
else {
LNode *p, *r;
p = L->next; //新链表
L->next = NULL; 
while (p != NULL) { //头插法把剩下的节点插进去
r = p->next;
p->next = L->next; 
L->next = p; 
p = r;
}
}
}

int main() {
int n;
cin >> n;
LinkList L= headLink(n);
printList(L);
reverseList(L);
printList(L);
return 0;
}

#include<iostream>

using namespace std;

const int MAX_SIZE = 10010;

class Stack {
private:
int data[MAX_SIZE];
int topIndex;
public:
Stack() {
topIndex = -1;
}

//入栈
void push(int num) {
if (full()) {
cout << &quot;The Stack is full&quot; << endl;
}
else {
data[++topIndex] = num;
}
}

//出栈
void pop() {
if (empty()) {
cout << &quot;The Stack is empty&quot; << endl;
}
else {
topIndex--;
}
}

//判空
bool empty() {
return topIndex == -1;
}

//判满
bool full() {
return topIndex == MAX_SIZE - 1;
}

//返回栈顶元素
int query() {
return data[topIndex];
}
};

int main() {
Stack s;
int n, num;
string ml;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> ml;
if (ml == &quot;push&quot;) {
cin >> num;
s.push(num);
}
else if (ml == &quot;pop&quot;)
s.pop();
else if (ml == &quot;empty&quot;) {
if (s.empty()) cout << &quot;YES&quot; << endl;
else cout << &quot;NO&quot; << endl;
}
else cout << s.query() << endl;
}
return 0;
}

#include<iostream>

using namespace std;

bool f(int num) {
for (int i = num - 1; i > num / 2; i--)
{
if (num % i == 0) return false;
}
return true;
}

int main() {
int i = 101, count=0;
for (; i < 200; i++) {
if (f(i)) {
count++;
cout << i << &quot; &quot;;
}
}

return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务