A: 漫無止境的八月
Description
又雙叒叕開始漫無止境的八月了,阿虛突然問起長門在這些循環中團長哪幾次扎起了馬尾,他有多少次抓住了蟬等等問題,長門一共回復n個自然數,每個數均不超過1500000000(1.5*10^9)。已知不相同的數不超過10000個,現在需要統計這些自然數各自出現的次數,並按照自然數從小到大的順序輸出統計結果。
Input
第1行是整數n,表示回復的自然數的個數。n<=1e6
第2~n+1行每行一個自然數。
Output
包含m行(m為n個自然數中不相同數的個數),按照自然數從小到大的順序輸出。每行輸出兩個整數,分別是自然數和該數出現的次數,其間用一個空格隔開。
Sample Input
8
2
4
2
4
5
100
2
100
Sample Output
2 3 4 2 5 1 100 2
題意:給你n個數,你要找到輸入的每個數出現了多少次,並從小到大的輸出該個數對應的值和出現的次數。
解法:這題我們可以利用map來存儲,我們知道map的key,value在輸入進去在之后map會自動的以key值進行排序,所以我們利用這個性質,key存放對應的數值,value存放對應的次數,然后輸出map就可以了。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map>
using namespace std; int n; map<int,int>m; int main() { scanf("%d",&n); m.clear(); int x; while(n--) { scanf("%d",&x); if(m.count(x)==0) m[x]=1; else m[x]++; } map<int,int>::iterator iter; for(iter=m.begin();iter!=m.end();iter++) printf("%d %d\n",iter->first,iter->second); return 0; } /********************************************************************** Problem: 2204 User: jk1601zr Language: C++ Result: AC Time:148 ms Memory:2424 kb **********************************************************************/
B: Magia
Description
吼姆啦醬來救被變成魔女的紗耶香攻擊的小圓辣!魔境背景中的指揮家指揮着這場表演的音樂,吼姆啦醬發現一個規律,如果過一串音符是回文的,那么這串音符會實物化來攻擊她,現在給出一段音符,判斷它是否是回文的。一個左右對稱的自然數稱為回文數,即這個數從左往右讀與從右往左讀是一樣的,如121,686,13731,8668等都是回文數。
Input
輸入一個int范圍內的自然數N,判斷它是否是回文數。如果是就輸出這個回文數,若不是則輸出-1。
Output
只有一行,N是回文數,就輸出N,不是就輸出-1。
Sample Input
686
Sample Output
686
題意:判斷回文。
解法:直接存在字符串數組里面,感覺會方便比較吧,看個人習慣。然后區分奇數偶數對應的判斷就行了。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map>
using namespace std; const int maxn=1000; char s[maxn]; int main() { scanf("%s",s); int len=strlen(s); bool flag=false; if(len%2==1) { int temp=len/2; for(int i=0;i<temp;i++) { if(s[i]!=s[len-i-1]) { flag=true; break; } } if(flag) printf("-1\n"); else { for(int i=0;i<len;i++) printf("%c",s[i]); printf("\n"); } } else { int temp=len/2; for(int i=0;i<=temp;i++) { if(s[i]!=s[len-i-1]) { flag=true; break; } } if(flag) printf("-1\n"); else { for(int i=0;i<len;i++) printf("%c",s[i]); printf("\n"); } } return 0; } /********************************************************************** Problem: 2205 User: jk1601zr Language: C++ Result: AC Time:4 ms Memory:2024 kb **********************************************************************/
C: 長門的運動會
Description
運動會,好開心~
CSU (California State University) 正在舉行一場特殊的接力跑比賽,比賽在環形跑道上進行,同一支隊伍的所有人從同一個位置向相同的方向出發,當需要接力的兩個人再次相遇時,他們就要交接棒。最后總成績是以隊伍跑的總路程計算的。
現在接力的第一棒在Nagato手中,需要把它交給Kyon。在長度為C的環形跑道上,他們出發了!Nagato以速度A勻速跑動,Kyon以速度B勻速跑動。他們在經過多長時間后可以再次相遇?
Input
多組數據,第一行為一個整數T (1 ≤ T ≤ 106),表示數據組數。
之后每行是一組數據,有三個整數C, A, B (1 ≤ C, A, B ≤ 109, A ≠ B),分別表示環形跑道的長度,Nagato的速度和Kyon的速度。
Output
每行輸出一個數,表示再次相遇所需的時間。絕對誤差或相對誤差小於10−5則認為是正確的。
Sample Input
2
3 1 2
5 10 7
Sample Output
3.00000000 1.66666667
題意:兩個人從同一起點一起跑,看誰先比另一個人多跑一圈。
解法:求出兩個人速度的差值,然后簡單的物理計算。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map>
using namespace std; const int maxn=1000; int T; int c,a,b; int main() { scanf("%d",&T); while(T--) { scanf("%d %d %d",&c,&a,&b); int temp=abs(a-b); double ans=c*1.0/(temp); printf("%.8f\n",ans); } return 0; } /********************************************************************** Problem: 2158 User: jk1601zr Language: C++ Result: AC Time:316 ms Memory:2024 kb **********************************************************************/
D: 剪刀石頭布
Description
現在一共有N個人(分別記為1, 2, …, N)在玩剪刀石頭布,如果知道他們每個人都出了什么,你能找出來誰是winner嗎?
當且僅當一個人可以贏其他所有人時,才稱這個人是winner。
我們將剪刀記作2,石頭記作0,布記作5,那么勝負關系就應當是2能贏5,5能贏0,0能贏2。
Input
輸入數據的第一行包含一個整數T ( 1 <= T <= 150),表示接下來一共有T組測試數據。
每組測試數據的第一行包含一個整數N (2 <= N <= 100000)表示一共有N個人在玩剪刀石頭布,接下來一行一共有N個數,每個數均為0、2或5中的某一個,依次描述了這N個人分別出了什么,其中第i個整數描述了第i個人出了什么。
Output
對於每組數據,用一行輸出一個整數表示winner是第幾個人([1, N]中的某個整數)。
如果不存在winner,則用一行輸出“No winner”(不包括引號)。
Sample Input
3
3
5 5 2
3
2 0 0
3
0 2 5
Sample Output
3 No winner No winner
題意:模擬剪刀石頭布的游戲,我們知道三個模式當且僅當出現兩種,並且一個能打敗另一個的時候才會有winner。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map> #include <set>
using namespace std; const int maxn=100005; int T; int n,x; //struct Node //{ // int val; // int cnt; //};
int a[3],pos[3]; int main() { scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0;i<3;i++) a[i]=pos[i]=0; for(int i=0;i<n;i++) { scanf("%d",&x); if(x==2) { a[0]++;pos[0]=i+1; } else if(x==0) { a[1]++;pos[1]=i+1; } else if(x==5) { a[2]++;pos[2]=i+1; } } // if(a[0]!=0&&a[1]!=0&&a[2]!=0) // printf("No winner\n");
if(a[0]==1&&a[1]==0) printf("%d\n",pos[0]); else if(a[1]==1&&a[2]==0) printf("%d\n",pos[1]); else if(a[2]==1&&a[0]==0) printf("%d\n",pos[2]); else printf("No winner\n"); } return 0; } /********************************************************************** Problem: 1202 User: jk1601zr Language: C++ Result: AC Time:0 ms Memory:0 kb **********************************************************************/
E: 全場最水題之陳興老師與比賽
Description
大家都知道ACM比賽罰時很重要。比如說你做A題要10分鍾,B題要15分鍾,如果先做A題再做B題,那么在ranking上的時間就是10 + (10)+ 15 = 35。如果先做B題再做A題總罰時就是15+(15)+
10=40.現在陳興老師要做一場比賽,比賽有n道題, 總時間是300分鍾。我們的陳興老師僅僅看題目就可以知道他做每道題需要的時間。比如一般的比賽,陳興老師做第一題需要1分鍾,第二題2分鍾,依此類推,陳興老師只需要66分鍾就可以AK一場11道題的比賽。PS: 陳興老師做題都是1Y,膜拜陳興老師Orz!
Input
第一行是一個數字n 0<n<=25
第二行是n個數字,第i個數字代表陳興老師出編號為i的題所需要的時間 ti( 0 < ti <= 80)。
Output
第一行輸出陳興老師的出題數和Penalty(總時間)
以下按照順序輸出陳興老師出題的順序,每行一個編號。(詳見輸出樣例)PS:時間一樣的按編號升序輸出。
Sample Input
3
1 2 3
4
1 2 3 4
6
60 60 60 60 60 60
Sample Output
3 10
1
2
3
4 20
1
2
3
4
5 900
1
2
3
4
5
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map>
using namespace std; struct Node { int t; int num; }node[30]; int cmp(Node a,Node b) { if(a.t!=b.t) return a.t<b.t; else
return a.num<b.num; } int main() { int n,i,j,sum,k; while(cin>>n) { for(i=0;i<n;i++) { cin>>node[i].t; node[i].num=i+1; } sort(node,node+n,cmp); sum=0;k=0; for(i=0;i<n;i++) { if(k+node[i].t>300) break; k+=node[i].t; } int temp1=i,temp2=i; for(i=0;i<temp1;i++) { sum+=node[i].t*(temp2--); } cout<<temp1<<" "<<sum<<endl; for(i=0;i<temp1;i++) { cout<<node[i].num<<endl; } } return 0; } /********************************************************************** Problem: 1315 User: jk1601zr Language: C++ Result: AC Time:320 ms Memory:2024 kb **********************************************************************/
F: 字符畫
Description
讀入 w,請輸出 2018
的字符畫,兩個數字之間有 w 個空格。具體格式請參考樣例輸出。
- 1 ≤ w ≤ 2018
Input
輸入文件只包含 1 個整數 w.
Output
輸出 5 行,每行 12 + 3w 個字符(只包含 o
和 .
兩種,字符畫的部分用 o
,空格的部分用 .
),以換行符結尾。
Sample Input
2
Sample Output
ooo..ooo..ooo..ooo ..o..o.o...o...o.o ooo..o.o...o...ooo o....o.o...o...o.o ooo..ooo..ooo..ooo
直接模擬就行。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map>
using namespace std; int n; int main() { scanf("%d",&n); printf("ooo"); for(int i=0;i<n;i++) printf("."); printf("ooo"); for(int i=0;i<n;i++) printf("."); printf("ooo"); for(int i=0;i<n;i++) printf("."); printf("ooo"); printf("\n"); printf("..o"); for(int i=0;i<n;i++) printf("."); printf("o.o"); for(int i=0;i<n;i++) printf("."); printf(".o."); for(int i=0;i<n;i++) printf("."); printf("o.o"); printf("\n"); printf("ooo"); for(int i=0;i<n;i++) printf("."); printf("o.o"); for(int i=0;i<n;i++) printf("."); printf(".o."); for(int i=0;i<n;i++) printf("."); printf("ooo"); printf("\n"); printf("o.."); for(int i=0;i<n;i++) printf("."); printf("o.o"); for(int i=0;i<n;i++) printf("."); printf(".o."); for(int i=0;i<n;i++) printf("."); printf("o.o"); printf("\n"); printf("ooo"); for(int i=0;i<n;i++) printf("."); printf("ooo"); for(int i=0;i<n;i++) printf("."); printf("ooo"); for(int i=0;i<n;i++) printf("."); printf("ooo"); printf("\n"); return 0; } /********************************************************************** Problem: 2163 User: jk1601zr Language: C++ Result: AC Time:8 ms Memory:2024 kb **********************************************************************/
H: joghs
Description
編號為1、 2、 3、 …、 n的n個人按順時針方向圍坐一圈,每人持有一個密碼(正整數)。從指定編號為1的人開始,按順時針方向自1開始報數,報到指定值m時停止報數,報第m的人出列,並將他的密碼作為新的m值,從他在順時針方向的下一個人開始,重新從1開始報數,如此類推,直至所有的人全部出列為止。輸入n(n<=1000),m(m<=30000)及密碼值(<=10000),試設計一個程序求出列順序。
Input
有二行,第一行,N和M,第二行,N個小於等於10000的密碼值,中間用空格隔開。
Output
只有一行,就是出列的順序,編號間以空格隔開。
Sample Input
6 7
1 4 2 8 5 7
Sample Output
1 2 6 3 5 4
題意:約瑟夫環問題,但是要注意m值是在變化的,開始看題沒注意。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map> #include <vector> #include <list>
using namespace std; const int maxn=1005; int n,m; int a[maxn]; int main() { scanf("%d %d",&n,&m); list<int>l; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); l.push_back(i); } list<int>::iterator it; int cnt=1; for(it=l.begin();l.size()!=1;) { if(cnt++==m) { printf("%d ",*it); m=a[*it]; it=l.erase(it); cnt=1; } else it++; if(it==l.end()) it=l.begin(); } printf("%d\n",*l.begin()); return 0; } /********************************************************************** Problem: 2053 User: jk1601zr Language: C++ Result: AC Time:24 ms Memory:2028 kb **********************************************************************/
G: N個數字求和
這題數據有鍋,待補。
I: 英文單詞
Description
編寫程序,讀入一行文本,文本是一個長度不超過255的英文句子,單詞之間有一個或一個以上的空格,輸出:
①統計單詞的個數;
②一個對應的英文句子,其中原句中的所有小寫字母均轉換成大寫字母,大寫字母轉換成小寫字母;
③刪除所有空格符后對應的句子。
Input
只有一行,就是一個英文句子
Output
有三行,第一行單詞的個數,第二行,轉換了大小寫的英文句子,第三行刪除空格的句子。
Sample Input
Who are you?
Sample Output
3 wHO ARE YOU? Whoareyou?
題意:直接模擬就行,但是要注意這個字符串的讀入,要用到getline,不然空格讀入不了。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map>
using namespace std; const int maxn=300; char s[maxn]; int main() { cin.getline(s,256); int len=strlen(s); // cout<<len<<endl;
int cnt=0; for(int i=0;i<len;i++) { if(s[i]==' '&&((s[i+1]>='a'&&s[i+1]<='z')||(s[i+1]>='A'&&s[i+1]<='Z'))) cnt++; } if(s[0]!=' ') cnt++; printf("%d\n",cnt); for(int i=0;i<len;i++) { if(s[i]>='a'&&s[i]<='z') printf("%c",s[i]-32); else if(s[i]>='A'&&s[i]<='Z') printf("%c",s[i]+32); else printf("%c",s[i]); } printf("\n"); for(int i=0;i<len;i++) { if(s[i]==' ') continue; else printf("%c",s[i]); } printf("\n"); return 0; } /********************************************************************** Problem: 2050 User: jk1601zr Language: C++ Result: AC Time:4 ms Memory:2024 kb **********************************************************************/
J: Num
Description
編一程序,輸入正整數N(N在2~32767之間), 求它的最大質因子(包括它本身)。
Input
只有一行,就是正整數N
Output
所求的最大質因子
Sample Input
7
Sample Output
7
題意:題面就很清楚了。
解法:預處理出2~32767所有的質數,然后從大到小對應的判斷就行了。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map> #include <set>
using namespace std; const int maxn=323767; set<int>s; int check[maxn]; int n; void init() { memset(check,0,sizeof(check)); for(int i=2;i<=maxn;i++) { if(!check[i]) s.insert(i); for(int j=i+i;j<=maxn;j+=i) check[j]=1; } } int main() { init(); scanf("%d",&n); for(int i=n;;i--) { if(s.count(i)&&n%i==0) { printf("%d\n",i); break; } } return 0; } /********************************************************************** Problem: 2051 User: jk1601zr Language: C++ Result: AC Time:32 ms Memory:4608 kb **********************************************************************/
K: 時間旅行
Description
假設 Bobo 位於時間軸(數軸)上 t0 點,他要使用時間機器回到區間 (0, h] 中。
當 Bobo 位於時間軸上 t 點,同時時間機器有 c 單位燃料時,他可以選擇一個滿足 ⌈xh⌉⋅h≤c⌈xh⌉⋅h≤c 的非負整數 x, 那么時間機器會在 [0, x]中隨機整數 y,使 Bobo 回到 (t − y) 點,同時消耗 y 單位燃料。 (其中 ⌈ ⋅ ⌉ 表示上取整)
因為時間機器的隨機性,對於給出的參數 h 和時間機器剩余燃料 c,Bobo 想知道能夠保證回到區間 (0, h] 中的 t0 的最大值。
- 1 ≤ h ≤ 109
- 0 ≤ c ≤ 109
- 數據組數不超過 105.
Input
輸入文件包含多組數據,請處理到文件結束。
每組數據包含 2 個整數 h 和 c.
Output
對於每組數據輸出 1 個整數表示 t0 的最大值。
Sample Input
100 99
100 100
100 149
Sample Output
100 101 150
模擬,讀清題意就可以了。
#include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <algorithm> #include <math.h> #include <queue> #include <set> #include <stack> #include <vector> #include <deque> #include <list>
using namespace std; typedef long long LL; const int INF=0x3f3f3f3f; const double eps=1e-8; const double pi=acos(-1.0); const int MOD=10056; const int maxn=2016; int h,c; int main() { while(scanf("%d %d",&h,&c)!=EOF) { if(h>c) printf("%d\n",h); else printf("%d\n",c+1); } return 0; } /********************************************************************** Problem: 2165 User: jk1601zr Language: C++ Result: AC Time:112 ms Memory:2024 kb **********************************************************************/