C语言循环单向链表法

#include<stdio.h>
#include<stdlib.h>
typedef struct Person
{char Name[8];struct Person* Next;
}Person;
typedef struct CircleSit{Person* FirstPerson;Person* LastPerson;int RemainingNum;
}CircleSit;
Person* NewPerson(char name[8])
{Person* New;New = (Person*)malloc(sizeof(Person));strcpy(New->Name,name);New->Next = NULL;return New;
}
CircleSit* InitialCircle(Person* person)
{CircleSit* operateObject;operateObject = (CircleSit*)malloc(sizeof(CircleSit));operateObject->FirstPerson = person;operateObject->LastPerson = person;operateObject->LastPerson->Next = operateObject->FirstPerson;strcpy(operateObject->FirstPerson->Name,person->Name);operateObject->RemainingNum = 1;return operateObject;}
int FoundCircle(CircleSit* operateObject,const int SeatsCount,FILE* InputNames)
{Person* pNew;int i;for (i = 1; i < SeatsCount; i++){char name[8];fscanf(InputNames, "%s", &name);pNew=NewPerson(name);operateObject->LastPerson->Next = pNew;operateObject->LastPerson = pNew;operateObject->LastPerson->Next = operateObject->FirstPerson;operateObject->RemainingNum++;}return 1;
}
int PrintCircle(CircleSit* Base)
{Person* p=Base->FirstPerson;do {printf("%s\n", p->Name);p = p->Next;} while (p != Base->FirstPerson);printf("Remain %d Persons\n", Base->RemainingNum);return 1;
}
int JosephExecute(CircleSit* Base,const int StopStepCount)
{Person* pre=Base->LastPerson;Person* cur=Base->FirstPerson;int count;while (Base->RemainingNum > 1){PrintCircle(Base);for (count = 1; count < StopStepCount; count++){pre = pre->Next;}cur = pre->Next;pre->Next = cur->Next;free(cur);Base->FirstPerson = pre->Next;Base->RemainingNum--;Base->LastPerson = Base->FirstPerson;for (count = 1; count < Base->RemainingNum; count++)Base->LastPerson = Base->LastPerson->Next;}PrintCircle(Base);printf("The Last Person to Out is %s\n", Base->FirstPerson->Name);return 1;
}
int main()
{FILE* InputNameText;fopen_s(&InputNameText,"C:\\Users\\dell\\Desktop\\Names.txt","r");Person* FirstOne;char name[] = "领导人";FirstOne = NewPerson(name);CircleSit* Base;Base = InitialCircle(FirstOne);int SeatsCount, StopStepCount;printf("Type in SeatsCount(<30): ");scanf("%d", &SeatsCount);if (SeatsCount > 30)SeatsCount = 30;FoundCircle(Base, SeatsCount, InputNameText);printf("Type in StopStepCount: ");scanf("%d",&StopStepCount);JosephExecute(Base, StopStepCount);return 0;
}

C数组标记法

#include<stdio.h>
const char* Names[34] = {
"刘植树","吴交通","张烫头","李隆胸","王帮助","吕做梦","余播放","柯暂停","肖升","韩降",
"赵漂移","孙快进","周添加","郑插入","冯图论","蒋点击","金开机","孔崩","鲁切换","方过滤",
"马当官","费洗澡","雷劈","唐数数","任爆炸","范减轻","曹充气","杨挂科","秦保研","贺加油",
"史欺骗","章摆放","何寻找","许导航"};
int main() {int PersonNumber, StopStepCount;printf("Type in PersonNumber(<=34): "); scanf_s("%d", &PersonNumber); if (PersonNumber > 34||PersonNumber<1)PersonNumber = 34;printf("Type in StopStepCount: "); scanf_s("%d", &StopStepCount); if (StopStepCount < 1)StopStepCount = 1;int move, remain=PersonNumber,index;int circle[34];for (index = 0; index < PersonNumber; index++)circle[index] = 1;index = 0;while (remain > 1){   move=1;index = index % PersonNumber;while (move <= StopStepCount){if (move != StopStepCount && circle[index % (PersonNumber)]) { move++; index++; }else if (move == StopStepCount && circle[index % (PersonNumber)]) { circle[index % (PersonNumber)] = 0; remain--; index++; break; }else index++;}}for (index = 0; index < PersonNumber; index++)if (circle[index])printf("%s\n",Names[index]);return 0;
}

