小技巧:本文之前由csdn自動生成了一個目錄,不必下拉一個一個去找,可通過目錄標題直接定位。
本文轉載自本人的csdn博客,復制過來的,排版就不弄了,歡迎轉載。
聲明:
題目部分皆為南陽OJ題目。
代碼部分包含AC代碼(可能不止一個)和最優代碼,大部分都是本人寫的,並且大部分為c代碼和少部分c++代碼and極少java代碼,但基本都是c語言知識點,沒有太多差別,可能代碼有的寫的比較丑,畢竟知識有限。
語言入門部分題基本都較為簡單,是學習編程入門的很好練習,也是ACM的第一步,入門的最佳方法,望認真對待。
本文由csdn-jtahstu原創,轉載請注明出處,歡迎志同道合的朋友一起交流學習。本人QQ:1373758426和csdn博客地址,鏈接:http://blog.csdn.net/jtahstu
該分類南陽OJ地址:我是地址,鏈接:http://acm.nyist.edu.cn/JudgeOnline/problemset.php?typeid=1
Now begin
1、
A+B Problem
- 描述
-
此題為練手用題,請大家計算一下a+b的值
- 輸入
- 輸入兩個數,a,b
- 輸出
- 輸出a+b的值
- 樣例輸入
-
2 3
- 樣例輸出
-
5
- 提示
-
例如:
C語言版:
#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); printf("%d\n",a+b); }
C++版:
#include<iostream> using namespace std; int main() { int a,b; cin>>a>>b; cout<<a+b<<endl; }
Java版:
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) throws Exception { Scanner cin=new Scanner(System.in); int a=cin.nextInt(),b=cin.nextInt(); System.out.println(a+b); } }
Java jdk 1.4 版
import java.io.*; import java.util.*; public class Main { public static void main (String args[]) throws Exception { BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); String line = stdin.readLine(); StringTokenizer st = new StringTokenizer(line); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); System.out.println(a+b); } }
請注意不要輸出過多提示性語句(如:“please input two numbers”),不然會WrongAnswer的
#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); printf("%d\n",a+b); } #include<iostream> main(){std::cout<<(1<<31)-1;}
4、
ASCII碼排序
- 描述
- 輸入三個字符(可以重復)后,按各字符的ASCII碼從小到大的順序輸出這三個字符。
#include<stdio.h> #define MAX 3 char a[MAX]; int main() { int n; scanf("%d\n", &n); while (n--) { char x, y, z; scanf("%s", a); x = a[0]; if (a[1] < x) x = a[1]; if (a[2] < x) x = a[2]; z = a[2]; if (a[1] > z) z = a[1]; if (a[0] > z) z = a[0]; y = a[0] + a[1] + a[2] - x - z; printf("%c %c %c\n", x, y, z); } return 0; } #include "stdio.h"//最優程序 main() { char a,b,c,d; int i; scanf("%d",&i); getchar(); while(i--) { scanf("%c%c%c",&a,&b,&c); getchar(); if (a>b) {d=a;a=b;b=d;} if (a>c) {d=a;a=c;c=d;} if (b>c) {d=b;b=c;c=d;} printf("%c %c %c\n",a,b,c); } }
11、
奇偶數分離
- 描述
- 有一個整型偶數n(2<= n <=10000),你要做的是:先把1到n中的所有奇數從小到大輸出,再把所有的偶數從小到大輸出。
# include"stdio.h" int main() { int i, b, c, d, j, k; scanf("%d", &i); for (d = 0; d < i; d++) { scanf("%d", &b); for (k = 1; k <= b; k++) { if ((k % 2) == 1) printf("%d ", k); } printf("\n"); for (j = 1; j <= b; j++) { if ((j % 2) == 0) printf("%d ", j); } printf("\n"); } return 0; } #include<stdio.h>//最優程序 int main() { int n; scanf("%d", &n); int a; while (n--) { scanf("%d", &a); for (int i = 1; i <= a; i += 2) printf("%d ", i); puts(""); for (int i = 2; i <= a; i += 2) printf("%d ", i); puts(""); } }
13、
Fibonacci數
- 描述
-
無窮數列1,1,2,3,5,8,13,21,34,55...稱為Fibonacci數列,它可以遞歸地定義為
F(n)=1 ...........(n=1或n=2)
F(n)=F(n-1)+F(n-2).....(n>2)
現要你來求第n個斐波納奇數。(第1個、第二個都為1)
#include <stdio.h> int F(int n) { if (n == 1 || n == 2) { return 1; } else { return F(n - 1) + F(n - 2); } } int main() { int i, n; scanf("%d", &i); while (i--) { scanf("%d", &n); printf("%d\n", F(n)); } return 0; } #include<stdio.h>//最優程序 main() { int m,n,i,s1,s2; scanf("%d",&m); while(m--) { scanf("%d",&n); for(i=3,s1=s2=1;i<=n;i++) { s1=s1+s2;s2=s1-s2; } printf("%d\n",s1); } }
22、
素數求和問題
- 描述
-
現在給你N個數(0<N<1000),現在要求你寫出一個程序,找出這N個數中的所有素數,並求和。
- 輸入
-
第一行給出整數M(0<M<10)代表多少組測試數據
每組測試數據第一行給你N,代表該組測試數據的數量。
接下來的N個數為要測試的數據,每個數小於1000 - 輸出
- 每組測試數據結果占一行,輸出給出的測試數據的所有素數和
- 樣例輸入
-
3 5 1 2 3 4 5 8 11 12 13 14 15 16 17 18 10 21 22 23 24 25 26 27 28 29 30
- 樣例輸出
-
10 41 52
- 來源
- [hzyqazasdf]原創
- 上傳者
- hzyqazasdf
#include<stdio.h> #include<math.h> int main() { int i, j, k, a, n, m, sum = 0; scanf("%d", &a); for (i = 0; i < a; i++) { scanf("%d", &n); for (j = 0; j < n; j++) { scanf("%d", &m); for (k = 2; k <= sqrt(m); k++) { if (m % k == 0) break; } if (k > sqrt(m) && m != 1) sum = sum + m; } printf("%d\n", sum); sum = 0; } return 0; } #include<stdio.h>//最優程序 #include <math.h> int main() { int m,n,i,j,a[1000],flag=0; long s; scanf("%d",&m); while(m--) { s=0; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { if(a[i]==1) continue; flag=0; for(j=2;j<=sqrt(a[i]);j++) { if(a[i]%j==0) {flag=1;break;} } if(flag==0) s+=a[i]; } printf("%d\n",s); } return 0; }
24、
素數距離問題
- 描述
-
現在給出你一些數,要求你寫出一個程序,輸出這些整數相鄰最近的素數,並輸出其相距長度。如果左右有等距離長度素數,則輸出左側的值及相應距離。
如果輸入的整數本身就是素數,則輸出該素數本身,距離輸出0- 輸入
-
第一行給出測試數據組數N(0<N<=10000)
接下來的N行每行有一個整數M(0<M<1000000), - 輸出
-
每行輸出兩個整數 A B.
其中A表示離相應測試數據最近的素數,B表示其間的距離。 - 樣例輸入
-
3 6 8 10
- 樣例輸出
-
5 1 7 1 11 1
- 來源
- 經典題目
- 上傳者
- hzyqazasdf
#include<stdio.h> #include<math.h> bool judge(int m); int main() { int N,n,i; scanf("%d",&N); while(N--) { scanf("%d",&n); if(judge(n)) { printf("%d 0\n",n); continue; } for(i=1; n-i!=-1; i++) { if(judge(n-i)) { printf("%d %d\n",n-i,i); break; } if(judge(n+i)) { printf("%d %d\n",n+i,i); break; } } } return 0; } bool judge(int m) { if(m==0||m==1) return false; int i; for(i=2; i<=sqrt(m); i++) { if(m%i==0)break; } if(i>sqrt(m)) return true; return false; } #include<iostream>//最優程序 #include<cmath> using namespace std; bool isprime(int n) { for(int k=2;k<=sqrt((double)n);k++) if((n%k)==0) return false; return true; } int main() { int n; cin>>n; while(n--) { int num,i,j; cin>>num; if(num==1) { cout<<"2 1"<<endl; continue; } for(i=num;!isprime(i);i--); for(j=num;!isprime(j);j++); if((num-i)<(j-num)) cout<<i<<' '<<(num-i)<<endl; else if((num-i)>(j-num)) cout<<j<<' '<<(j-num)<<endl; else if((num-i)==(j-num)) cout<<i<<' '<<(num-i)<<endl; } }
25、
A Famous Music Composer
- 描述
-
Mr. B is a famous music composer. One of his most famous work was his set of preludes. These 24 pieces span the 24 musical keys (there are musically distinct 12 scale notes, and each may use major or minor tonality). The 12 distinct scale notes are:
A A#=Bb B C C#=Db D D#=Eb E F F#=Gb G G#=Ab Five of the notes have two alternate names, as is indicated above with equals sign. Thus, there are 17 possible names of scale notes, but only 12 musically distinct notes. When using one of these as the keynote for a musical key, we can further distinguish between major and minor tonalities. This gives 34 possible keys, of which 24 are musically distinct.In naming his preludes, Mr. B used all the keys except the following 10, which were named instead by their alternate names:Ab minor A# major A# minor C# major Db minor D# major D# minor Gb major Gb minor G# major Write a program that, given the name of a key, give an alternate name if it has one, or report the key name is unique.- 輸入
- Each test case is described by one line having the format "note tonality", where "note" is one of the 17 names for the scale notes given above, and "tonality" is either "major" or "minor" (quotes for clarify).
- 輸出
- For each case output the required answer, following the format of the sample.
- 樣例輸入
-
Ab minor D# major G minor
- 樣例輸出
-
Case 1: G# minor Case 2: Eb major Case 3: UNIQUE
- 來源
- hdu
- 上傳者
- 李如兵
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; char str[100]; int cas = 1; int main() { while(gets(str)) { printf("Case %d: ",cas++); int i,j,len; len = strlen(str); if(str[1] == ' ') printf("UNIQUE\n"); else { if(str[1] == '#') { if(str[0] == 'G') printf("Ab"); else printf("%cb",str[0]+1); } else if(str[1] == 'b') { if(str[0] == 'A') printf("G#"); else printf("%c#",str[0]-1); } for(i = 2;i<len;i++) printf("%c",str[i]); printf("\n"); } } return 0; } #include<iostream>//最優程序 #include<string> using namespace std; string trans(string a){ string b=""; if(a[1]=='#'){ b+=char((a[0]-'A'+1)%7+'A'); b+='b'; }else{ b+=char((a[0]-'A'+6)%7+'A'); b+='#'; } return b; } int main(){ string a,b; for(int t=1; cin>>a>>b; t++){ cout<<"Case "<<t<<": "; if(a.length()==1) cout<<"UNIQUE"<<endl; else cout<<trans(a)<<" "<<b<<endl; } return 0; }
31、
5個數求最值
- 描述
-
設計一個從5個整數中取最小數和最大數的程序
- 輸入
- 輸入只有一組測試數據,為五個不大於1萬的正整數
- 輸出
- 輸出兩個數,第一個為這五個數中的最小值,第二個為這五個數中的最大值,兩個數字以空格格開。
- 樣例輸入
-
1 2 3 4 5
- 樣例輸出
-
1 5
- 來源
- C語言課本第四章第一題
- 上傳者
- 張雲聰
#include<stdio.h> int main() { int a[5], temp, i, j; for (i = 0; i < 5; i++) scanf("%d", &a[i]); for (j = 0; j < 5; j++) for (i = 0; i < 4; i++) if (a[i] > a[i + 1]) { temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } printf("%d %d", a[0], a[4]); return 0; } #include<iostream>//最優程序 #include<iterator> #include<algorithm> using namespace std; int main() { int a[5]; copy(istream_iterator<int>(cin),istream_iterator<int>(),a); cout<<*min_element(a,a+5)<<" "<<*max_element(a,a+5)<<endl; }
33、
蛇形填數
- 描述
-
在n*n方陳里填入1,2,...,n*n,要求填成蛇形。例如n=4時方陳為:
10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 4
#include"stdio.h" #include<stdlib.h> int main() { int a[109][109]= {0},i,j,k,n,m,top,x,y; top=1; scanf("%d",&n); a[x=0][y=n-1]=1; while(top<n*n) { while(x+1<n&&!a[x+1][y]) a[++x][y]=++top; while(y-1>=0&&!a[x][y-1]) a[x][--y]=++top; while(x-1>=0&&!a[x-1][y]) a[--x][y]=++top; while(y+1<n&&!a[x][y+1]) a[x][++y]=++top; } for(i=0; i<n; i++) { for(j=0; j<n; j++) printf("%d ",a[i][j]); printf("\n"); } //system("33.exe\n"); } #include<stdio.h>//最優程序 int main() { int a,b,c,d,n,sum=1; int yi[101][101]; scanf("%d",&n); for(a=0;a<=(n-1)/2;a++) { for(b=a;b<=n-a-1;b++) yi[b][n-a-1]=sum++; for(b=n-2-a;b>=a;b--) yi[n-a-1][b]=sum++; for(b=n-a-2;b>=a;b--) yi[b][a]=sum++; for(b=a+1;b<n-a-1;b++) yi[a][b]=sum++; } for(c=0;c<n;c++) { for(d=0;d<n;d++) printf("%d ",yi[c][d]); printf("\n"); } }
34、
韓信點兵
- 描述
- 相 傳韓信才智過人,從不直接清點自己軍隊的人數,只要讓士兵先后以三人一排、五人一排、七人一排地變換隊形,而他每次只掠一眼隊伍的排尾就知道總人數了。輸 入3個非負整數a,b,c ,表示每種隊形排尾的人數(a<3,b<5,c<7),輸出總人數的最小值(或報告無解)。已知總人數不小於10,不超過100 。
#include<iostream> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; int n=(a*70+b*21+c*15)%105; if(n>100||n<10) cout<<"No answer"<<endl; else cout<<n<<endl; }
39、
水仙花數
- 描述
-
請判斷一個數是不是水仙花數。
其中水仙花數定義各個位數立方和等於它本身的三位數。
#include "stdio.h" int main(int argc, char const *argv[]) { int n=0; //freopen("input.txt","r",stdin); while(scanf("%d",&n)==1) { if(n!=0) { if (n==153||n==370||n==371||n==407) printf("Yes\n"); else printf("No\n"); } } return 0; } #include<iostream>//最優程序 using namespace std; int main() { int a; while(1) { cin>>a; if(a==0) break; cout<<((a==153||a==370||a==371||a==407)?"Yes":"No")<<endl; } }
40、
公約數和公倍數
- 描述
- 小明被一個問題給難住了,現在需要你幫幫忙。問題是:給出兩個正整數,求出它們的最大公約數和最小公倍數。
#include<stdio.h> int main() { int a, b, c, n, k; scanf("%d", &n); //輸入一個整數n(0<n<=10000),表示有n組測試數據 while (n--) { scanf("%d %d", &a, &b); //輸入兩個整數 k = a * b; while (b != 0) { c = a % b; a = b; b = c; } printf("%d %d\n", a, k / a); } return 0; } import java.io.*; import java.util.*; public class Main { public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static Scanner count = new Scanner(System.in); public static void main(String[] arges) { int n = count.nextInt(); while ((n--) > 0) { int a = count.nextInt(), b = count.nextInt(); System.out.println(gcd(a, b) + " " + a * b / gcd(a, b)); } count.close(); } } #include<stdio.h> int main() { unsigned int u,v,r,s,i,d; scanf("%u",&s); for(i=1;i<=s;i++) { scanf("%u%u",&u,&v); d=u*v; while(v!=0) { r=u%v; u=v; v=r; } printf("%u %u\n",u,d/u); } return 0; }
41、
三個數從小到大排序
import java.util.Scanner; public class Main { public static void main(String[] args) { int a,b,c,n; Scanner num=new Scanner(System.in); System.out.print(""); a=num.nextInt(); System.out.print(""); b=num.nextInt(); System.out.print(""); c=num.nextInt(); if(a>b) { n=a;a=b;b=n; } if(b>c) { n=b;b=c;c=n; } if(a>c) { n=a;a=c;c=n; } System.out.println(a+" "+b+" "+c); } } #include <stdio.h>//最優程序 int main() { int a,b,c,an[3],i,t,j,max,flag; scanf ("%d %d %d",&an[0],&an[1],&an[2]); for (i=0;i<3;i++) { t=max=an[i]; flag=i; for (j=i;j<3;j++) if (an[j]>t) { max=an[j]; flag=j; }; t=an[i]; an[i]=max; an[flag]=t; } for (i=2;i>=0;i--) printf ("%d ",an[i]); return 0; }
56、
階乘因式分解(一)
- 描述
- 輸入
-
第一行是一個整數s(0<s<=100),表示測試數據的組數
隨后的s行, 每行有兩個整數n,m。 - 輸出
- 輸出m的個數。
- 樣例輸入
-
2 100 5 16 2
- 樣例輸出
-
24 15
- 來源
- 網絡
- 上傳者
- 苗棟棟
#include <stdio.h> int main(void) { int N, n, m, count; scanf("%d", &N); while (N--) { count = 0; scanf("%d%d", &n, &m); while (n) { n = n / m; count = count + n; } printf("%d\n", count); } return 0; } #include<iostream>//最優程序 using namespace std; int get(int n,int num) { if(n==0) return 0; else return get(n/num,num)+n/num; } int main() { int n; cin>>n; while(n--) { int a,b; cin>>a>>b; cout<<get(a,b)<<endl; } }
57、
6174問題
- 描述
-
假 設你有一個各位數字互不相同的四位數,把所有的數字從大到小排序后得到a,從小到大后得到b,然后用a-b替換原來這個數,並且繼續操作。例如,從 1234出發,依次可以得到4321-1234=3087、8730-378=8352、8532-2358=6174,又回到了它自己!現在要你寫一個 程序來判斷一個四位數經過多少次這樣的操作能出現循環,並且求出操作的次數
比如輸入1234執行順序是1234->3087->8352->6174->6174,輸出是4
#include<stdio.h> int main() { int n,m,a[4],i,j,count,max,min,t; scanf("%d",&n); while(n--) { count=1; scanf("%d",&m); while(m!=6174) { for(i=0; i<4; i++) { a[i]=m%10; m=m/10; } for(i=0; i<4; i++) for(j=i+1; j<4; j++) if(a[i]<a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } max=a[0]*1000+a[1]*100+a[2]*10+a[3]; min=a[3]*1000+a[2]*100+a[1]*10+a[0]; m=max-min; count++; } printf("%d\n",count); } return 0; } #include<iostream>//最優程序 #include<algorithm> #include<stdio.h> using namespace std; int main() { //freopen("1.txt","r",stdin); int k; cin>>k; while(k--) { int n,a[4],n1,n2; scanf("%d",&n); int s=1; while(n!=6174) { a[0]=n%10; a[3]=n/1000; a[1]=n/10%10; a[2]=n/100%10; sort(a,a+4); n1=1000*a[3]+100*a[2]+10*a[1]+a[0]; n2=1000*a[0]+100*a[1]+10*a[2]+a[3]; n=n1-n2; s++; } printf("%d\n",s); } }
60、
誰獲得了最高獎學金
- 描述
-
某校的慣例是在每學期的期末考試之后發放獎學金。發放的獎學金共有五種,獲取的條件各自不同:
1) 院士獎學金,每人8000元,期末平均成績高於80分(>80),並且在本學期內發表1篇或1篇以上論文的學生均可獲得;
2) 五四獎學金,每人4000元,期末平均成績高於85分(>85),並且班級評議成績高於80分(>80)的學生均可獲得;
3) 成績優秀獎,每人2000元,期末平均成績高於90分(>90)的學生均可獲得;
4) 西部獎學金,每人1000元,期末平均成績高於85分(>85)的西部省份學生均可獲得;
5) 班級貢獻獎,每人850元,班級評議成績高於80分(>80)的學生干部均可獲得;
只要符合條件就可以得獎,每項獎學金的獲獎人數沒有限制,每名學生也可以同時獲得多項獎學金。例如姚林的期末平均成績是87分,班級評議成績82分,同時他還是一位學生干部,那么他可以同時獲得五四獎學金和班級貢獻獎,獎金總數是4850元。
現在給出若干學生的相關數據,請計算哪些同學獲得的獎金總數最高(假設總有同學能滿足獲得獎學金的條件)。- 輸入
- 第 一行輸入數據N,表示測試數據組數(0<N<100),每組測試數據輸入的第一行是一個整數X(1 <= X <= 100),表示學生的總數。接下來的X行每行是一位學生的數據,從左向右依次是姓名,期末平均成績,班級評議成績,是否是學生干部,是否是西部省份學生, 以及發表的論文數。姓名是由大小寫英文字母組成的長度不超過20的字符串(不含空格);期末平均成績和班級評議成績都是0到100之間的整數(包括0和 100);是否是學生干部和是否是西部省份學生分別用一個字符表示,Y表示是,N表示不是;發表的論文數是0到10的整數(包括0和10)。每兩個相鄰數 據項之間用一個空格分隔。
- 輸出
- 每組測試數據輸出包括三行,第一行是獲得最多獎金的學生的姓名,第二行是這名學生獲得的獎金總數。如果有兩位或兩位以上的學生獲得的獎金最多,輸出他們之中在輸入文件中出現最早的學生的姓名。第三行是這X個學生獲得的獎學金的總數。
- 樣例輸入
-
1 4 YaoLin 87 82 Y N 0 ChenRuiyi 88 78 N Y 1 LiXin 92 88 N N 0 ZhangQin 83 87 Y N 1
- 樣例輸出
-
ChenRuiyi 9000 28700
- 來源
- NOIP2005
- 上傳者
- hzyqazasdf
#include <iostream> using namespace std; int main(void) { string str,_str; char ca,cb; int n,m,x,y,z,s,sum,max; cin>>n; while(n--) { cin>>m; max = sum = 0; while(m--) { s = 0; cin>>str>>x>>y>>ca>>cb>>z; if(x>80&&z>0) { s += 8000; sum += 8000; } if(x>85&&y>80) { s += 4000; sum += 4000; } if(x>90) { s += 2000; sum += 2000; } if(x>85&&cb == 'Y') { s += 1000; sum += 1000; } if(y>80&&ca == 'Y') { s += 850; sum += 850; } if(max < s) { max = s; _str = str; } } cout<<_str<<endl<<max<<endl<<sum<<endl; } } #include<iostream>//最優程序 #include<string> #include<algorithm> #include<numeric> using namespace std; int calc(int qm,int py,bool gb,bool xb,bool lw) { int all=0; if(qm>80 && lw) all+=8000; if(qm>85&& py>80) all+=4000; if(qm>90) all+=2000; if(xb&&qm>85) all+=1000; if(gb&&py>80) all+=850; return all; } int main() { int n; cin>>n; while(n--) { int m; cin>>m; int max_num=0,all=0; string max_stu; while(m--) { int qm,py,lw; string xm,gbs,xbs; cin>>xm>>qm>>py>>gbs>>xbs>>lw; bool gb=gbs=="Y",xb=xbs=="Y"; int num=calc(qm,py,gb,xb,lw>0); all+=num; if(num>max_num) {max_num=num;max_stu=xm;} } cout<<max_stu<<endl<<max_num<<endl<<all<<endl; } }
62、
笨小熊
- 描述
-
笨小熊的詞匯量很小,所以每次做英語選擇題的時候都很頭疼。但是他找到了一種方法,經試驗證明,用這種方法去選擇選項的時候選對的幾率非常大!
這種方法的具體描述如下:假設maxn是單詞中出現次數最多的字母的出現次數,minn是單詞中出現次數最少的字母的出現次數,如果maxn-minn是一個質數,那么笨小熊就認為這是個Lucky Word,這樣的單詞很可能就是正確的答案。- 輸入
-
第一行數據N(0<N<100)表示測試數據組數。
每組測試數據輸入只有一行,是一個單詞,其中只可能出現小寫字母,並且長度小於100。 - 輸出
-
每組測試數據輸出共兩行,第一行是一個字符串,假設輸入的的單詞是Lucky Word,那么輸出“Lucky Word”,否則輸出“No Answer”;
第二行是一個整數,如果輸入單詞是Lucky Word,輸出maxn-minn的值,否則輸出0 - 樣例輸入
-
2 error olympic
- 樣例輸出
-
Lucky Word 2 No Answer 0
- 來源
- NOIP2008
- 上傳者
- hzyqazasdf
#include<iostream> #include<string> using namespace std; int prime_number(int n) { if(n==1||n==0)return 0; if(n==2)return 1; else for(int i=2;i*i<=n;i++) { if(n%i==0)return 0; } return 1; } int maxn_minn(string a) { int b[101]={0}; for(int i=0; i<a.size(); i++) for(int j=0; j<a.size(); j++) { if(a[i]==a[j]) b[i]++; } int maxn=b[0]; int minn=b[0]; for(int i=0;i<a.size();i++) { if(b[i]>maxn)maxn=b[i]; if(b[i]<minn)minn=b[i]; } return maxn-minn; } int main() { int n; cin>>n; while(n--) { string a; cin>>a; prime_number(maxn_minn(a))==1?cout<<"Lucky Word\n"<<maxn_minn(a)<<endl:cout<<"No Answer\n"<<"0"<<endl; } return 0; } #include<iostream>//最優程序 #include<string> #include<algorithm> #include<numeric> using namespace std; bool isPrime(int n) { if(n==0) return false; if(n==1) return false; if(n==2) return true; for(int i=2;i*i<=n;i++) { if(n%i==0) return false; } return true; } int min_e(int *p,int *q) { int m=1000; for(int* i=p;i!=q;i++) { if(*i<m && *i!=0) m=*i; } return m; } int main() { int n; string str; cin>>n; while(n--) { int count[26]={0}; cin>>str; for(int i=0;i!=str.size();++i) { ++count[str[i]-'a']; } int nn=*max_element(count,count+26)-min_e(count,count+26); if(isPrime(nn)) cout<<"Lucky Word"<<endl<<nn<<endl; else cout<<"No Answer"<<endl<<0<<endl; } }
64、
雞兔同籠
- 描述
- 已知雞和兔的總數量為n,總腿數為m。輸入n和m,依次輸出雞和兔的數目,如果無解,則輸出“No answer”(不要引號)。
#include "stdio.h" int main(int argc, char const *argv[]) { int i,m,n,N; scanf("%d",&N); while(N--) { int x,y,a=0; scanf("%d%d",&n,&m); for(i=0;i<=n;i++) { if(i*2+(n-i)*4==m) { a=1; printf("%d %d\n",i,n-i);break; } } if(a==0)printf("No answer\n"); } return 0; } #include<iostream>//最優程序 using namespace std; int main() { int n,a,b,p,q; cin>>n; while(n--) { cin>>a>>b; q=(b-2*a)/2; p=a-q; if(p<0 ||q<0 || b%2) cout<<"No answer"<<endl; else cout<<p<<" "<<q<<endl; } }
65、
另一種階乘問題
- 描述
-
大家都知道階乘這個概念,舉個簡單的例子:5!=1*2*3*4*5.現在我們引入一種新的階乘概念,將原來的每個數相乘變為i不大於n的所有奇數相乘例如:5!!=1*3*5.現在明白現在這種階乘的意思了吧!
現在你的任務是求出1!!+2!!......+n!!的正確值(n<=20)
#include <stdio.h> int f(int n) { int i, s = 1; for (i = 1; i <= n; i += 2) s *= i; return s; } int main() { int a, n, i, s; scanf("%d", &a); while (a--) { s = 0; scanf("%d", &n); for (i = 1; i <= n; i++) s += f(i); printf("%d\n", s); } return 0; } /* #include<iostream> using namespace std; int f(int n) { if(n%2) return n==1?1:n*f(n-2); return f(n-1); } int g(int n) { return n?g(n-1)+f(n):0; } int main() { int n,m; cin>>n; while(n--) { cin>>m; cout<<g(m)<<endl; } }*/ #include<iostream>//最優程序 using namespace std; int main() { int n,m,r[]={0,1,2,5,8,23,38,143,248,1193,2138,12533,22928,158063,293198,2320223,4347248,38806673,73266098,727995173,1382724248}; cin>>n; while(n--) { cin>>m; cout<<r[m]<<endl; } }
72、
Financial Management
- 描述
-
Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems to have enough. Larry has decided that he needs to grab hold of his financial portfolio and solve his financing problems. The first step is to figure out what's been going on with his money. Larry has his bank account statements and wants to see how much money he has. Help Larry by writing a program to take his closing balance from each of the past twelve months and calculate his average account balance.
- 輸入
- The input will be twelve lines. Each line will contain the closing balance of his bank account for a particular month. Each number will be positive and displayed to the penny. No dollar sign will be included.
- 輸出
- The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be no other spaces or characters in the output.
- 樣例輸入
-
100.00 489.12 12454.12 1234.10 823.05 109.20 5.27 1542.25 839.18 83.99 1295.01 1.75
- 樣例輸出
-
1581.42
- 來源
- [張潔烽]原創
- 上傳者
- 張潔烽
#include "stdio.h" int main(int argc, char const *argv[]) { double sum=0,a; int n=12; // freopen("input.txt","r",stdin); while(n--) { scanf("%lf",&a); sum+=a; } printf("%.2lf\n",sum/12); return 0; } #include<iostream>//最優程序 #include<iomanip> using namespace std; int main() { double sum=0,a; for(int i=0;i<12;i++) { cin>>a; sum+=a; } cout<<fixed<<setprecision(2)<<sum/12.0<<endl; }
74、
小學生算術
- 描述
- 很多小學生在學習加法時,發現“進位”特別容易出錯。你的任務是計算兩個三位數在相加時需要多少次進位。你編制的程序應當可以連續處理多組數據,直到讀到兩個0(這是輸入結束標記)。
#include <stdio.h> int main() { int m,n,i,j; int a[3],b[3]; while(scanf("%d%d",&m,&n),m!=0||n!=0) { a[0]=m/100; a[1]=(m-100*a[0])/10; a[2]=m%10; b[0]=n/100; b[1]=(n-100*b[0])/10; b[2]=n%10; j=0; for(i=2; i>=0; i--) { if(a[i]+b[i]>=10) { j++; a[i-1]+=1;//要考慮進位問題 } } printf("%d\n",j); } return 0; } #include<stdio.h> int main() { int a,b,c,d,e,f,n,m,i; for(;;) { scanf("%d%d",&n,&m); if(n==0&&m==0) return 0; else { i=0; a=n/100;b=n%100/10;c=n%10; d=m/100;e=m%100/10;f=m%10; if(c+f>=10) {i+=1;b+=1;} if(b+e>=10) {i+=1;a+=1;} if(a+d>=10) {i+=1;} printf("%d\n",i); } } return 0; }
75、
日期計算
- 描述
-
如題,輸入一個日期,格式如:2010 10 24 ,判斷這一天是這一年中的第幾天。
- 輸入
- 第一行輸入一個數N(0<N<=100),表示有N組測試數據。后面的N行輸入多組輸入數據,每行的輸入數據都是一個按題目要求格式輸入的日期。
- 輸出
- 每組輸入數據的輸出占一行,輸出判斷出的天數n
- 樣例輸入
-
3 2000 4 5 2001 5 4 2010 10 24
- 樣例輸出
-
96 124 297
- 來源
- [naonao]改編C語言習題
- 上傳者
- naonao
#include <iostream> using namespace std; int leap(int a) { if(a%4==0&&a%100!=0||a%400==0) return 1; else return 0; } int number(int year,int m,int d) { int sum=0,i,j,k,a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int b[12]={31,29,31,30,31,30,31,31,30,31,30,31}; if(leap(year)==1) for(i=0;i<m-1;i++) sum+=b[i]; else for(i=0;i<m-1;i++) sum+=a[i]; sum+=d; return sum; } int main(int argc, char const *argv[]) { int n; cin>>n; while(n--) { int year,month,day,n; cin>>year>>month>>day; n=number(year,month,day); cout<<n<<endl; } return 0; } #include<stdio.h>//最優程序 int main() { int a,b=0,c,y,m,d,fib; scanf("%d",&a); while(a--) { scanf("%d %d %d",&y,&m,&d); if(y%400==0||y%100!=0&&y%4==0) fib=29; else fib=28; for(c=1;c<=m;c++) switch(c-1) { case 1: case 3: case 5: case 7: case 8: case 10:b+=31;break; case 2:b+=fib;break; case 4: case 6: case 9: case 11:b+=30;break; } b+=d; printf("%d\n",b); b=0; } return 0; }
77、
開燈問題
- 描述
-
有 n盞燈,編號為1~n,第1個人把所有燈打開,第2個人按下所有編號為2 的倍數的開關(這些燈將被關掉),第3 個人按下所有編號為3的倍數的開關(其中關掉的燈將被打開,開着的燈將被關閉),依此類推。一共有k個人,問最后有哪些燈開着?輸入:n和k,輸出開着的 燈編號。k≤n≤1000
#include<stdio.h> int main( ) { int n, k, j, open,i; scanf("%d%d", &n, &k ); for(i = 1 ; i <= n ; i++ ) { open = 1; for( j = 2 ; j <= k ; j++ ) { if( i % j == 0 ) open = (open+1) % 2; } if( open ) printf("%d ", i ); } return 0; } #include <iostream>//最優程序 using namespace std; int main() { int n,k,a[1000],i; cin>>n>>k; for(i=0;i<n;i++) a[i]=1; for(i=2;i<=k;i++) for(int j=0;j<n;j++) { if((j+1)%i==0) if(a[j]==0)a[j]=1; else a[j]=0; } for(i=0;i<n;i++) if(a[i]==1)cout<<i+1<<" "; cout<<endl; return 0; }
94、
cigarettes
- 描述
-
Tom has many cigarettes. We hypothesized that he has n cigarettes and smokes them
one by one keeping all the butts. Out of k > 1 butts he can roll a new cigarette.
Now,do you know how many cigarettes can Tom has?- 輸入
- First input is a single line,it's n and stands for there are n testdata.then there are n lines ,each line contains two integer numbers giving the values of n and k.
- 輸出
- For each line of input, output one integer number on a separate line giving the maximum number of cigarettes that Peter can have.
- 樣例輸入
-
3 4 3 10 3 100 5
- 樣例輸出
-
5 14 124
- 來源
- [rooot]原創
- 上傳者
- rooot
#include<iostream> using namespace std; int main() { int n;cin>>n; while(n--) { int a,b; cin>>a>>b; int sum=0; sum+=a; while(a>=b) { a-=b;//只要a-b還大於b , 就相當於a會多一個 a++; sum++; } cout<<sum<<endl; } return 0; } #include "stdio.h" #include<fstream> int main() { //freopen("d:\\1.txt","r",stdin); //freopen("d:\\2.txt","w",stdout); int m; scanf("%d",&m); while(m--) { int n,k,sum; scanf("%d%d",&n,&k); sum=n; while(n/k) { sum+=n/k; n=n/k+n%k; } printf("%d\n",sum); } return 0; }
96、
n-1位數
- 描述
-
已知w是一個大於10但不大於1000000的無符號整數,若w是n(n≥2)位的整數,則求出w的后n-1位的數。
#include<stdio.h><pre name="code" class="cpp">//較簡單的做法 int main() { int num; int M; scanf("%d",&num); while(num--) { scanf("%d",&M); int i=10; while(M/i>=10) { i=i*10; } printf( "%d\n" ,M%i); } } #include<cstdio>//最優程序 int main() { int n,m; scanf("%d",&n); while(n--) { scanf("\n%*c%d",&m); printf("%d\n",m); } }
97、
兄弟郊游問題
- 描述
- 兄弟倆騎車郊游,弟弟先出發,每分鍾X米,M分鍾后,哥哥帶一條狗出發。以每分鍾Y米的速度去追弟弟,而狗則以每分鍾Z米的速度向弟弟跑去,追上弟弟后又立即返回,直到哥哥追上弟弟時,狗跑了多少米?
#include "stdio.h" int main() { int n; scanf("%d",&n); while(n--) { double m,x,y,z,s; scanf("%lf%lf%lf%lf",&m,&x,&y,&z); s=x*m/(y-x)*z; printf("%.2lf\n",s); } return 0; } #include<iostream>//最優程序 #include<stdio.h> using namespace std; int main() { int n; cin>>n; while(n--) { int s,a,b,c; cin>>s>>a>>b>>c; printf("%.2lf\n",s*a/(double)(b-a)*c); } }
98、
成績轉換
- 描述
-
輸入一個百分制的成績M,將其轉換成對應的等級,具體轉換規則如下:
90~100為A;
80~89為B;
70~79為C;
60~69為D;
0~59為E;
#include "stdio.h" int main(int argc, char const *argv[]) { int n,a; scanf("%d",&n); while(n--) { scanf("%d",&a); if(90<=a&&a<=100) printf("A\n"); if(80<=a&&a<=89) printf("B\n"); if(70<=a&&a<=79) printf("C\n"); if(60<=a&&a<=69) printf("D\n"); if(0<=a&&a<=59) printf("E\n"); } return 0; } #include<iostream>//最優程序 using namespace std; int main() { int n,s; cin>>n; while(n--) { cin>>s; switch(s/10) { case 10: case 9:cout<<"A"<<endl;break; case 8:cout<<"B"<<endl;break; case 7:cout<<"C"<<endl;break; case 6:cout<<"D"<<endl;break; default:cout<<"E"<<endl;break; } } }
100、
1的個數
- 描述
- 小南剛學了二進制,他想知道一個數的二進制表示中有多少個1,你能幫他寫一個程序來完成這個任務嗎?
#include <iostream> #include <stdio.h> using namespace std; int main(int argc, char const *argv[]) { int n; cin>>n; while(n--) { int a,i,count=0; cin>>a; while(a) { if(a%2==1) count++; a/=2; } cout<<count<<endl; } return 0; } #include<stdio.h>//最優程序 main(){int n,m,s;scanf("%d",&n);while(n--){scanf("%d",&m);s=0;while(m)m&=m-1,s++;printf("%d\n",s);}}
101、
兩點距離
- 描述
-
輸入兩點坐標(X1,Y1),(X2,Y2)(0<=x1,x2,y1,y2<=1000),計算並輸出兩點間的距離。
#include<math.h> #include<stdio.h> int main() { int n; double l,x1,x2,y1,y2; // freopen("input.txt","r",stdin); scanf("%d",&n); while(n--) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); l=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); printf("%.2lf\n",l); } return 0; } #include<iostream>//最優程序 #include<math.h> #include<iomanip> using namespace std; int main() { /*freopen("1.txt","r",stdin); freopen("2.txt","w",stdout);*/ double x1,x2,y1,y2,m; double a; cin>>m; while(m--) { cin>>x1>>y1>>x2>>y2; a=sqrt((double)((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))); cout.setf(ios::fixed); cout<<setprecision(2)<<a<<endl; } return 0; }
111、
分數加減法
- 描述
-
編寫一個C程序,實現兩個分數的加減法
- 輸入
-
輸入包含多行數據
每行數據是一個字符串,格式是"a/boc/d"。
其中a, b, c, d是一個0-9的整數。o是運算符"+"或者"-"。
數據以EOF結束
輸入數據保證合法 - 輸出
-
對於輸入數據的每一行輸出兩個分數的運算結果。
注意結果應符合書寫習慣,沒有多余的符號、分子、分母,並且化簡至最簡分數 - 樣例輸入
-
1/8+3/8 1/4-1/2 1/3-1/3
- 樣例輸出
-
1/2 -1/4 0
- 來源
- 水題比賽
- 上傳者
- hzyqazasdf
#include <iostream> #include<cstdlib> using namespace std; int yinshu(int x,int y) { if(x<y) { int tmp; tmp = x; x = y; y = tmp; } if(x%y == 0) return abs(y); else return yinshu(y,x%y); } int main(void)//最優程序 { int a,b,c,d; char optr,num; while(cin >> a >> num >> b >> optr >> c >> num >> d) { int x,y; switch(optr) { case '+': { x = a*d+b*c; y = b*d; break; } case '-': { x = a*d-b*c; y = b*d; break; } } if(x == 0) cout << 0 << endl; else if(y/yinshu(x,y) == 1) cout << x/yinshu(x,y) << endl; else cout << x/yinshu(x,y) << "/" << y/yinshu(x,y) << endl; } return 0; }
113、
字符串替換
- 描述
-
編寫一個程序實現將字符串中的所有"you"替換成"we"
- 輸入
-
輸入包含多行數據
每行數據是一個字符串,長度不超過1000
數據以EOF結束 - 輸出
- 對於輸入的每一行,輸出替換后的字符串
- 樣例輸入
-
you are what you do
- 樣例輸出
-
we are what we do
- 來源
- 水題比賽
- 上傳者
- hzyqazasdf
#include "iostream" #include "cstring" #include "cstdio" using namespace std; int main(int argc, char const *argv[]) { char a[1001]; while(gets(a)!=NULL) { int i,len=0; len=strlen(a); for(i=0; i<len; i++) { if(a[i]=='y'&&a[i+1]=='o'&&a[i+2]=='u') { cout<<"we"; i+=2; } else cout<<a[i]; } cout<<endl; } return 0; } #include<algorithm>//最優程序 #include<iostream> #include<string> using namespace std; int main() { string s, s1, s2; while(getline(cin,s)) { int flag; s1 = "you"; s2 = "we"; flag = s.find(s1,0); while(flag != string::npos) { s.replace(flag, 3, s2); flag = s.find(s1, flag + 1); } cout << s << endl; } return 0; }
122、
Triangular Sums
- 描述
-
The nth Triangular number, T(n) = 1 + … + n, is the sum of the first n integers. It is the number of points in a triangular array with n points on side. For example T(4):
X
X X
X X X
X X X XWrite a program to compute the weighted sum of triangular numbers:
W(n) =
SUM[k = 1…n; k * T(k + 1)]
- 輸入
-
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.
Each dataset consists of a single line of input containing a single integer n, (1 ≤ n ≤300), which is the number of points on a side of the triangle. - 輸出
- For each dataset, output on a single line the dataset number (1 through N), a blank, the value of n for the dataset, a blank, and the weighted sum ,W(n), of triangular numbers for n.
- 樣例輸入
-
4 3 4 5 10
- 樣例輸出
-
1 3 45 2 4 105 3 5 210 4 10 2145
- 來源
- Greater New York 2006
- 上傳者
- 張雲聰
#include<iostream> using namespace std; int main() { int n,j=1; cin>>n; while(n--) { int a,sum=0; cin>>a; for(int i=1;i<=a;i++) sum+=(i*(i+1)*(i+2))/2; cout<<j++<<" "<<a<<" "<<sum<<endl; } return 0; } #include<iostream>//最優程序 using namespace std; const int M=310; int W[M]; int main() { for(int i=1;i!=M;i++) W[i]=W[i-1]+i*(i+1)*(i+2)/2; int m,n; cin>>n; for(int i=1;i<=n;i++) { cin>>m; cout<<i<<" "<<m<<" "<<W[m]<<endl; } }
125、
盜夢空間
- 描述
-
《盜夢空間》是一部精彩的影片,在這部電影里,Cobb等人可以進入夢境之中,夢境里的時間會比現實中的時間過得快得多,這里假設現實中的3分鍾,在夢里就是1小時。
然而,Cobb他們利用強效鎮靜劑,可以從第一層夢境進入第二層夢境,甚至進入三層,四層夢境,每層夢境都會產生同樣的時間加速效果。那么現在給你Cobb在各層夢境中經歷的時間,你能算出現實世界過了多長時間嗎?
比如,Cobb先在第一層夢境待了1個小時,又在第二層夢境里待了1天,之后,返回第一層夢境之后立刻返回了現實。
那么在現實世界里,其實過了396秒(6.6分鍾)
- 輸入
-
第一行輸入一個整數T(0<=T<=100),表示測試數據的組數。
每組測試數據的第一行是一個數字M(3<=M<=100)
隨 后的M行每行的開頭是一個字符串,該字符串如果是"IN" 則Cobb向更深層的夢境出發了,如果是字符串"OUT"則表示Cobb從深層的夢回到了上一層。如果是首字符串是"STAY"則表示Cobb在該層夢境 中停留了一段時間,本行隨后將是一個整數S表示在該層停留了S分鍾(1<=S<=10000000)。數據保證在現實世界中,時間過了整數 秒。 - 輸出
- 對於每組測試數據,輸出現實世界過的時間(以秒為單位)。
- 樣例輸入
-
1 6 IN STAY 60 IN STAY 1440 OUT OUT
- 樣例輸出
-
396
- 來源
- 通信興趣小組選撥賽
- 上傳者
- admin
#include <iostream> #include <string> using namespace std; int main() { int n, m; cin >> n; while (n--) { double jt = 0, min, tt = 1; cin >> m; while (m--) { string s; cin >> s; if (s == "IN") { tt *= 20; continue; } if (s == "OUT") { tt /= 20; continue; } cin >> min; jt += min / tt; } cout << jt * 60 << endl; } return 0; } #include<iostream>//最優程序 #include<string> using namespace std; int main() { int m; string s; cin>>m; while(m--) { int p=1,n,t=0,tt; cin>>n; while(n--) { cin>>s; if(s=="STAY") { cin>>tt; t+=tt*60/p; } else if(s=="IN") p*=20; else p/=20; } cout<<t<<endl; } }
168、
房間安排
- 描述
-
2010 年上海世界博覽會(Expo2010),是第41屆世界博覽會。於2010年5月1日至10月31日期間,在中國上海市舉行。本次世博會也是由中國舉辦的 首屆世界博覽會。上海世博會以“城市,讓生活更美好”(Better City,Better Life)為主題,將充分探索21世紀城市生活。
這次世博會總投資達450億人民幣,創造了世界博覽會史上的最大規模記錄。吸引200個國家和國際組織參展。預計有7000萬人次的參觀者。
為 了更好地接待在這期間來自世界各地的參觀者,如何合理安排各賓館的住房問題提到了日程。組委會已接到了大量的客戶住宿定單,每張定單的內容包括要住宿的房 間數,開始住宿時間和要住的天數。為了便於整個城市各賓館的管理,組委會希望對這些定單進行安排,目的是用盡可能少的房間來滿足這些定單,以便空出更多的 房間用於安排流動游客。
組委會請求DR.Kong來完成這個任務,對這些定單進行合理安排,使得滿足這些定單要求的房間數最少。
假設:某個定單上的游客一旦被安排到某房間,在他預定住宿的期間內是不換房間的。為了簡化描述,定單上的開始住宿時間為距離現在的第幾天。例如,定單為(10,30,5)表示游客要求使用10個房間,第30天開始連住5天。
- 輸入
-
第一行:T 表示有T組測試數據
每組測試數據第一行:N 表示定單數
每組測試數據接下來有N行,每行有三個整數 A B C 表示房間數,開始住宿時間和天數
1<=T<=100
1<=N<=10000 1<=A<=10 1<=B<=180 1<=c<=10 - 輸出
- 輸出一個整數,為滿足所有定單要求的最少房間數。
- 樣例輸入
-
1 3 3 10 4 4 9 3 3 12 6
- 樣例輸出
-
7
- 來源
- 第三屆河南省程序設計大賽
- 上傳者
- 張雲聰
#include <iostream> using namespace std; int main() { int n; cin>>n; while(n--){ int m,a[200]={0},b,c,d;//計算出每天的最大人數,即房間數,保存 cin>>m; while(m--){ cin>>b>>c>>d; for(int i=c;i<c+d;i++)//是<,第c+d天不住的 a[i]+=b; } int max=a[1]; for(int i=2;i<181;i++) if(a[i]>max) max=a[i]; cout<<max<<endl; } return 0; } #include <stdio.h>//最優程序 #include <string.h> #define MAX 200 int Scan(){ int res=0 , ch; while(!((ch=getchar()) >= '0' && ch <= '9')) if(ch==EOF) return EOF; res=ch-'0'; while((ch=getchar()) >= '0' && ch <= '9') res = res*10 + (ch-'0'); return res; } int main() { int Ncase,d[MAX]; scanf("%d",&Ncase); while(Ncase--){ memset(d,0,sizeof(d)); int max = -1, n, a, b, c; n = Scan(); while(n--){ a = Scan(); b = Scan(); c = Scan(); d[b] += a; d[b+c] -= a; } for (int i = 1 ; i < MAX ; i++){ d[i] = d[i-1] + d[i]; if (max < d[i]){ max = d[i]; } } printf("%d\n",max); } return 0; }
169、
素數
- 描述
-
走進世博園某信息通信館,參觀者將獲得前所未有的尖端互動體驗,一場充滿創想和喜悅的信息通信互動體驗秀將以全新形式呈現,從觀眾踏入展館的第一步起,就將與手持終端密不可分,人類未來夢想的驚喜從參觀者的掌上展開。
在 等候區的夢想花園中,參觀者便開始了他們奇妙的體驗之旅,等待中的游客可利用手機等終端參與互動小游戲,與夢想劇場內的虛擬人物Kr. Kong 進行猜數比賽。當屏幕出現一個整數X時,若你能比Kr. Kong更快的發出最接近它的素數答案,你將會獲得一個意想不到的禮物。
例如:當屏幕出現22時,你的回答應是23;當屏幕出現8時,你的回答應是7;若X本身是素數,則回答X;若最接近X的素數有兩個時,則回答大於它的素數。
- 輸入
-
第一行:N 要競猜的整數個數
接下來有N行,每行有一個正整數X
1<=N<=5 1<=X<=1000 - 輸出
- 輸出有N行,每行是對應X的最接近它的素數
- 樣例輸入
-
4 22 5 18 8
- 樣例輸出
-
23 5 19 7
- 來源
- 第三屆河南省程序設計大賽
- 上傳者
- 張雲聰
#include<stdio.h> int main() { int a[1010]= {1,1}; int i,j; for(i=2; i<32; i++) { if(a[i]==0) for(j=i*i; j<1010; j+=i) a[j]=1; } int N; scanf("%d",&N); while(N--) { int n; scanf("%d",&n); for(i=n; a[i]==1; i--); for(j=n; a[j]==1; j++); if((j-n)<=(n-i)) printf("%d\n",j); else printf("%d\n",i); } } #include<iostream>//最優程序 #include<algorithm> using namespace std; int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181, 191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409, 419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647, 653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911, 919,929,937,941,947,953,967,971,977,983,991,997,1009}; int main() { int n,m; cin>>n; while(n--) { cin>>m; if(m==1) {cout<<2<<endl;continue;} int* l=lower_bound(prime,prime+169,m); if(*l-m<=m-*(l-1)) cout<<*l<<endl; else cout<<*(l-1)<<endl; } }
198、
數數
- 描述
-
我們平時數數都是喜歡從左向右數的,但是我們的小白同學最近聽說德國人數數和我們有些不同,他們正好和我們相反,是從右向左數的。因此當他看到123時會說“321”。
現在有一位德國來的教授在鄭州大學進行關於ACM的講座。現在他聘請你來擔任他的助理,他給你一些資料讓你找到這些資料在書中的頁數。現在你已經找到了對應的頁碼,要用英文把頁碼告訴他。
為了簡化我們的問題,你只需要返回單詞的大寫的首字母。(數字0讀成字母O)
注意:每個數字式單獨讀取的,因此不會出現11讀成double one的情況。
#include<iostream> #include<string> using namespace std; int main ( ) { int n; cin >> n; while ( n-- ) { string s; cin>>s; for ( int i = s.size()-1; i >=0; i-- ) { switch ( s[i] ) { case '0': case '1': cout << "O"; break; case '2': case '3': cout << "T"; break; case '4': case '5': cout << "F"; break; case '6': case '7': cout << "S"; break; case '8': cout << "E"; break; case '9': cout << "N"; break; } } cout << endl; } //system ( "PAUSE" ); return 0; } #include<cstdio>//最優程序 char str[]="OOTTFFSSENT"; void show(int t) { if(t){putchar(*(str+t%10));show(t/10);} } int main() { int n,t; scanf("%d",&t); while(t--) { scanf("%d",&n); show(n);puts(""); } }
199、
無線網絡覆蓋
- 描述
-
我們的樂樂同學對於網絡可算得上是情有獨鍾,他有一個計划,那就是用無線網覆蓋鄭州大學。
現 在學校給了他一個機會,因此他要購買很多的無線路由。現在他正在部署某條大道的網絡,而學校只允許把他的無線路由器放在路的正中間。我們默認這條大道是筆 直的並且它在任何地方的寬度都一樣。並且所有的路由器的覆蓋面積是相同的。現在樂樂計算出這條大道的長和寬,以及路由器的覆蓋半徑,想請你幫忙,幫他計算 出他最少要購買的路由器的數量。
注意:為了防止某種干擾,兩台無線路由之間的最小距離不能小於1米
圖1中為一條矩形的道路,中間的虛線代表中線。圖2為最小覆蓋的示意圖。
/* * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=199 * by jtahstu on 2015/2/11 16:00 */ #include <iostream> #include <cmath> using namespace std; int main() { int r, d, l, n; cin >> n; while (n--) { cin >> l >> d >> r; if (4 * r * r - d * d < 1) { cout << "impossible" << endl; } else { double a = sqrt((2 * r) * (2 * r) * 1.0 - (d * d)); if ((int) (l / a) == l / a) cout << (int) (l / a) << endl; else cout << (int) (l / a) + 1 << endl; } } return 0; } #include<stdio.h>//最優程序 #include<math.h> int main(){int T;double L,D,R,a; scanf("%d",&T);while(T--) {scanf("%lf%lf%lf",&L,&D,&R); a=4*R*R-D*D;if(a>0)printf("%.0lf\n",ceil(L/sqrt(a))); else puts("impossible");}}
204、
Coin Test
- 描述
-
As is known to all,if you throw a coin up and let it droped on the desk there are usually three results. Yes,just believe what I say ~it can be the right side or the other side or standing on the desk, If you don't believe this,just try In the past there were some famous mathematicians working on this .They repeat the throwing job once again. But jacmy is a lazy boy.He is busy with dating or playing games.He have no time to throw a single coin for 100000 times. Here comes his idea,He just go bank and exchange thousands of dollars into coins and then throw then on the desk only once. The only job left for him is to count the number of coins with three conditions.
He will show you the coins on the desk to you one by one. Please tell him the possiblility of the coin on the right side as a fractional number if the possiblity between the result and 0.5 is no larger than 0.003. BE CAREFUL that even 1/2,50/100,33/66 are equal only 1/2 is accepted ! if the difference between the result and 0.5 is larger than 0.003,Please tell him "Fail".Or if you see one coin standing on the desk,just say "Bingo" any way.
- 輸入
-
Three will be two line as input.
The first line is a number N(1<N<65536)
telling you the number of coins on the desk.
The second line is the result with N litters.The letter are "U","D",or "S","U" means the coin is on the right side. "D" means the coin is on the other side ."S" means standing on the desk. - 輸出
- If test successeded,just output the possibility of the coin on the right side.If the test failed please output "Fail",If there is one or more"S",please output "Bingo"
- 樣例輸入
-
6 UUUDDD
- 樣例輸出
-
1/2
- 來源
- 鄭州大學校賽題目
- 上傳者
- 張雲聰
#include<stdio.h> int divisor( int n, int m ) { if( n % m == 0 ) return m; else divisor( m, n % m ); } int main( ) { int n, n1 = 0, n2 = 0, m1, m2; char ch[65537]; bool stand = false; scanf("%d", &n ); scanf("%s", ch ); for( int i = 0 ; i < n ; i++ ) { if( ch[i] == 'U' ) n1++; else if( ch[i] == 'D' ) n2++; else if( ch[i] == 'S' ) stand = true; } if( stand ) printf("Bingo\n"); else if( (1.0*n1/n) > (0.5+0.003) || (1.0*n1/n) < (0.5-0.003) ) printf("Fail\n"); else printf("%d/%d\n", n1/divisor( n, n1 ), n/divisor( n, n1 ) ); return 0; } #include<cstdio>//最優程序 int u,d; int gcd(int a,int b) { if(a==0) return b; else return gcd(b%a,a); } int main() { int n; char c; scanf("%d",&n); getchar(); for(int i=0;i!=n;i++) { c=getchar(); if(c=='S') {puts("Bingo");return 0;} if(c == 'U') ++u; else ++d; } int g=gcd(u,u+d); if((double)u/(u+d)-0.5>0.003 ||(double)u/(u+d)-0.5<-0.003) puts("Fail"); else printf("%d/%d",u/g,(u+d)/g); }
206、
矩形的個數
- 描述
-
在一個3*2的矩形中,可以找到6個1*1的矩形,4個2*1的矩形3個1*2的矩形,2個2*2的矩形,2個3*1的矩形和1個3*2的矩形,總共18個矩形。
給出A,B,計算可以從中找到多少個矩形。
- 輸入
-
本題有多組輸入數據(<10000),你必須處理到EOF為止
輸入2個整數A,B(1<=A,B<=1000)
- 輸出
- 輸出找到的矩形數。
- 樣例輸入
-
1 2 3 2
- 樣例輸出
-
3 18
- 來源
- FOJ月賽-2007年3月
- 上傳者
- ACM_趙銘浩
#include "stdio.h" int main(int argc, char const *argv[]) { int a,b; while(scanf("%d%d",&a,&b)!=EOF) { printf("%.0lf\n",(double)a*(a+1)*b*(b+1)/4); } return 0; } #include <stdio.h>//最優程序 main(){float a,b;while(scanf("%f%f",&a,&b)+1)printf("%.0f\n",(a+1)*a*(b+1)*b/4);}
216、
A problem is easy
- 描述
-
When Teddy was a child , he was always thinking about some simple math problems ,such as “What it’s 1 cup of water plus 1 pile of dough ..” , “100 yuan buy 100 pig” .etc..
One day Teddy met a old man in his dream , in that dream the man whose name was“RuLai” gave Teddy a problem :
Given an N , can you calculate how many ways to write N as i * j + i + j (0 < i <= j) ?
Teddy found the answer when N was less than 10…but if N get bigger , he found it was too difficult for him to solve.
Well , you clever ACMers ,could you help little Teddy to solve this problem and let him have a good dream ?- 輸入
- The first line contain a T(T <= 2000) . followed by T lines ,each line contain an integer N (0<=N <= 10^11).
- 輸出
- For each case, output the number of ways in one line
- 樣例輸入
-
2 1 3
- 樣例輸出
-
0 1
- 上傳者
- 苗棟棟
#include<iostream> using namespace std; int main() { int n; cin>>n; while(n--) { int a,count=0; cin>>a; for(int i=1; (i+1)*(i+1)<=(a+1); i++) if((a+1)%(i+1)==0) count++; cout<<count<<endl; } return 0; } #include<cstring> #include<cstdio> #include<map> #include<string> #include<algorithm> #include<vector> #include<iostream> #include<cmath> using namespace std; #define CLR(arr,val) memset(arr,val,sizeof(arr)) int main() { int t,n,cnt=0; //long long num; int num; scanf("%d",&t); while(t--) { // scanf("%lld",&num); scanf("%d",&num); int nn=(int)(sqrt(num+1.0)+0.5); num++; cnt=0; for(int i=2;i<=nn;i++) if(num%i==0) cnt++; printf("%d\n",cnt); } }
217、
a letter and a number
- 描述
-
we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, ... f(Z) = 26, f(z) = -26;
Give you a letter x and a number y , you should output the result of y+f(x).- 輸入
- On the first line, contains a number T(0<T<=10000).then T lines follow, each line is a case.each case contains a letter x and a number y(0<=y<1000).
- 輸出
- for each case, you should the result of y+f(x) on a line
- 樣例輸入
-
6 R 1 P 2 G 3 r 1 p 2 g 3
- 樣例輸出
-
19 18 10 -17 -14 -4
- 上傳者
- 苗棟棟
#include "stdio.h" int main(int argc, char const *argv[]) { int n; //freopen("input.txt","r",stdin); scanf("%d",&n); while(n--) { char a; int b,c,d; char ch=getchar(); scanf("%c%d",&a,&b); c=a; if(a>='A'&&a<='Z') d=c-64; if(a>='a'&&a<='z') d=96-c; printf("%d\n",b+d); } return 0; } #include<stdio.h>//最優程序 main() { int x,n; scanf("%d",&x); while(x--) { getchar(); char c; scanf("%c %d",&c,&n); if(c>=65&&c<=90) c=c-64; else c=0-(c-96); n=c+n; printf("%d\n",n); } }
241、
字母統計
- 描述
- 現在給你一個由小寫字母組成字符串,要你找出字符串中出現次數最多的字母,如果出現次數最多字母有多個那么輸出最小的那個。
#include <stdio.h> #include "string.h" #include <iostream> using namespace std; int main(int argc, char const *argv[]) { int n,i,len; char a[1011]; cin>>n; getchar(); while(n--) { char b[1011]={0}; int fla=0,max=0; cin>>a; len=strlen(a); for(i=0; i<len; i++) { b[a[i]-'a']++; } max=b[0]; for(i=0; i<26; i++) { if(b[i]>max)//||(b[i]==max&&a[i]-'a'<a[fla]-'a') { max=b[i]; fla=i; } } cout<<(char)(fla+'a')<<endl; } return 0; } #include<stdio.h>//最優程序 #include<string.h> main() { int x,i,max,q; char a[1011]; scanf("%d",&x); getchar(); while(x--) { int s[26]={0}; gets(a); for(i=strlen(a)-1;i>=0;i--) s[a[i]-97]++; max=0; for(i=0;i<26;i++) if(max<s[i]) max=s[i],q=i; printf("%c\n",q+97); } }
242、
計算球體積
- 描述
- 根據輸入的半徑值,計算球的體積。
#include<stdio.h> int main() { double n,pi=3.1415926; while(scanf("%lf",&n)==1) { printf("%.0lf\n",4*pi*n*n*n/3); } return 0; }
243、
交換輸出
- 描述
- 輸入n(n<100)個數,找出其中最小的數,將它與最前面的數交換后輸出這些數。(如果這個第一個數就是最小的數,則保持原樣輸出,如果最小的數有相同的按照前面的交換)
#include<stdio.h> #include<iostream> using namespace std; int main() { int n, i, a[100], t, j; while (cin >> n && n) { j = 0; for (i = 0; i < n; i++) cin >> a[i]; t = a[0]; for (i = 0; i < n; i++) { if (a[0] > a[i]) { a[0] = a[i]; j = i; } } a[j] = t; for (i = 0; i < n; i++) printf("%d ", a[i]); printf("\n"); } return 0; } #include<iostream>//最優程序 #include<iterator> #include<algorithm> using namespace std; int data[110]; int main() { int n; while(cin>>n && n) { for(int i=0;i!=n;i++) cin>>data[i]; iter_swap(data,min_element(data,data+n)); copy(data,data+n,ostream_iterator<int>(cout," ")); cout<<endl; } }
244、
16進制的簡單運算
- 描述
- 現在給你一個16進制的加減法的表達式,要求用8進制輸出表達式的結果。
#include"stdio.h" int main() { int n, a, b; while (scanf("%d", &n) != EOF) { while (n--) { scanf("%x %x", &a, &b); printf("%o\n", a + b); } } return 0; } #include<stdio.h>//最優程序 int main() { int T; scanf("%d",&T); while(T--) { int a,b,d; char c; scanf("%x%c%x",&a,&c,&b); if(c=='+') d=a+b; else d=a-b; if(d>=0) printf("%o\n",d); else printf("-%o\n",-d); } }
255、
C小加 之 隨機數
- 描述
- ACM 隊的“C小加”同學想在學校中請一些同學一起做一項問卷調查,為了實驗的客觀性,他先用計算機生成了N個1到1000之間的隨機整數 (0<N≤100),對於其中重復的數字,只保留一個,把其余相同的數去掉,不同的數對應着不同的學生的學號。然后再把這些數從小到大排序,按照排 好的順序去找同學做調查。請你協助 C小加 完成“去重”與“排序”的工作。
#include<iostream> #include<algorithm> using namespace std; int main(int argc, char const *argv[]) { int n; cin>>n; while(n--) { int m,i,a[110],count; count=0; cin>>m; for(i=0; i<m; i++) cin>>a[i]; sort(a,a+m); for(i=0; i<m; i++) { if(a[i]!=a[i+1]) count++; else if(a[i]==a[i+1]) a[i]=-1; } cout<<count<<endl; for(i=0; i<m; i++) { if(a[i]!=-1) cout<<a[i]<<" "; } cout<<endl; } return 0; } #include<iostream>//最優程序 #include<map> #include<cstdio> #include<algorithm> #include<iterator> using namespace std; const int maxn=110; int tab[maxn]; int main() { int t;cin>>t; while(t--){ int n;cin>>n; for(int i=0;i<n;i++)scanf("%d",&tab[i]); sort(tab,tab+n); cout<<(n=distance(tab,unique(tab,tab+n)))<<endl; copy(tab,tab+n,ostream_iterator<int>(cout," "));cout<<endl; } }
259、
茵茵的第一課
- 描述
-
茵茵今年已經六年級了,爸爸給她報了一個學習程序設計的班。
第一節課上,老師講的就是如何輸入一個數,再原樣輸出出來。
以現在的你看來,挺容易的是不?
那么,就請你也寫出一個一樣的程序吧- 輸入
-
第一行是一個整數N(N<10)表示測試數據的組數)
接下來的n行 每行只有一個數(可能是小數,也可能是整數)
這個數的位數(整數位數+小數位數)不超過19位 - 輸出
- 原樣輸出每個數,每輸出占一行
- 樣例輸入
-
2 3.5 5
- 樣例輸出
-
3.5 5
- 來源
- 2008年小學生程序設計友誼賽試題
- 上傳者
- ACM_趙銘浩
#include<stdio.h> #include<string.h> int main() { int n; char a[20]; scanf("%d",&n); while(n--) { scanf("%s",a); printf("%s\n",a); } return 0; }
260、
數數小木塊
- 描述
-
在牆角堆放着一堆完全相同的正方體小木塊,如下圖所示:
因為木塊堆得實在是太有規律了,你只要知道它的層數就可以計算所有木塊的數量了。
現在請你寫個程序 給你任一堆木塊的層數,求出這堆木塊的數量.
- 輸入
-
第一行是一個整數N(N<=10)表示測試數據的組數)
接下來的n行 每行只有一個整數 ,表示這堆小木塊的層數, - 輸出
- 對應每個輸入的層數有一個輸出,表示這堆小木塊的總數量,每個輸出占一行
- 樣例輸入
-
215
- 樣例輸出
-
135
- 來源
- 2008年小學生程序設計友誼賽試題
- 上傳者
- ACM_趙銘浩
#include "iostream" using namespace std; int main() { int n,i,a,b[10001],sum; cin>>n; b[0]=0; for(i=1;i<=10001;i++) {b[i]=b[i-1]+i;} while(n--) { sum=0; cin>>a; for(i=1;i<=a;i++) {sum+=b[i];} cout<<sum<<endl; } return 0; } #include <stdio.h>//最優程序 int main() { int n,t; scanf("%d",&t); while(t--) { scanf("%d",&n); printf("%d\n",n*(n+1)*(n+2)/6); } }
263、
精 挑 細 選
- 描述
-
小王是公司的倉庫管理員,一天,他接到了這樣一個任務:從倉庫中找出一根鋼管。這聽起來不算什么,但是這根鋼管的要求可真是讓他犯難了,要求如下:
1、 這根鋼管一定要是倉庫中最長的;
2、 這根鋼管一定要是最長的鋼管中最細的;
3、 這根鋼管一定要是符合前兩條的鋼管中編碼最大的(每根鋼管都有一個互不相同的編碼,越大表示生產日期越近)。
相關的資料到是有,可是,手工從幾百份鋼管材料中選出符合要求的那根……
要不,還是請你編寫個程序來幫他解決這個問題吧。- 輸入
-
第一行是一個整數N(N<=10)表示測試數據的組數)
每組測試數據的第一行 有一個整數m(m<=1000),表示倉庫中所有鋼管的數量,
之后m行,每行三個整數,分別表示一根鋼管的長度(以毫米為單位)、直徑(以毫米為單位)和編碼(一個9位整數)。 - 輸出
-
對應每組測試數據的輸出只有一個9位整數,表示選出的那根鋼管的編碼,
每個輸出占一行 - 樣例輸入
-
222000 30 1234567892000 20 98765432143000 50 8721984423000 45 7524981242000 60 7651287423000 45 652278122
- 樣例輸出
-
987654321752498124
- 來源
- 2008年小學生程序設計友誼賽試題
- 上傳者
- ACM_趙銘浩
#include<stdio.h> int main() { int n; scanf("%d",&n); while(n--) { int m,a,b,c,a1,b1,c1; scanf("%d",&m); scanf("%d%d%d",&a,&b,&c); for(int i=1; i<m; i++) { scanf("%d%d%d",&a1,&b1,&c1); if(a1>a||(a1==a&&b1<b)||(a1==a&&b1==b&&c1>c)) a=a1,b=b1,c=c1; } printf("%d\n",c); } return 0; } #include<stdio.h>//最優程序 int main() { int n,m,i,a,b,c,x,y,z; scanf("%d",&n); while(n--) { scanf("%d",&m); a=0;b=0;c=0; for(i=0;i<m;i++) { scanf("%d%d%d",&x,&y,&z); if(x>a||x==a&&y<b||x==a&&y==b&&z>c) { a=x;b=y;c=z; } } printf("%d\n",c); } return 0; }
264、
國王的魔鏡
- 描述
-
國王有一個魔鏡,可以把任何接觸鏡面的東西變成原來的兩倍——只是,因為是鏡子嘛,增加的那部分是反的。
比如一條項鏈,我們用AB來表示,不同的字母表示不同顏色的珍珠。如果把B端接觸鏡面的話,魔鏡會把這條項鏈變為ABBA。如果再用一端接觸的話,則會變成ABBAABBA(假定國王只用項鏈的某一端接觸魔鏡)。
給定最終的項鏈,請編寫程序輸出國王沒使用魔鏡之前,最初的項鏈可能的最小長度。
- 輸入
-
第一行是一個整數N(N<=10)表示測試數據的組數)
每組測試數據占一行 只有一個字符串(長度小於100),由大寫英文字母組成,表示最終的項鏈。 - 輸出
- 每組測試數據的輸出只有一個整數,表示國王沒使用魔鏡前,最初的項鏈可能的最小長度。
- 樣例輸入
-
2ABBAABBAA
- 樣例輸出
-
21
- 來源
- 2008年小學生程序設計友誼賽試題
- 上傳者
- ACM_趙銘浩
#include<cstdio> #include<cstring> #include<iostream> using namespace std; int main() { int temp,k,i; char s[100]; scanf("%d",&temp); getchar(); while(temp--) { gets(s); k=strlen(s); while(k) { for(i=0; i<k; ++i) if(s[i]!=s[k-1-i]||k==1||k%2) { cout<<k<<endl; k=0; } k=k/2; } } } #include<stdio.h>//最優程序 #include<string.h> main() { int z,x,q,i; char a[101],t[51]; scanf("%d",&z); getchar(); while(z--) { gets(a); do { x=strlen(a); if(x%2) break; for(i=0;i<x/2;i++) t[i]=a[x-1-i]; t[i]='\0'; a[x/2]='\0'; q=strcmp(a,t); }while(q==0); printf("%d\n",x); } }
266、
字符串逆序輸出
- 描述
-
給定一行字符,逆序輸出此行(空格.數字不輸出)
- 輸入
-
第一行是一個整數N(N<10)表示測試數據的組數)
每組測試數據占一行,每行數據中間有且只有一個空格(這樣你可以把此行當成兩個字符串讀取)。
每行字符長度不超過40
並且保證輸入的字符只有空格(1個),數字,小寫字母三種 - 輸出
- 對應每行測試數據,逆序輸出(空格和數字不輸出)
- 樣例輸入
-
3 abc 123de abc 123 abc d
- 樣例輸出
-
edcba cba dcba
- 來源
- [521521]原創
- 上傳者
- ACM_趙銘浩
#include <stdio.h> #include <string.h> int main(void) { int n,k,i; char a[100]; //freopen("input.txt", "r", stdin); scanf("%d",&n); getchar(); while(n--) { gets(a); k=strlen(a); for(i=k-1; i>=0; i--) if(a[i]>='a'&&a[i]<='z') printf("%c",a[i]); printf("\n"); } return 0; } #include <stdio.h> void revers() { char c; if((c = getchar()) != '\n') revers(); if(c != '\n'&&c>='a'&&c<='z') putchar(c); } int main() { int a; scanf("%d\n",&a); while(a--) { revers(); printf("\n"); } return 0; }
268、
荷蘭國旗問題
- 描述
-
荷蘭國旗有三橫條塊構成,自上到下的三條塊顏色依次為紅、白、藍。現有若干由紅、白、藍三種顏色的條塊序列,要將它們重新排列使所有相同顏色的條塊在一起。本問題要求將所有紅色的條塊放最左邊、所有白色的條塊放中間、所有藍色的條塊放最右邊。
#include "iostream" #include "cstdio" #include "cstring" using namespace std; int main(int argc, char const *argv[]) { int n,i; char a[1001]; cin>>n; getchar(); while(n--) { int x=0,y=0,z=0,m=0; //把getchar();放這里了,我去 gets(a); m=strlen(a); for(i=0;i<m;i++) { if(a[i]=='R')x++; if(a[i]=='W')y++; if(a[i]=='B')z++; } for (i=0;i<x;i++) cout<<'R'; for(i=0;i<y;i++) cout<<'W'; for(i=0;i<z;i++) cout<<'B'; cout<<endl; } return 0; } #include<stdio.h>//最優程序 int main() { int n; scanf("%d",&n); getchar(); while(n--) { int w=0,b=0; char c; while((c=getchar())!=10) c=='R'?printf("R"):(c=='W'?w++:b++); while(w--) putchar('W'); while(b--) putchar('B'); printf("\n"); } }
273、
字母小游戲
- 描述
-
給你一個亂序的字符串,里面包含有小寫字母(a--z)以及一些特殊符號,請你找出所給字符串里面所有的小寫字母的個數, 拿這個數對26取余,輸出取余后的數字在子母表中對應的小寫字母(0對應z,1對應a,2對應b....25對應y)。
- 輸入
- 第一行是一個整數n(1<n<1000)表示接下來有n行的字符串m(1<m<200)需要輸入
- 輸出
- 輸出對應的小寫字母 每個小寫字母單獨占一行
- 樣例輸入
-
2 asdasl+%$^&ksdhkjhjksd adklf&(%^(alkha
- 樣例輸出
-
q j
- 來源
- [zinber]原創
- 上傳者
- zinber
#include "stdio.h" #include "string.h" int main(int argc, char const *argv[]) { int n; char a; //freopen("input.txt","r",stdin); scanf("%d",&n); getchar(); while(n--) { int number=0; while(scanf("%c",&a),a!='\n') { if (a>='a'&&a<='z') number++; } if (number%26==0) printf("z\n"); else printf("%c\n",'a'+number%26-1); } return 0; } #include <cstdio>//最優程序 #include <cctype> #include <cstring> int main(){ int n,i; char arr[201]; scanf("%d",&n); while(n--){ scanf("%s",arr); int l=strlen(arr),r=0; for(int i=0;i!=l;i++) if(islower(arr[i])) r++; r%=26; printf("%c\n",r==0?'z':96+r); } }
274、
正三角形的外接圓面積
- 描述
-
給你正三角形的邊長,pi=3.1415926 ,求正三角形的外接圓面積。
- 輸入
- 只有一組測試數據 第一行輸入一個整數n(1<n<1000)表示接下來要輸入n個邊長m(1.0<=m<1000.0)
- 輸出
- 輸出每個正三角形的外接圓面積,保留兩位小數,每個面積單獨占一行。
- 樣例輸入
-
5 1 13 22 62 155
- 樣例輸出
-
1.05 176.98 506.84 4025.43 25158.92
- 來源
- [zinber]原創
- 上傳者
- zinber
#include<stdio.h> #define pi 3.1415926 int main() { double a,s=0; int n; scanf("%d",&n); while(n--) { scanf("%lf",&a); s=a*a*pi/3; printf("%.2lf\n",s); } return 0; } #include<stdio.h>//最優程序 int main() { int n; double m,pi=3.1415926; scanf("%d",&n); while(n--) { scanf("%lf",&m); printf("%.2lf\n",pi*m*m/3); } return 0; }
275、
隊花的煩惱一
- 描述
-
ACM隊的隊花C小+經常抱怨:“C語言中的格式輸出中有十六、十、八進制輸出,然而卻沒有二進制輸出,哎,真遺憾!誰能幫我寫一個程序實現輸入一個十進制數n,輸出它的二進制數呀?”
難道你不想幫幫她嗎?^_^
#include "iostream"//十進制轉換成二進制 using namespace std; void dec_bin(const int x) { if (x/2>0) { dec_bin(x/2); cout<<x%2; } else cout<<x; } int main(int argc, char const *argv[]) { int data=0; while(cin>>data) { dec_bin(data); cout<<endl; } return 0; }
276、
比較字母大小
- 描述
-
任意給出兩個英文字母,比較它們的大小,規定26個英文字母A,B,C.....Z依次從大到小。
#include<stdio.h> int main() { int i; char a, b; scanf("%d", &i); while (i--) { getchar(); scanf("%c %c", &a, &b); if (a > b) printf("%c<%c\n", a, b); else if (a == b) printf("%c=%c\n", a, b); else if (a < b) printf("%c>%c\n", a, b); } return 0; }
277、
車牌號
- 描述
- 茵茵很喜歡研究車牌號碼,從車牌號碼上可以看出號碼注冊的早晚,據研究發現,車牌號碼是按字典序發放的,現在她收集了很多車牌號碼,請你設計程序幫她判斷注冊較早的號碼。車牌號碼由5個字母或數字組成
#include "iostream" #include "string.h" #include "cstdio" using namespace std; int main(int argc, char const *argv[]) { int n; cin>>n; while(n--) { int m,i; char a[6]="zzzzz",str[6]; cin>>m; getchar(); for(i=0; i<m; i++) { scanf("%s",str); if(strcmp(a,str)>0) strcpy(a,str); } printf("%s\n",a); } return 0; } #include<stdio.h>//最優程序 #include<string.h> main(){int n;scanf("%d",&n);while(n--) {int m;char a[6],c[6];scanf("%d",&m);getchar();gets(c); while(--m){gets(a);if(strcmp(a,c)<0)strcpy(c,a);}puts(c);}}
283、
對稱排序
- 描述
-
In your job at Albatross Circus Management (yes, it's run by a bunch of clowns), you have just finished writing a program whose output is a list of names in nondescending order by length (so that each name is at least as long as the one preceding it). However, your boss does not like the way the output looks, and instead wants the output to appear more symmetric, with the shorter strings at the top and bottom and the longer strings in the middle. His rule is that each pair of names belongs on opposite ends of the list, and the first name in the pair is always in the top part of the list. In the first example set below, Bo and Pat are the first pair, Jean and Kevin the second pair, etc.
- 輸入
- The input consists of one or more sets of strings, followed by a final line containing only the value 0. Each set starts with a line containing an integer, n, which is the number of strings in the set, followed by n strings, one per line, NOT SORTED. None of the strings contain spaces. There is at least one and no more than 15 strings per set. Each string is at most 25 characters long.
- 輸出
-
For each input set print "SET n" on a line, where n starts at 1, followed by the output set as shown in the sample output.
If length of two strings is equal,arrange them as the original order.(HINT: StableSort recommanded) - 樣例輸入
-
7 Bo Pat Jean Kevin Claude William Marybeth 6 Jim Ben Zoe Joey Frederick Annabelle 5 John Bill Fran Stan Cece 0
- 樣例輸出
-
SET 1 Bo Jean Claude Marybeth William Kevin Pat SET 2 Jim Zoe Frederick Annabelle Joey Ben SET 3 John Fran Cece Stan Bill
- 來源
- POJ
- 上傳者
- sadsad
#include<string> #include<iostream> #include<algorithm> using namespace std; bool cmp(string a, string b) { return a.size() < b.size(); } int main() { int n; int count=0; string a[10001]; while(cin>>n) { if(n==0)break; for(int i=0;i<n;i++) { cin>>a[i]; } sort(a,a+n,cmp);//sort cout<<"SET "<<++count<<endl; for(int i=0;i<n;i+=2) cout<<a[i]<<endl; if(n%2==0) { for(int i=n-1;i>0;i-=2) cout<<a[i]<<endl; } else for(int i=n-2;i>0;i-=2) cout<<a[i]<<endl; } return 0; } #include<stdio.h>//最優程序 #include<string.h> main() { int x,i,j,q=0; char s[16][26],t[26],z[16][26]; while(1) { scanf("%d",&x); if(x==0) break; q++; getchar(); for(i=0;i<x;i++) gets(s[i]); for(i=1;i<x;i++) for(j=0;j<x-i;j++) if(strlen(s[j])>strlen(s[j+1])) {strcpy(t,s[j]);strcpy(s[j],s[j+1]);strcpy(s[j+1],t);} for(i=0,j=0;i<x;i++,j++) { strcpy(z[j],s[i]); if(i!=x-1) strcpy(z[x-j-1],s[++i]); } printf("SET %d\n",q); for(i=0;i<x;i++) puts(z[i]); } }
324、
猴子吃桃問題
- 描述
- 有一堆桃子不知數目,猴子第一天吃掉一半,又多吃了一個,第二天照此方法,吃掉剩下桃子的一半又多一個,天天如此,到第m天早上,猴子發現只剩一只桃子了,問這堆桃子原來有多少個? (m<29)
#include "stdio.h" int f(int m) { if(m==0) return 1; else return 2*(f(m-1)+1); } int main() { int n,m; //freopen("input.txt","r",stdin); scanf("%d",&n); while(n--) { scanf("%d",&m); printf("%d\n",f(m)); } return 0; } #include<stdio.h>//最優程序 int main() { int n,m; scanf("%d",&n); while(n--) { scanf("%d",&m); printf("%d\n",(3<<m)-2); } return 0; }
399、
整除個數
- 描述
- 1、2、3… …n這n(0<n<=1000000000)個數中有多少個數可以被正整數b整除。
#include<stdio.h> int main() { int n,b; while(scanf("%d%d",&n,&b)!=EOF) { int count; count=n/b; printf("%d\n",count); } return 0; } #include <iostream>//最優程序 using namespace std; int main() { int n,r; while(cin>>n>>r) cout<<n/r<<endl; }
455、
黑色帽子
- 描述
-
最近發現了一個搞笑的游戲,不過目前還沒玩過。一個舞會上,每個人頭上都戴着一頂帽子,帽子只有黑白兩種,黑的至少有一頂。每個人都能看到別人帽子的顏 色,可是看不見自己的。主持人先讓大家看看別人頭上戴的是什么帽子,然后關燈,如果有人認為自己戴的的黑色帽子,就打自己一個耳光(
,都很自覺,而且不許打別人的哦),開燈,關燈,開燈……。因為都不想打自己耳光,所以不確定的情況下都不會打自己的,現在有n頂黑色帽子,第幾次關燈才會聽到有人打自己耳光?
#include<stdio.h> int main() { int k; scanf("%d",&k); while(k--) { int n; scanf("%d",&n); printf("%d\n",n); } return 0; } #include<cstdio>//最優程序 main(){char _[15];gets(_);while(gets(_))puts(_);}
457、
大小寫互換
- 描述
- 現在給出了一個只包含大小寫字母的字符串,不含空格和換行,要求把其中的大寫換成小寫,小寫換成大寫,然后輸出互換后的字符串。
#include<stdio.h> #include<string.h> int main() { int n, i, len; char a[100]; scanf("%d", &n); while (n--) { scanf("%s", a); len = strlen(a); for (i = 0; i < len; i++) { if (a[i] <= 'z' && a[i] >= 'a') a[i] = a[i] - 32; else a[i] = a[i] + 32; } printf("%s\n", a); } return 0; } #include<stdio.h>//最優程序 int main() { int a,b,c,n; char x; scanf("%d",&n); getchar(); while(n--) { while(scanf("%c",&x)&&x!='\n') { if(x>=97&&x<=122) printf("%c",x-32); else if(x<=90&&x>=64) printf("%c",x+32); } printf("\n"); } }
458、
小光棍數
- 描述
- 最近Topcoder的XD遇到了一個難題,倘若一個數的三次方的后三位是111,他把這樣的數稱為小光棍數。他已經知道了第一個小光棍數是471,471的三次方是104487111,現在他想知道第m(m<=10000000000)個小光棍數是多少?
#include "iostream" using namespace std; int main(int argc, char const *argv[]) { int n; cin>>n; while(n--) { long long m; cin>>m; cout<<(m-1)*1000+471<<endl; } return 0; } #include<stdio.h>//最優程序 int main() { long long a,b,c,d,e; scanf("%lld",&a); while(a--) { scanf("%lld",&b); printf("%lld\n",(b-1)*1000+471); } return 0; }
463、
九九乘法表
- 描述
-
小時候學過的九九乘法表也許將會扎根於我們一生的記憶,現在讓我們重溫那些溫暖的記憶,請編程輸出九九乘法表.
是那種反過來的三角形啦,具體如下圖:
每兩個式子之前用一個空格 隔開。。。
- 輸入
-
第一有一個整數N,表示有N組數據(N<10)
接下來由N行,每行只有一個整數M(1<=M<=9); - 輸出
-
對應每個整數M,根據要求輸出乘法表的前N行,具體格式參見輸入輸出樣例和上圖.
每兩組測試數據結果之間有一個空行隔開,具體如輸出樣例。 - 樣例輸入
-
3 2 1 5
- 樣例輸出
-
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45
- 來源
- 原創
- 上傳者
- ACM_趙銘浩
#include "iostream" using namespace std; int main(int argc, char const *argv[]) { int n,i,j,m; cin>>n; while(n--) { cin>>m; for(i=1; i<=m; i++) { for(j=i; j<=9; j++) cout<<i<<'*'<<j<<'='<<i*j<<' '; cout<<endl; } } return 0; } #include<stdio.h>//最優程序 int main() { int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<=n;i++){ for(int j=i;j<10;j++) printf("%d*%d=%d ",i,j,i*j); printf("\n"); } } }
477、
A+B Problem III
- 描述
-
求A+B是否與C相等。
- 輸入
-
T組測試數據。
每組數據中有三個實數A,B,C(-10000.0<=A,B<=10000.0,-20000.0<=C<=20000.0)
數據保證小數點后不超過4位。
- 輸出
-
如果相等則輸出Yes
不相等則輸出No - 樣例輸入
-
3 -11.1 +11.1 0 11 -11.25 -0.25 1 2 +4
- 樣例輸出
-
Yes Yes No
- 上傳者
- 李文鑫
#include "iostream" using namespace std; int main(int argc, char const *argv[]) { int n; cin>>n; while(n--) { double a,b,c; cin>>a>>b>>c; if((a+b-c>-0.0001)&&(a+b-c<0.0001)) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; } #include <cstdio>//最優程序 #include <math.h> double a,b,c; main() { for(scanf("%lf",&a);~scanf("%lf%lf%lf",&a,&b,&c);puts(fabs(c-(a+b))<1e-6?"Yes":"No")); }
484、
The Famous Clock
- 描述
-
Mr. B, Mr. G and Mr. M are now in Warsaw, Poland, for the 2012’s ACM-ICPC World Finals Contest. They’ve decided to take a 5 hours training every day before the contest. Also, they plan to start training at 10:00 each day since the World Final Contest will do so. The scenery in Warsaw is so attractive that Mr. B would always like to take a walk outside for a while after breakfast. However, Mr. B have to go back before training starts, otherwise his teammates will be annoyed. Here is a problem: Mr. B does not have a watch. In order to know the exact time, he has bought a new watch in Warsaw, but all the numbers on that watch are represented in Roman Numerals. Mr. B cannot understand such kind of numbers. Can you translate for him?
- 輸入
- Each test case contains a single line indicating a Roman Numerals that to be translated. All the numbers can be found on clocks. That is, each number in the input represents an integer between 1 and 12. Roman Numerals are expressed by strings consisting of uppercase ‘I’, ‘V’ and ‘X’. See the sample input for further information.
- 輸出
- For each test case, display a single line containing a decimal number corresponding to the given Roman Numerals.
- 樣例輸入
-
I II III IV V VI VII VIII IX X XI XII
- 樣例輸出
-
Case 1: 1 Case 2: 2 Case 3: 3 Case 4: 4 Case 5: 5 Case 6: 6 Case 7: 7 Case 8: 8 Case 9: 9 Case 10: 10 Case 11: 11 Case 12: 12
- 來源
- HDU
- 上傳者
- ACM_宋志恆
#include<iostream> #include<string> using namespace std; int main() { string a; int count=1; while(cin>>a) { if(a=="I") { cout<<"Case "<<count<<": 1"<<endl; count++; } if(a=="II") { cout<<"Case "<<count<<": 2"<<endl; count++; } if(a=="III") { cout<<"Case "<<count<<": 3"<<endl; count++; } if(a=="IV") { cout<<"Case "<<count<<": 4"<<endl; count++; } if(a=="V") { cout<<"Case "<<count<<": 5"<<endl; count++; } if(a=="VI") { cout<<"Case "<<count<<": 6"<<endl; count++; } if(a=="VII") { cout<<"Case "<<count<<": 7"<<endl; count++; } if(a=="VIII") { cout<<"Case "<<count<<": 8"<<endl; count++; } if(a=="IX") { cout<<"Case "<<count<<": 9"<<endl; count++; } if(a=="X") { cout<<"Case "<<count<<": 10"<<endl; count++; } if(a=="XI") { cout<<"Case "<<count<<": 11"<<endl; count++; } if(a=="XII") { cout<<"Case "<<count<<": 12"<<endl; count++; } } return 0; } #include<map>//最優程序 #include<iostream> #include<string> using namespace std; map<string,int>m; int main() { m["I"]=1; m["II"]=2; m["III"]=3; m["IV"]=4; m["V"]=5; m["VI"]=6; m["VII"]=7; m["VIII"]=8; m["IX"]=9; m["X"]=10; m["XI"]=11; m["XII"]=12; string s; int c=0; while(cin>>s) cout<<"Case "<<++c<<": "<<m[s]<<endl; return 0; }
596、
誰是最好的Coder
- 描述
-
計科班有很多Coder,帥帥想知道自己是不是綜合實力最強的coder。帥帥喜歡帥,所以他選了帥氣和編程水平作為評選標准。每個同學的綜合得分是帥氣程度得分與編程水平得分的和。他希望你能寫一個程序幫他一下。
- 輸入
-
數據有多組。
輸入一個數n,代表計科班的總人數。
接下來有n行數,一行數有兩個數a,b。
其中a代表該同學的編程水平,b代表該同學的帥氣程度。
n=0表示輸入結束。 - 輸出
- 每組數據占一行,輸出所有同學中綜合得分最高的分數。
- 樣例輸入
-
5 9 10 7 11 1 6 5 7 3 5 2 7 3 7 6 0
- 樣例輸出
-
19 13
- 上傳者
- wsp0214
#include "stdio.h" int main() { int n,max=0,a,b,i; // freopen("input.txt","r",stdin); while(scanf("%d",&n),n) { max=0; for(i=0; i<n; i++) { scanf("%d%d",&a,&b); if((a+b)>max)max=(a+b); } printf("%d\n",max); } return 0; } #include<stdio.h>//最優程序 main(){int n;while(scanf("%d",&n),n){int a,b,s=0;while(n--){scanf("%d%d",&a,&b);if(a+b>s) s=a+b;}printf("%d\n",s);}}
599、
奮斗的小蝸牛
- 描述
-
傳 說中能站在金字塔頂的只有兩種動物,一種是鷹,一種是蝸牛。一只小蝸牛聽了這個傳說后,大受鼓舞,立志要爬上金字塔。為了實現自己的夢想,蝸牛找到了老 鷹,老鷹告訴它金字塔高H米,小蝸牛知道一個白天自己能向上爬10米,但由於晚上要休息,自己會下滑5米。它想知道自己在第幾天能站在金字塔頂,它想讓你 幫他寫個程序幫助它。
- 輸入
-
第一行有一個整數t,表示t組測試數據。
第二行一個整數H(0<H<10^9)代表金字塔的高度。 - 輸出
- 輸出一個整數n表示小蝸牛第n天站在金字塔頂上
- 樣例輸入
-
2 1 5
- 樣例輸出
-
1 1
- 上傳者
- ACM_王孝鋒
#include<stdio.h> int main(void) { int H,i,t; scanf("%d",&t); for(i=1; i<=t; i++) { int day=1,h=0; scanf("%d",&H); while(h+10<H) { h+=5; day++; } printf("%d\n",day); } return 0; }
733、
萬聖節派對
- 描述
-
萬聖節有一個Party,XadillaX顯然也要去湊熱鬧了。因為去湊熱鬧的人數非常龐大,幾十W的數量級吧,自然要進場就需要有門票了。很幸運的,XadillaX竟然拿到了一張真·門票!這真·門票的排列規則有些奇怪:
-
門票號是由0~6組成的六位數(0~6這幾個數字可重用)
-
每一個門票號的每一位不能有三個連續相同的數字(如123335是不行的)
-
每一個門票號相鄰的兩位相差必須在四以下(≤4)(如016245是不行的)
-
#include<stdio.h> #include<math.h> int main() { int n; scanf("%d",&n); while(n--) { int ticket1, ticket2, i, j; int flag1, flag2, flag3; char x[6]; scanf("%d %d",&ticket1, &ticket2); for (i = ticket1; i <= ticket2; i++) { flag1 = 1; flag2 = 1; flag3=1; x[0] = i / 100000 + '0'; x[1] = i /10000 % 10 + '0'; x[2] = i / 1000 % 10 + '0'; x[3] = i / 100 % 10 + '0'; x[4] = i / 10 % 10 + '0'; x[5] = i % 10 + '0'; for (j = 0; j < 6; j++) if (x[j] - '0' > 6) { flag3 = 0; break; } for (j = 0; j < 5; j++) if (fabs(x[j] - x[j+1]) >4) { flag1 = 0; break; } for (j = 0; j < 4; j++) if (x[j] - x[j+1] == 0 && x[j] - x[j+2] == 0) { flag2 = 0; break; } if (flag1 == 1 && flag2 == 1 && flag3 == 1) { for (j = 0; j < 6; j++) printf("%c",x[j]); printf("\n"); } } printf("\n"); } return 0; }
779、
蘭州燒餅
- 描述
-
燒餅有兩面,要做好一個蘭州燒餅,要兩面都弄熱。當然,一次只能弄一個的話,效率就太低了。有這么一個大平底鍋,一次可以同時放入k個蘭州燒餅,一分鍾能做好一面。而現在有n個蘭州燒餅,至少需要多少分鍾才能全部做好呢?
- 輸入
- 依次輸入n和k,中間以空格分隔,其中1 <= k,n <= 100000
- 輸出
- 輸出全部做好至少需要的分鍾數
- 樣例輸入
-
3 2
- 樣例輸出
-
3
- 提示
- 如樣例,三個蘭州燒餅編號a,b,c,首先a和b,然后a和c,最后b和c,3分鍾完成
- 上傳者
- 勿念情
#include<stdio.h> int main() { int n, k; while (scanf("%d%d", &n, &k) != EOF) { if (n < k) printf("2\n"); else { if (2 * n % k == 0) printf("%d\n", 2 * n / k); else printf("%d\n", 2 * n / k + 1); } } return 0; }
811、
變態最大值
- 描述
-
Yougth講課的時候考察了一下求三個數最大值這個問題,沒想到大家掌握的這么爛,幸好在他的幫助下大家算是解決了這個問題,但是問題又來了。
他想在一組數中找一個數,這個數可以不是這組數中的最大的,但是要是相對比較大的,但是滿足這個條件的數太多了,怎么辦呢?他想到了一個辦法,把這一組數從開始把每相鄰三個數分成一組(組數是從1開始),奇數組的求最大值,偶數組的求最小值,然后找出這些值中的最大值。
#include "iostream" using namespace std; int max1(int a,int b,int c)//我去,這里用的max,尼瑪 { int t; if(a>b){t=a;a=b;b=t;} if(a>c){t=a;a=c;c=t;} if(b>c){t=b;b=c;c=t;} return c; } int min1(int x,int y,int z) { int m; if(x>y){m=x;x=y;y=m;} if(x>z){m=x;x=z;z=m;} if(y>z){m=y;y=z;z=m;} return x; } int main(int argc, char const *argv[]) { int n,i,d[10001],e[4000]; while(cin>>n) { int max=0; for(i=1;i<=n;i++) { cin>>d[i]; } for(i=3;i<=n;i+=3) { if((i/3)%2==1) { e[i/3]=max1(d[i-2],d[i-1],d[i]); } if((i/3)%2==0) { e[i/3]=min1(d[i-2],d[i-1],d[i]); } } max=e[1]; for(i=2;i<=n/3;i++) if(e[i]>max) max=e[i]; cout<<max<<endl; } return 0; } #include <cstdio>//最優程序 #define Max(a,b,c) a>(b>c?b:c)?a:(b>c?b:c) #define Min(a,b,c) a>(b>c?c:b)?(b>c?c:b):a int main() { int a[10005]; int n,i,j,h; while(~scanf("%d",&n)) { int max=1<<32; for(i=0;i<n&&scanf("%d",&a[i]);i++){} for(i=0;i<n;i+=3){ h=i%2==0?Max(a[i],a[i+1],a[i+2]):Min(a[i],a[i+1],a[i+2]); max=h>max?h:max; } printf("%d\n",max); } return 0; }
813、
對決
- 描述
-
Topcoder 招進來了 n 個新同學,Yougth計划把這個n個同學分成兩組,要求每組中每個人必須跟另一組中每個同學進行一次算法對決,問存不存在一種分組方式在k場完成對決。(兩組中每一組中人數都要大於0)
#include "stdio.h" int main(int argc, char const *argv[]) { int i,n,k; while(scanf("%d%d",&n,&k),n,k) { int jt=0; for(i=0; i<n; i++) { if(k==i*(n-i)) { printf("YES\n"); jt=1; break; } } if(jt==0) printf("NO\n"); } return 0; } #include <cstdio>//最優程序 int main() { int n,k; while(scanf("%d%d",&n,&k)&&(n||k)) { int ok=0; for(int i=1;i<=n/2;i++) { if(i*(n-i)==k) ok=1;continue; } if(ok) printf("YES\n"); else printf("NO\n"); } return 0; }
822、
畫圖
- 描述
-
計算機畫圖也挺有趣的哈!那我們就來用計算機畫幅圖吧。。。
- 輸入
- 輸入一個正整數N(0<N<=10),表示要輸出的正方形的邊上*的個數
- 輸出
- 輸出一個滿足題意的正方形
- 樣例輸入
-
4
- 樣例輸出
-
**** **** **** ****
- 上傳者
- TC_趙坤垚
#include "stdio.h" int main(int argc, char const *argv[]) { int n,i,j; scanf("%d",&n); for (i=0; i<n; i++) { for(j=0; j<n; j++) { printf("*"); } printf("\n"); } return 0; }
833、
取石子(七)
- 描述
-
Yougth和Hrdv玩一個游戲,拿出n個石子擺成一圈,Yougth和Hrdv分別從其中取石子,誰先取完者勝,每次可以從中取一個或者相鄰兩個,Hrdv先取,輸出勝利着的名字。
#include <stdio.h> int main(void) { int n; while (scanf("%d", &n) != EOF) { if (n < 3) printf("Hrdv\n"); else printf("Yougth\n"); } } #include<cstdio> int n; int main() { while(~scanf("%d",&n)) printf(n>=3?"Yougth\n":"Hrdv\n"); return 0; }
844、
A+B Problem(V)
- 描述
- 做了A+B Problem之后,Yougth感覺太簡單了,於是他想讓你求出兩個數反轉后相加的值。幫幫他吧
#include <stdio.h> /* 整數反轉 */ long long rev(int a) { long long s = 0; while (a) { s = s * 10 + a % 10; a /= 10; } return s; } int main(void) { int m, n; long long M, N; while (1) { scanf("%d%d", &m, &n); if (m == 0 && n == 0) break; printf("%lld\n", rev(m) + rev(n)); } return 0; } #include <iostream>//最優程序 #include <string> using namespace std; int main() { string s,t; int x,y; while(cin>>s>>t) { int x=0,y=0; if(s[0]=='0'&&t[0]=='0') break; for(int i=s.size()-1;i>=0;i--) x=x*10+(s[i]-'0'); for(int i=t.size()-1;i>=0;i--) y=y*10+(t[i]-'0'); cout<<x+y<<endl; } return 0; }
845、
無主之地1
- 描述
- 子 曉最近在玩無主之地1,他對這個游戲的評價不錯,結合了FPS與RPG元素,可玩度很高。不過,他發現了一代的任務系統做的不好,任務系統並沒有幫他統計 清楚哪個區域有多少任務,而且,給任務的時候呢,也比較散亂。比如,在1區域的一個任務點,你領到了4個任務;2區域的一個任務點,你領到了3個任務;游 戲一段時間后,你又在1區域另一個任務點個領到了3任務(之前任務沒有完成),3區域領到了9個任務……他感覺很凌亂,現在他要設計一個程序來統計每個區 域有多少個任務。
#include <stdio.h> int main() { int m,n,i,j; int a[100]= {0}; int c[100]= {0},k=0; while(scanf("%d %d",&m,&n)&&m&&n) { a[m]+=n; c[k++]=m; } for(i=0; i<k-1; i++) { for(j=i+1; j<k; j++) { if(c[i]==c[j]) c[j]=0; } } for(i=0; i<k; i++) { if(c[i]!=0) { printf("%d %d\n",c[i],a[c[i]]); c[i]=0; } } return 0; }
975、
關於521
- 描述
-
Acm隊的流年對數學的研究不是很透徹,但是固執的他還是想一頭扎進去。
瀏 覽網頁的流年忽然看到了網上有人用玫瑰花瓣拼成了521三個數字,頓時覺得好浪漫,因為每個男生都會不經意的成為浪漫的制造者。此后,流年走到哪里都能看 到5、2、1三個數字,他怒了,現在他想知道在連續的數中有多少數全部包含了這三個數字。例如12356就算一個,而5111就不算。特別的,如果他看到 了521三個數連續出現,會特別的憤怒。例如35210。
#include <iostream> #include <string.h> using namespace std; int array[2][1000010]; int main() { int a, b, c; int cnt = 0, cntp = 0; memset(array, 0, sizeof(array)); for (int i = 1; i <= 1000000; i++) { a = b = c = 0; if ((i/100000)%10 == 1 || (i/10000)%10 == 1 || (i/1000)%10 == 1 || (i/100)%10 == 1 || (i/10)%10 == 1 || i%10 == 1) a = 1; if ((i/100000)%10 == 2 || (i/10000)%10 == 2 || (i/1000)%10 == 2 || (i/100)%10 == 2 || (i/10)%10 == 2 || i%10 == 2) b = 1; if ((i/100000)%10 == 5 || (i/10000)%10 == 5 || (i/1000)%10 == 5 || (i/100)%10 == 5 || (i/10)%10 == 5 || i%10 == 5) c = 1; if (a && b && c) ++cnt; if ((i/1000)%1000 == 521 || (i/100)%1000 == 521 || (i/10)%1000 == 521 || (i%1000) == 521) ++cntp; array[0][i] += cnt; array[1][i] += cntp; } int x, y, ca = 0; while (cin >> x >> y) { cout << "Case " << ++ca << ":"; cout << array[0][y] - array[0][x-1] << " " << array[1][y] - array[1][x-1] << endl; } return 0; } #include<stdio.h>//最優程序 int a[2][1000001]={0}; int main() { int k=0,i,sum=0; for(i=1;i<=1000000;i++) { if((i%10==5||(i/10)%10==5||(i/100)%10==5||(i/1000)%10==5||(i/10000)%10==5||(i/100000)%10==5)&&(i%10==2||(i/10)%10==2||(i/100)%10==2||(i/1000)%10==2 ||(i/10000)%10==2||(i/100000)%10==2)&&(i%10==1||(i/10)%10==1||(i/100)%10==1||(i/1000)%10==1||(i/10000)%10==1||(i/100000)%10==1)) {sum++; if(i/1000==521||(i/100)%1000==521||(i/10)%1000==521||i%1000==521)k++;} a[0][i]+=sum; a[1][i]+=k; } int m,n,w=0; while(scanf("%d%d",&n,&m)!=EOF) printf("Case %d:%d %d\n",++w,a[0][m]-a[0][n-1],a[1][m]-a[1][n-1]); }
1071、
不可以!
- 描述
-
判斷:兩個數x、y的正負性。
要求:不可以使用比較運算符,即"<",">","<=",">=","==","!="。
/* * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1071 * by jtahstu on 2015/2/11 15:00 * copy csdn 聽自己心跳的聲音 * http://blog.csdn.net/u013634213/article/details/40055329 */ #include<cstdio> #include<cstring> int main() { int a, b; while (scanf("%d%d", &a, &b) != EOF) { if (!(a * b)) { puts("Signs can't be sure"); continue; } if ((a >> 31) ^ (b >> 31)) puts("Signs are opposite"); else puts("Signs are not opposot"); } return 0; } #include<stdio.h>//最優程序 int main() { int a,b,c; while(~scanf("%d%d",&a,&b)) { c=((a>>31)&1)+((b>>31)&1); if((!a)||(!b))printf("Signs can't be sure\n"); else printf("Signs are %s\n",c&1?"opposite":"not opposot"); } }
1092、
數字分隔(二)
- 描述
-
在一個遙遠的國家,銀行為了更快更好的處理用戶的訂單,決定將一整串的數字按照一定的規則分隔開來,分隔規則如下:
1、實數的整數部分按照每三個數字用逗號分隔開(整數部分的高位有多余的0時,需先將多余的0過濾后,再進行數字分隔,如:0001234567 輸出結果為1,234,567.00)
2、小數部分保留兩位小數(四舍五入)
3、如果該數是負的,則在輸出時需用括號將分隔后的數字括起來,例如:-10005.1645的輸出結果為(10,005.16)
- 輸入
- 多組測試數據,每行輸入一個實數n(n的位數小於100)
- 輸出
- 輸出分隔后的結果
- 樣例輸入
-
00012345670.0000-10005.1645
- 樣例輸出
-
1,234,567.000.00(10,005.16)
- 來源
- calamity_coming
- 上傳者
- ACM_孫毓陽
#include <iostream> #include <cstring> using namespace std; int main() { char s[110],t[110],r[110]; while(cin>>s) { memset(r,'\0',sizeof(r)); memset(t,'\0',sizeof(t)); int i,l,poi=0,dis,car,k=0,f=1,j=0; l=strlen(s); for(i=s[0]=='-'?1:0; i<l; i++)//處理原字符串 { if(s[i]!='0'&&f) { if(s[i]=='.') t[j++]='0'; f=0; } if(s[i]=='0'&&f) continue; else t[j++]=s[i]; } if(!j) { t[j++]='0';f=0;//判斷當原字符串為0時的情況 } for(i=0; i<j; i++) { if(t[i]=='.') { poi=i;//找出小數點的位置 f=1;//如果有小數點,則f置1 break; } } if(f)//當字符串有小數點時 { dis=j-1-poi;//計算小數點到字符串末尾的距離 if(dis==2) for(i=j-1; i>=0; i--)r[k++]=t[i];//如果只有兩位小數,原樣輸出 else if(dis==1)//如果有一位小數,則最后一位補0 { r[k++]='0'; for(i=j-1; i>=0; i--) r[k++]=t[i]; } else//如果距離大於2 { if(t[poi+3]<'5') for(i=poi+2; i>=0; i--)r[k++]=t[i];//如果第三位小數小於5 else//如果第三位小數大於5 { car=1; for(i=poi+2; i>=0; i--) { if(t[i]=='.') { r[k++]=t[i]; continue; } if(car) t[i]+=1; if(t[i]>'9') { t[i]='0'; car=1; } else car=0; r[k++]=t[i]; } if(t[0]=='0') r[k++]='1'; } } } else for(i=j-1; i>=0; i--)r[k++]=t[i];//沒有小數點,原樣輸出 if(s[0]=='-')//如果是負數 { if(!f) { cout<<'(';//負數需要在字符串前后加上括號 for(i=k-1; i>=0; i--) { cout<<r[i]; if(i%3==0&&i) cout<<',';//控制','的位置 } cout<<".00"<<')'<<endl;//沒有小數點,補上小數點和兩位零 } else { cout<<'('; for(i=k-1; i>=0; i--) { cout<<r[i]; if(i%3==0&&i>3) cout<<','; } cout<<')'<<endl; } } else { if(!f) { for(i=k-1; i>=0; i--) { cout<<r[i]; if(i%3==0&&i) cout<<','; } cout<<".00"<<endl; } else { for(i=k-1; i>=0; i--) { cout<<r[i]; if(i%3==0&&i>3) cout<<','; } cout<<endl; } } } }
1104、
就我不坑
- 描述
-
as we all know ,就我不坑,呵呵,當然,這次我還是不坑,我只讓你幫我翻譯一下數字即可。
所謂翻譯,就是將一個數字用中文讀出來,很簡單吧,快快AC吧。
數字的中文表示分別為:零、壹、貳、叄、肆、伍、陸、柒、捌、玖、拾、佰、仟、萬、億.
這題換題了,之前不是這題目,所以就粘貼了一份來 #include<stdio.h> #include<string.h> int main() { char ch[20]; int len,i,k,t,p; while(scanf("%s",ch)!=EOF) { k=0;p=1; len=strlen(ch); for(i=0;i<len;i++) { t=len-k; if(ch[i]=='0'&&(t-1)%4!=0&&ch[i+1]!='0'||ch[0]=='0') printf("零"); if(ch[i]=='1') printf("壹"); if(ch[i]=='2') printf("貳"); if(ch[i]=='3') printf("叄"); if(ch[i]=='4') printf("肆"); if(ch[i]=='5') printf("伍"); if(ch[i]=='6') printf("陸"); if(ch[i]=='7') printf("柒"); if(ch[i]=='8') printf("捌"); if(ch[i]=='9') printf("玖"); if(ch[i]!='0'||(t-1)%4==0) { if(t==10) printf("拾"); if(t==9) printf("億"); if(t==8) printf("仟"); if(t==7) printf("佰"); if(t==6) printf("拾"); if(t==5&&(ch[k]!='0'||ch[k-1]!='0'||ch[k-2]!='0'||ch[k-3]!='0')) printf("萬"); if(t==4) printf("仟"); if(t==3) printf("佰"); if(t==2) printf("拾"); } k++; } printf("\n"); } return 0; }
OK,本文到此也就結束了,也花了本人好幾個小時的時間,但AC這些題卻用了好幾個月,花了相當長的時間,也是邊刷邊學習,不斷改進,下次再寫匯總也不知何年何月了,但還會寫其他的,馬上學java了,又有的寫了,慢慢來。