玩具谜题
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());}} }?
總結
- 上一篇: C++文件输入输出
- 下一篇: 选择排序(Select_Sort)