C++list容器形成循环

#include"stdafx.h"
using namespace std;
int Joseph(list<string> base,const int StopStepCount)
{list<string>::iterator itr,end=base.end();itr = base.begin();int c;while (base.size() > 1){for (c = 1; c < StopStepCount; c++){advance(itr,1);if (itr == base.end())itr = base.begin();}itr =  base.erase(itr);if (itr == base.end())itr = base.begin();}itr = base.begin();cout <<*itr<< endl;return 1;
}
int main()
{list<string> NAMES{"刘植树","吴交通","张烫头","李隆胸","王帮助","吕做梦","余播放","柯暂停","肖升","韩降","赵漂移","孙快进","周添加","郑插入","冯图论","蒋点击","金开机","孔崩","鲁切换","方过滤","马当官","费洗澡","雷劈","唐数数","任爆炸","范减轻","曹充气","杨挂科","秦保研","贺加油","史欺骗","章摆放","何寻找","许导航" };int PersonCount, StopStepCount,c;cout << "Type in PersonCount: ";cin >> PersonCount;list<string>::iterator itr = NAMES.begin();list<string> Names;for (c = 1; c <= PersonCount; c++) { Names.push_back(*itr); advance(itr,1); }cout << "Type in StopStepCount: "; cin >> StopStepCount;Joseph(Names,StopStepCount);return 0;
}

WindowsGDI绘图离座过程

