> 编号为1,2,…,n的n个人按顺时针方向围坐在一张圆桌周围,每人持有一个密码(正整数)。
开始任选一个正整数m作为报数上限值,从第一个人开始按顺时针方向自1开始报数,报到m时停止报数,报m的那
个人出列,将他的密码作为新的m值,从他顺时针方向的下一个人开始重新从1报数,数到m的那个人又出列;如
此下去,直至圆桌周围的人全部出列为止。要求按出列顺序输出n个人的编号。
#include
#include
int a;
typedef struct Student{
int index;
int data;
struct Student *next;
}Elemsn;
Elemsn *last = NULL;
Elemsn * CreatLink(Elemsn *head,int a){
Elemsn *p,*t;
int i;
for(i = 0;i<a;i++){
p = (Elemsn *)malloc(sizeof(Elemsn));
scanf("%d",& p->data);
p->index = i+1;
p->next = NULL;
if(head == NULL){
head = t = p;
}
else{
t = t->next = p;
}
}
last = p;
//p->next = head;
return head;
}
Elemsn* removeN(Elemsn *head){
// a--;
Elemsn *temp;
temp = head;
last = last->next = temp;
//temp = head;
head = head->next;
temp->next = NULL;
//last = temp;
// last->next = head;
return head;
}
void changeN(Elemsn *head,int a,int b){
int d =1;
//int flag = 1;
while( a!=1){
if(d%b !=0){
head = removeN(head);
d++;
}else{
b = head->data;
printf("%d ",head->index);
a--;
head = head->next;
d=1;
}
}
printf("%d",head->index);
}
int main(void){
Elemsn *head = NULL;
int b;
scanf("%d %d",&a,&b);
head = CreatLink(head,a);
changeN(head,a,b);
}
Elemsn * AscendingOrder(Elemsn *h){
Elemsn *hn=NULL,*pim,*p,*t,*q;
for(pim=h;pim;){
p=h;
while(p){
if((p->emop)<(pim->emop)){
pim=p;
}
p=p->next;
} //找到整条链中最小的结点
if(pim ==h){
h=h->next;
}
else{
q=h;
while((q->next)!=pim) {
q=q->next; //q为pim的前驱
}
q->next=pim->next; //分离最小pim
}
pim->next=NULL;
if(hn==NULL){
hn=t=pim; //新搭一条链,没有新创建结点,只是链的指向发生了改变
}
else{
t=t->next=pim; //尾插新链
}
pim=h;
}
return hn; //返回新链头结点
}
> 假定运算符集合为{ +、-、*、/、(、)},利用栈将输入的中缀表达式转换成等价的后缀表达式,并输出。
输入
输入一个字符串表示的中缀表达式,其中每个操作数用一个小写字母表示。
输出
将对应的后缀表达式输出。
lude<stdio.h>
#include
char str[20];
int top = -1;
int priority(char c){
if( c == '/' || c == '*'){
return 1;
}
if( c == '+' || c == '-'){
return 0;
}
if(c == '('){
return -1;
}
}
void Push(char c){
int d,flag = 1;
if(top == -1){
top++;
str[top] = c;
}
else{
if(priority(c) > priority(str[top])){
top++;
str[top] = c;
}
else{
while(priority(c) <= priority(str[top])){
// if(str[top]!='('){
// d = top;
// while(d!=-1){
// if(str[d]=='(')
// {
// flag = 0;
// }
// d--;
// }
// if(flag == 0){
// break;
// }
// else
// printf("%c",str[top]);
// }
// top--;
// if(top == -1){
// break;
// }
char tmp = str[top];
if(tmp == '('){
break ;
}
top--;
printf("%c",tmp);
if(top == -1) {
break;
}
}
top ++;
str[top] = c;
}
}
}
int main(void){
char s[40];
gets(s);
int i;
for(i = 0;s[i];i++){
if('a' <= s[i] &&s[i]<='z' ){
printf("%c",s[i]);
}
else{
if(s[i] == '(' || s[i] == ')'){
if(s[i] == '('){
top++;
str[top] = '(';
}
else{
while(str[top] != '('){
printf("%c",str[top]);
top--;
}
top--;
}
}
else{
Push(s[i]);
}
}
}
while(!(top == -1) &&str[top]!='('){
printf("%c",str[top]);
top--;
}
}
package SHUZU.String;
import java.util.Scanner;
public class maseA {
/**
* 定义迷宫数组
*/private int[][] array = {
{0, 0, 1, 0, 0, 0, 1, 0},
{0, 0, 1, 0, 0, 0, 1, 0},
{0, 0, 1, 0, 1, 1, 0, 1},
{0, 1, 1, 1, 0, 0, 1, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 1},
{0, 1, 1, 1, 1, 0, 0, 1},
{1, 1, 0, 0, 0, 1, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 0}
};
private int maxLine = 8;
private int maxRow = 9;
// Scanner input = new Scanner(System.in);
//
// int a = input.nextInt();
// int b = input.nextInt();
// int[][]array = new int[a][b];
// int maxLine = b;
// int maxRow = a;
public static void main(String[] args) {
// System.out.println(System.currentTimeMillis());
new maseA().check(0,0);
//System.out.println(System.currentTimeMillis());
}
// public void For(){
// for(int i= 0;i
// for(int j = 0;j
// array[i][j] = input.nextInt();
// }
// }
// new maseA().check(0, 0);
// }
private void check(int i, int j) {
//如果到达右下角出口
if (i == maxRow - 1 && j == maxLine - 1) {
print();
//return;
}
//向右走
if (canMove(i, j, i, j + 1)) {
array[i][j] = 5;
check(i, j + 1);
array[i][j] = 0;
}
//向下走
if (canMove(i, j, i + 1, j)) {
array[i][j] = 5;
check(i + 1, j);
array[i][j] = 0;
}
//向左走
if (canMove(i, j, i, j - 1)) {
array[i][j] = 5;
check(i, j - 1);
array[i][j] = 0;
}
//向上走
if (canMove(i, j, i - 1, j)) {
array[i][j] = 5;
check(i - 1, j);
array[i][j] = 0;
}
}
private boolean canMove(int i, int j, int targetI, int targetJ) {
// System.out.println("从第" + (i + 1) + "行第" + (j + 1) + "列,走到第" + (targetI + 1) + "行第" + (targetJ + 1) + "列");
if (targetI < 0 || targetJ < 0 || targetI >= maxRow || targetJ >= maxLine) {
// System.out.println("到达最左边或最右边,失败了");
return false;
}
if (array[targetI][targetJ] == 1) {
// System.out.println("目标是墙,失败了");
return false;
}
//避免在两个空格间来回走
if (array[targetI][targetJ] == 5) {
// System.out.println("来回走,失败了");
return false;
}
return true;
}
private void print() {
System.out.println("得到一个解:");
for (int i = 0; i < maxRow; i++) {
for (int j = 0; j < maxLine; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}