1 条题解

  • 0
    @ 2025-4-12 21:52:29

    C :

    #include<stdio.h>
    #include<stdlib.h>
    typedef struct Node
    {
    	int data;
    	struct Node *next;
    }Node;
    int main()
    {
    	int m,n;
    	scanf("%d",&n);
    	for(int l=1;l<=n;l++)
    	{
    		scanf("%d",&m);
    		int k=1;
    		Node *c,*s,*r,*p;
    		c=(Node *)malloc(sizeof(Node));
    		c->next=NULL;
    		r=c;
    		p=c;
    		for(int j=1;j<=m;j++)
    		{
    			s=(Node *)malloc(sizeof(Node));
    			s->data=k++;
    			r->next=s;
    			r=r->next;
    		}
    		r->next=p->next;
    		p=p->next;
    		int count=1;
    		while(p->next->data!=p->data)
    		{
    			if(count%3==0)
    			{
    				printf("%d ",p->data);
    				p->data=p->next->data;
    				p->next=p->next->next;
    				count++;
    				continue;
    			}
    			p=p->next;
    			count++;
    		}
    		printf("%d\n",p->data);
    		/*for(int i=0;i<=m+10;i++)
    		{
    			printf("%d ",p->data);
    			p=p->next;
    			printf("\n");
    		}*/
    	}
    	//system("pause");
    	return 0;
    }
    

    C++ :

    #include <iostream>
    #include <malloc.h>
    #include <cstdio>
    using namespace std;
    
    typedef struct node
    {
        int num;
        struct node *next;
    } LNode;
    
    int main()
    {
        //freopen("test.in", "r", stdin);
        //freopen("test.out", "w", stdout);
        int t;
        cin >> t;
        while (t--)
        {
            LNode *head,*p,*q;
            int N;
            cin>>N;
            p=(LNode*)(malloc(sizeof(LNode)));
            p->num=1;
            head=p;
            for(int i=1; i<N; i++)
            {
                p->next=(LNode*)(malloc(sizeof(LNode)));
                p=p->next;
                p->num=i+1;
            }
            p->next=head;
            p=head;
            while(p->next!=p)
            {
                q=p->next;
                p=q->next;
                q->next=p->next;
                cout<<p->num<<" ";
                delete p;
                p=q->next;
            }
            cout<<p->num<<endl;
            delete p;
    
        }
        return 0;
    }
    
    

    Pascal :

    var
    a:array[1..50] of boolean;
    m,q,n,i,s,h:longint;
    begin
      readln(m);
      for q:=1 to m do
      begin
        if q>=2 then writeln;
        read(n);
        for i:=1 to n do a[i]:=true;
        s:=n; h:=0; i:=0;
        repeat
          i:=i+1;
          if i>n then i:=1;
          if a[i]=true then h:=h+1;
          if h=3 then
          begin
            write(i,' ');
            h:=0;
            dec(s);
            a[i]:=false;
          end;
        until s=1;
        for i:=1 to n do if a[i] then write(i);
      end;
    end.
    
    

    Java :

    public class Main {
                public static void main(String[] args) { 
                  java.util.Scanner in= new java.util.Scanner(System.in) ;
                while(in.hasNextInt()){
                int a = in.nextInt();
                for(int i=0;i<a;i++) {
                int b = in.nextInt();
                int[] number =new int[b];
                int m=1;
                for(int j=0;j<b;j++){
                    number[j]=m;
                    m++;
                }
                int c=0,count=0;
                while(c!=b){
                for(int j=0;j<b;j++){
                    if(number[j]!=0) count++;
                    if(count==3) {
                        System.out.print(j+1);c++;
                        if(c<b) System.out.print(" ");
                        number[j]=0;count=0;
                }
            } 
            }System.out.println();
        }
    }
    }
    }
    
    • 1

    信息

    ID
    2082
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者