#include<iostream>
#include<list>
#include<string>
#include<math.h>
#define TWOPI (2*3.1416)
using namespace std;
TCHAR* Names[];
class Person {
public:Person(TCHAR* NameSrc);Person();bool GetState() { return OnSeat; }TCHAR* GetName() { return Name; }void LeaveSeat() { OnSeat = false; }
private:bool OnSeat;TCHAR* Name;
};
Person::Person(TCHAR* NameSrc)
{OnSeat = true;Name = NameSrc;
}
Person::Person()
{OnSeat = true;Name = (TCHAR*)"默认姓名";
}class Circle {
public:Circle(int iPersonNum);void ExecutJoseph(list<Person>::iterator &itr,const int &iStopStepPara,int &move,int &ArrowAngle,HWND &hwnd);list<Person>* GetCircle() { return &Persons; }
private:list<Person> Persons;int RemainingNum;};
Circle::Circle(int iPersonNum=0)
{int i;for (i = 0; i < iPersonNum; i++){Person temp(Names[i]);Persons.push_back(temp);}RemainingNum = iPersonNum;
}
void Circle::ExecutJoseph(list<Person>::iterator &itr,const int &iStopStepPara,int &move, int &ArrowAngle, HWND &hwnd)
{if(RemainingNum > 1){if (move <= iStopStepPara){if (move < iStopStepPara){if ((*itr).GetState())move++;advance(itr, 1);ArrowAngle++;if (itr == Persons.end())itr = Persons.begin();}else if(move == iStopStepPara){if ((*itr).GetState()) {(*itr).LeaveSeat();move = 1;RemainingNum--;}advance(itr, 1);ArrowAngle++;if (itr == Persons.end())itr = Persons.begin();}}}else {for (itr = Persons.begin(); itr != Persons.end(); itr++){if ((*itr).GetState()){MessageBox(hwnd, (*itr).GetName(), TEXT("最后一个人是"), MB_ICONINFORMATION);SendMessage(hwnd, WM_DESTROY, NULL, NULL);break;}}}return;
}
TCHAR* Names[] = {(TCHAR*)TEXT("刘植树"),
(TCHAR*)TEXT("吴交通"),(TCHAR*)TEXT("张烫头"),(TCHAR*)TEXT("李隆胸"),(TCHAR*)TEXT("王帮助"),(TCHAR*)TEXT("吕做梦"),(TCHAR*)TEXT("余播放"),(TCHAR*)TEXT("柯暂停"),(TCHAR*)TEXT("肖升"),(TCHAR*)TEXT("韩降"),
(TCHAR*)TEXT("赵漂移"),(TCHAR*)TEXT("孙快进"),(TCHAR*)TEXT("周添加"),(TCHAR*)TEXT("郑插入"),(TCHAR*)TEXT("冯图论"),(TCHAR*)TEXT("蒋点击"),(TCHAR*)TEXT("金开机"),(TCHAR*)TEXT("孔崩"),(TCHAR*)TEXT("鲁切换"),
(TCHAR*)TEXT("方过滤"),(TCHAR*)TEXT("马当官"),(TCHAR*)TEXT("费洗澡"),(TCHAR*)TEXT("雷劈"),(TCHAR*)TEXT("唐数数"),(TCHAR*)TEXT("任爆炸"),(TCHAR*)TEXT("范减轻"),(TCHAR*)TEXT("曹充气"),(TCHAR*)TEXT("杨挂科"),
(TCHAR*)TEXT("秦保研"),(TCHAR*)TEXT("贺加油"),(TCHAR*)TEXT("史欺骗"),(TCHAR*)TEXT("章摆放"),(TCHAR*)TEXT("何寻找"),(TCHAR*)TEXT("许导航") };LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{static TCHAR szAppName[] = TEXT("Joseph");HWND hwnd;MSG msg;WNDCLASS wndclass;wndclass.style = CS_HREDRAW | CS_VREDRAW;wndclass.lpfnWndProc = WndProc;wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);wndclass.lpszClassName = szAppName;wndclass.lpszMenuName = NULL;wndclass.hInstance = hInstance;wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);if (!RegisterClass(&wndclass)){MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);return 0;}hwnd = CreateWindow(szAppName,TEXT("JosephWindow"),WS_OVERLAPPEDWINDOW,220,50,800,600,NULL,NULL,hInstance,NULL);ShowWindow(hwnd, iCmdShow);UpdateWindow(hwnd);while (GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;
}LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{static int  cxClient, cyClient;HDC hdc;//硬件设备环境句柄int i;PAINTSTRUCT ps;TCHAR* szBuffer;//存字符串用HBRUSH GreenBrush = CreateSolidBrush(RGB(0, 255, 0));HBRUSH RedBrush = CreateSolidBrush(RGB(255, 0, 0));HBRUSH HoldBrush;HPEN Arrow = CreatePen(PS_SOLID, 13, BLACK_PEN);HPEN Eraser = CreatePen(PS_SOLID, 30, RGB(255,255,255));HPEN HoldPen;static int TableRadius=150;list<Person>::iterator itr;static list<Person>::iterator itr_move;Person cur;const int PersonAll = 10,iStopStepPara = 3;static Circle SizeFixedExample(PersonAll);static list<Person>* Persons;static int ArrowAngle,move=1;static int Time = 300;switch (message){case WM_CREATE:Persons = SizeFixedExample.GetCircle();itr_move = (*Persons).begin();case WM_SIZE:cxClient = LOWORD(lParam);cyClient = HIWORD(lParam);return 0;case WM_PAINT:Sleep(Time);hdc = BeginPaint(hwnd, &ps);SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL);for (itr = (*Persons).begin(), i = 0; itr != (*Persons).end(); advance(itr, 1), i++){cur = *itr;HoldPen = (HPEN)SelectObject(hdc, GetStockObject(BLACK_PEN));SetTextAlign(hdc, TA_CENTER | VTA_CENTER);if (cur.GetState()) {SetBkColor(hdc, RGB(0, 255, 0));HoldBrush = (HBRUSH)SelectObject(hdc, GreenBrush);}else {SetBkColor(hdc, RGB(255, 0, 0));HoldBrush = (HBRUSH)SelectObject(hdc, RedBrush);}Ellipse(hdc, (int)(sin(i*TWOPI / PersonAll)*TableRadius - 30), -(int)(cos(i*TWOPI / PersonAll)*TableRadius + 24), (int)(sin(i*TWOPI / PersonAll)*TableRadius + 30), -(int)(cos(i*TWOPI / PersonAll)*TableRadius - 36));TextOut(hdc, (int)(sin(i*TWOPI / PersonAll)*TableRadius), -(int)(cos(i*TWOPI / PersonAll)*TableRadius), cur.GetName(), lstrlen(cur.GetName()));}HoldPen = (HPEN)SelectObject(hdc, Eraser);MoveToEx(hdc, (int)(sin((ArrowAngle-1)*TWOPI / PersonAll)*(TableRadius + 50)), -(int)(cos((ArrowAngle - 1)*TWOPI / PersonAll)*(TableRadius + 50)), NULL);LineTo(hdc, (int)(sin((ArrowAngle -1)*TWOPI / PersonAll)*(TableRadius + 66)), -(int)(cos((ArrowAngle - 1)*TWOPI / PersonAll)*(TableRadius + 66)));if ((*itr_move).GetState()) {HoldPen = (HPEN)SelectObject(hdc, Arrow);MoveToEx(hdc, (int)(sin(ArrowAngle*TWOPI / PersonAll)*(TableRadius + 50)), -(int)(cos(ArrowAngle*TWOPI / PersonAll)*(TableRadius + 50)), NULL);LineTo(hdc, (int)(sin(ArrowAngle*TWOPI / PersonAll)*(TableRadius + 66)), -(int)(cos(ArrowAngle*TWOPI / PersonAll)*(TableRadius + 66)));Time = 300;}else Time = 0;HoldPen = (HPEN)SelectObject(hdc, GetStockObject(BLACK_PEN));HoldPen = (HPEN)SelectObject(hdc, GetStockObject(BLACK_PEN));SizeFixedExample.ExecutJoseph(itr_move,iStopStepPara,move,ArrowAngle,hwnd);InvalidateRect(hwnd, NULL, TRUE);EndPaint(hwnd, &ps);return 0;case WM_DESTROY:PostQuitMessage(0);DeleteObject(GreenBrush);DeleteObject(RedBrush);DeleteObject(Eraser);DeleteObject(Arrow);return 0;}return DefWindowProc(hwnd, message, wParam, lParam);
}

