QQ4. 游戏任务标记
描述
游戏里面有很多各式各样的任务,其中有一种任务玩家只能做一次,这类任务一共有1024个,任务ID范围[1,1024]。请用32个unsigned int类型来记录着1024个任务是否已经完成。初始状态都是未完成。 输入两个参数,都是任务ID,需要设置第一个ID的任务为已经完成;并检查第二个ID的任务是否已经完成。 输出一个参数,如果第二个ID的任务已经完成输出1,如果未完成输出0。如果第一或第二个ID不在[1,1024]范围,则输出-1。输入描述
输入包括一行,两个整数表示任务ID.输出描述
输出是否完成示例1
输入:
1024 1024
输出:
1
C++ 解法, 执行用时: 1ms, 内存消耗: 220KB, 提交时间: 2017-07-29
#include<cstdio> int main() { unsigned int a,b; scanf("%d%d",&a,&b); if(a<0||a>1024||b<0||b>1024) printf("-1\n"); else { if(a==b) printf("1\n"); else printf("0\n"); } return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 220KB, 提交时间: 2017-07-19
// // main.cpp // 2 // // Created by 胡诚真 on 2017/7/19. // Copyright © 2017年 胡诚真. All rights reserved. // #include <iostream> #include <memory.h> typedef struct { unsigned int array[32]; } game; void findGamePlayed(game &playGame, int gameToFind){ if (1 <= gameToFind && gameToFind <= 1024) { int index = gameToFind / 32 - 1; if (playGame.array[index] == 1 << (gameToFind % 32)) { printf("1"); } else { printf("0"); }; } else { printf("-1"); } } void setGamePlayed(game &playgame, int gameplayed){ if (1 <= gameplayed && gameplayed <= 1024) { int index = gameplayed / 32 - 1; playgame.array[index] = 1 << (gameplayed % 32); int gameToFind; scanf("%d", &gameToFind); findGamePlayed(playgame, gameToFind); } else { printf("-1"); } } int main(int argc, const char * argv[]) { game playgame; memset(&playgame, sizeof(unsigned int), 32); int gameplayed; int gameToFind; scanf("%d", &gameplayed); setGamePlayed(playgame, gameplayed); return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 220KB, 提交时间: 2017-07-15
#include <iostream> #include <vector> #include <numeric> #include<stdio.h> #include<assert.h> using namespace std; static int panbie[32]={ -1 }; int Isdo(int id1, int id2) { if(id1<1||id1>1024||id2<1||id2>1024) { return -1; } panbie[(id1-1)/32]= (id1-1)%32 +1 ; if(panbie[(id2-1)/32]==(id2-1)%32+1 ) { return 1; } else { return 0; } } int main() { int index1,index2; int result; cin>>index1>>index2; result=Isdo(index1,index2); cout<<result; }
C++ 解法, 执行用时: 1ms, 内存消耗: 220KB, 提交时间: 2017-07-15
// Copyright (c) 2017 panda. All rights reserved. #include <iostream> #include <vector> using namespace std; void set_task(vector<unsigned> &t ,unsigned id) { size_t i = id / 32; size_t j = id % 32; t[i] |= 1 << (j-1); } int look_task(const vector<unsigned> t, unsigned id) { size_t i = id / 32; size_t j = id % 32; return !(t[i] ^ (t[i]|(1<<(j-1)))); } int main() { unsigned id1, id2; vector<unsigned> tasks(32, 0); cin >> id1 >> id2; int result; if (id1 < 1 || id1 > 1024 || id2 < 1 || id2 > 1024) result = -1; else { id1 -= 1; id2 -= 1; set_task(tasks, id1); result = look_task(tasks, id2); } cout << result << endl; return 0; }
C++ 解法, 执行用时: 1ms, 内存消耗: 220KB, 提交时间: 2017-07-12
#include<iostream> using namespace std; unsigned int arr[32]; int main() { int m, n; while(cin>>m>>n) { if(n>1024 || n<1) { cout<<-1<<endl; continue; } int x1 = (m-1)/32; int x2 = (n-1)/32; arr[x1] |= (1<<((m-1)%32)); cout<<( (arr[x2]&(1<<((n-1)%32))) != 0 )<<endl; } return 0; }