日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

玩具谜题

發布時間:2024/10/5 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 玩具谜题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

https://www.luogu.org/problemnew/show/P1563

題解:題目不難,主要第一次遇到異或,就想寫一下

C++版本一

/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUG #define RI register int using namespace std; typedef long long ll; typedef __int128 lll; const int N=100000+1000; const int MOD=1e9+7; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; int t,n,m,k,q; map< int,string>s; bool a[N]; int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endifchar str[20];scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%d %s",&k,str);a[i]=k;s[i]=str;}int pos=1;while(m--){scanf("%d %d",&k,&q);if(a[pos]^k){pos+=q;}else{pos-=q;}if(pos<=0){pos+=n;}if(pos>n){pos-=n;}}cout << s[pos] << endl;//cout << "Hello world!" << endl;return 0; }

C++版本二

include<bits/stdc++.h> using namespace std; struct node {int head;string name; }a[100005]; int n,m,x,y; int main() {cin>>n>>m;for(int i=0;i<n;i++){cin>>a[i].head>>a[i].name;}int now=0;for(int i=1;i<=m;i++){cin>>x>>y;if(a[now].head==0&&x==0)now=(now+n-y)%n;else if(a[now].head==0&&x==1)now=(now+y)%n;else if(a[now].head==1&&x==0)now=(now+y)%n;else if(a[now].head==1&&x==1)now=(now+n-y)%n;}cout<<a[now].name<<endl;return 0; }

?

JAVA版本一

import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader;public class Main {static int place, n, m;// place是當前位置,n是人物個數,m是指令個數static int[] N;// 存儲人物朝向的數組static String[] Name;// 存儲人物名稱的數組public static void main(String[] args) {InputReader in = new InputReader(System.in);n = in.nextInt();m = in.nextInt();N = new int[n];Name = new String[n];for (int i = 0; i < n; i++) {// 讀取所有的人物N[i] = in.nextInt();Name[i] = in.next();}for (int i = 0; i < m; i++) {// 讀取所有操作同時計算opeart(in.nextInt(), in.nextInt());}System.out.println(Name[place]);}static void opeart(int i, int j) {// 其實只有兩種情況,// 1 如果朝向內側又向左移動表示為 0 0,如果朝向外側有向右移動表示為1 1,這種情況下應該用當前位置減去移動的距離// 2朝向和移動方向不想同0 1或者1 0,則應該用當前位置加上移動的距離if (N[place] == i)place = (place + n - j) % n;elseplace = (place + j) % n;}// 這是個內部類,借鑒cf上某個java大佬的寫法,只是為了解決讀取輸入太慢的問題static class InputReader {public BufferedReader reader;public StringTokenizer tokenizer;public InputReader(InputStream stream) {reader = new BufferedReader(new InputStreamReader(stream), 32768);tokenizer = null;}public String next() {while (tokenizer == null || !tokenizer.hasMoreTokens()) {try {tokenizer = new StringTokenizer(reader.readLine());} catch (IOException e) {throw new RuntimeException(e);}}return tokenizer.nextToken();}public int nextInt() {return Integer.parseInt(next());}} }

?

總結

以上是生活随笔為你收集整理的玩具谜题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。