C C++ 分别编程赋不同姓名的约瑟夫环及用WindowsApi(C++及C)显示离座过程动画相关推荐

  1. C语言 (编程题:约瑟夫环 报数游戏)

    1.前言 最近有点emo,写博客也没有什么特别想写的内容 这篇博客把约瑟夫环用C语言来写一下 巩固一下原来学过的知识. 2.什么是约瑟夫环 这有个历史故事还是比较有意思的:据说著名犹太历史学家Jose ...

  2. 1.7 编程基础之字符串 30 字符环 python

    http://noi.openjudge.cn/ch0107/30/ """ 1.7 编程基础之字符串 30 字符环 http://noi.openjudge.cn/ch ...

  3. 用户输入自己的【姓名 年龄 爱好】 + 加CP【姓名 年龄 爱好】,点击按钮,页面显示该用户的信息 点击提交按钮,JSON显示用户信息+副本信息(副本初始值是{})

    本文主要是利用了BOM.DOM和json/js数据的转换等知识来综合操练的一个小例子 例子如下: 用户输入自己的[姓名 年龄 爱好] + 加CP[姓名 年龄 爱好],点击按钮,页面显示该用户的信息 点 ...

  4. 从键盘中输入姓名和电话号码,以#号结束,编程实现输入姓名可查询电话号码的功能。

  5. 6-4 多线程编程:使用多线程同步与通信,模拟实现售票系统的售票和退票过程。 (20 分)

    使用synchronized实现同步控制,并使用wait()和notify()实现线程之间的通信. 编程要求如下: 可售出票的数量为一个固定值(total),从键盘读入.当余票的数量足够出售时(余票数 ...

  6. ESP8266-Arduino编程实例-二维码(QR Code)生成与TFT LCD显示

    二维码(QR Code)生成与TFT LCD显示 QR 码(快速响应码的缩写)是一种矩阵条码(或二维条码,由日本汽车公司 Denso Wave 于 1994 年发明.条形码是一种机器可读的光学标签,可 ...

  7. android编程任务进度条,Android应用开发之AsyncTask 处理耗时操作和显示进度条

    在Android中实现异步任务机制有两种,Handler和AsyncTask.优缺点自己百度,推荐使用AsyncTask. private ProgressDialog dialog; //新建一个对 ...

  8. python动画精灵_【python游戏编程之旅】第六篇---pygame中的Sprite(精灵)模块和加载动画...

    本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 直到现在我们已经学了很多pygame基础知识了,从这篇博客开始我们将会学习pygame中高级部分,更多和精灵 ...

  9. 简单版的约瑟夫环问题编程:清除隔壁编号的

    根据查找的只是发现约瑟夫环是有规律的:清除隔壁编号的 人的总数:1--2--3--4--5--6--7--(对应下面的存活位置) 存活位置:1--1--3--1--3--5--7-- 一号清除号,三号 ...

最新文章

  1. leetcode每日一题系列——797. 所有可能的路径
  2. 网站搭建 (第09天) 博客统计排行
  3. 【Java 虚拟机原理】Class 字节码二进制文件分析 四 ( 字段表数据结构 | 字段表详细分析 | 访问标志 | 字段名称 | 字段描述符 | 属性项目 )
  4. 【Netty】 异步任务调度 ( TaskQueue | ScheduleTaskQueue | SocketChannel 管理 )
  5. Appcan页面跳转
  6. python如何去掉方括号_python从入门到实战外星人入侵
  7. Celery实现定时任务crontab
  8. 计算机考试老师批卷么,【揭秘】高考答卷扫描到电脑上是啥模样?阅卷老师喜欢啥样的答卷?...
  9. 评分卡模型开发(一)--用户数据缺失值处理
  10. oracle varchar默认长度_Mysql Online DDL之VARCHAR字段扩容探索
  11. Parse error: syntax error, unexpected '__data' (T_STRING), expecting ',' or ')'
  12. 中台,都被你们说糊涂了
  13. linux6.5输错密码锁定,干货分享:解决redhat6.5登陆后跳回Login,循环登陆问题
  14. 1.Modbus通信协议-什么是Modbus?
  15. 三星A5显示服务器未响应,三星A5手机死机了 屏幕一直亮着 按什么键都没反应==求解答...
  16. 最新最全的免费股票数据接口--沪深A股深度分析财务分析数据API接口(十一)
  17. 镜子中的 Google
  18. 最长递增子序列O(NlogN)算法
  19. php数组合并多维,PHP合并多维数组
  20. CNN与句子分类之动态池化方法DCNN--模型介绍篇

热门文章

  1. 新的数字革命真的有意义么?
  2. 图形学基础|基于LTC的面光源渲染
  3. linux下链接so提示skipping incompatible ../bin/xxx.so when searching for -lxxx
  4. spring-boot-maven-plugin 报错,找不到
  5. 北京市2021年高考适应性测试成绩查询,教育局2021适应性考试成绩查询
  6. 基于Tensorflow2.1的Mask RCNN兼容性问题(Variables were created within a Lambda layer (anchors))
  7. 单招计算机专业考多少分可以录取,单招一般多少分能过 要考多少分录取
  8. linux内核协议栈 IPv4之发送接口 ip_queue_xmi()【TCP使用】
  9. dev hda1在linux中表示,LINUX-hda1表示意思
  10. 性能鲁棒性、稳定性鲁棒性、鲁棒性、鲁棒性能、鲁棒稳定性、性能、稳定性的区别