摘要: C++中的sort函数可以直接完美地取代Pas中十多行的快排代码,在这里,总结一下sort函数的用法:首先是不加参数的情况:#include#include using namespace std;int main(){ int n,a[10000]; scanf("%d",&n); for (int i=0; i#include using namespace std;bool cmp(int a,int b) {return a>b;};int main(){ int n,a[10000]; scanf("%d",&n); fo 阅读全文
posted @ 2013-12-28 10:05 forever97 阅读(474) 评论(0) 推荐(0) 编辑
摘要: 题解:分析题目,就是一个裸的欧拉函数,于是AC。#include int eular(int n){ int ret=1,i; for(i=2;i*i1) ret*=n-1; return ret;}int main(){ int n; scanf("%d",&n); ... 阅读全文
posted @ 2013-12-27 16:00 forever97 阅读(84) 评论(0) 推荐(0) 编辑
摘要: 题目大意:求出比给出数小的互质的质数个数。题解:直接用筛法求素数,稍微改编一下,将原先的布尔数组变为数组用来记录信息就可以了。注意点:大的数组定义要放在程序的开头,不要放在main里面,不然会栈溢出。#include #define max 1000000int prim[max]={0};int main(){ int n; int cnt=1; for(int i=2; i<max; ++i) { if(prim[i]) continue; for(int j=i; j<max; j+=i) prim[j]=cnt; ... 阅读全文
posted @ 2013-12-27 15:48 forever97 阅读(105) 评论(0) 推荐(0) 编辑
摘要: #include int gcd(int m, int n) { return m==0?n:gcd(n % m, m); } int main() { int m, n; while(scanf("%d%d",&m,&n)!=EOF) { printf("%d\n", m + n - gcd(m, n)); } return 0; } 题解:考虑切蛋糕为q块需要q刀,p块需要p刀,但是两者切痕有重叠,重叠部分为两者的最大公约数,所以问题就迎刃而解了。 阅读全文
posted @ 2013-12-27 15:09 forever97 阅读(111) 评论(0) 推荐(0) 编辑
摘要: #include long long gcd(long long a,long long b){ if (b==0) return a; return (gcd(b,a%b));}int main(){ int t; long long a,b,c,d; scanf("%d",&t); while (t--) { scanf("%I64d/%I64d%I64d/%I64d",&a,&b,&c,&d); a=a*b*c*d/gcd(a*d,c*b); b=b*d; if(a%b) {c=... 阅读全文
posted @ 2013-12-27 14:50 forever97 阅读(124) 评论(0) 推荐(0) 编辑
摘要: 米勒罗宾素数测试:/* if n long long power(long long m,long long n,long long k){ long long b = 1; while (n>0) { if (n&1) b=(b*m)%k; n=n>>1; m=(m*m)%k; } return b;} bool Miller_Rabbin(long long t){ if (t==2) return true; if (power(2,t-1,t)!=1) return false; if (pow... 阅读全文
posted @ 2013-12-27 14:17 forever97 阅读(205) 评论(0) 推荐(0) 编辑
摘要: #include int gcd(int a,int b){ if (b==0) return a; else return gcd(b,a%b);}int main(){ int m,n; while (scanf("%d%d",&m,&n)!=EOF) { printf("%d\n",m*n/gcd(m,n)); } return 0;} 阅读全文
posted @ 2013-12-25 16:49 forever97 阅读(238) 评论(0) 推荐(0) 编辑
摘要: #include #include using namespace std;int main(){ int n; while(scanf("%d",&n)!=EOF) { int a[100000]; for(int i=0; i<n; i++) scanf("%d",&a[i]); sort(a,a+n); printf("%d\n",a[n/2]); } return 0;} 阅读全文
posted @ 2013-12-25 16:40 forever97 阅读(122) 评论(0) 推荐(0) 编辑
摘要: #include #include #include using namespace std;struct cc{ int id; int mark;}a[10000];bool cmp(cc a,cc b) {return (a.mark>b.mark);} int main(){ int m,n; while(scanf("%d",&n)!=EOF) { int i=0; while(scanf("%d%d",&a[i].id,&a[i].mark),a[i].id!=0) i++; int s=i; s... 阅读全文
posted @ 2013-12-25 16:29 forever97 阅读(98) 评论(0) 推荐(0) 编辑
摘要: #include #include using namespace std;int main(){ int n; scanf("%d",&n); while(n--) { int m; int a[100]; scanf("%d",&m); for (int i=0; i<m; i++) scanf("%d",&a[i]); sort(a,a+m); printf("%d\n",a[1]); } return 0; } 阅读全文
posted @ 2013-12-24 20:56 forever97 阅读(113) 评论(0) 推荐(0) 编辑
摘要: #include #include using namespace std;int main(){ int n; while (scanf("%d",&n)!=EOF) { int a[10010]; for (int i=0; i<n; i++) scanf("%d",&a[i]); sort(a,a+n); int begin=0,end=n-2,flag=1; printf("%d",a[n-1]); while (begin<=end) { ... 阅读全文
posted @ 2013-12-24 20:46 forever97 阅读(237) 评论(0) 推荐(0) 编辑
摘要: #include #include #include using namespace std;struct node{ string name,begin,end;}a[1000];int cmp1(node a,node b) { return a.beginb.end;}int main(){ int n,m,i; cin>>n; while(n--) { cin>>m; for(i=0;i>a[i].name>>a[i].begin>>a[i].end; sort(a,a+m,cmp1); cout<<... 阅读全文
posted @ 2013-12-24 19:57 forever97 阅读(159) 评论(0) 推荐(0) 编辑
摘要: #include #include using namespace std;struct arc{ int a; int b;};bool cmp(arc x,arc y){ return (x.a+"); for (int j=3; j\n"); } printf("\n"); } } return 0;} 注意点:在这道题中学会了sort的各种用法,还有结构体的定义,需要注意的是结构体定义完后一定要加上‘;’。 阅读全文
posted @ 2013-12-24 19:13 forever97 阅读(147) 评论(0) 推荐(0) 编辑
摘要: 题解:对于这道题,我们采用这样的策略,首先,排序是明显的,然后第一步,若己方最弱的比对方最弱的强,则两者比赛,如果不是,进行第二步,比较己方最强的和敌方最强的,若己方强则两者比赛,否则将己方最弱的与敌方最强的比赛。然后依次做下去,统计结果即为答案。注意点:学习了sort的用法,是从0开始读入数组的,注意algorithm的拼写,最后注意判断是双等号,pas的习惯还是没有改过来……#include #include using namespace std;int main(){ int n; while(scanf("%d",&n),n!=0) { int al,.. 阅读全文
posted @ 2013-12-22 15:15 forever97 阅读(126) 评论(0) 推荐(0) 编辑
摘要: #include #include int main(){ int n; scanf("%d",&n); for(int i=1; ib) { int t=a; a=b; b=t; } a=(a+1)/2; b=(b+1)/2; x[a]++; x[b+1]--; } max=0; s=0; for(int j=1; jmax) max=s; ... 阅读全文
posted @ 2013-12-22 14:13 forever97 阅读(121) 评论(0) 推荐(0) 编辑
摘要: #include #include using namespace std;int main(){ int n; scanf("%d",&n); for(int i=1; i<=n; i++) { char c,demo; int a,b; scanf("%c%c%c%d%c%d",&demo,&c,&demo,&a,&demo,&b); if (c=='+') printf("%d\n",a+b); if (c=='-') printf(& 阅读全文
posted @ 2013-12-22 12:58 forever97 阅读(126) 评论(0) 推荐(0) 编辑
摘要: #include #include using namespace std;void swap(int& a,int& b){ int t; t=a; a=b; b=t;}int main(){ int n; scanf("%d",&n); for (int i=1; ic) swap(c,b); if (a>c) swap(a,c); if ((c*c)==(a*a+b*b)) printf("%s\n\n","yes"); else printf("%s\n\n","no 阅读全文
posted @ 2013-12-20 16:53 forever97 阅读(162) 评论(0) 推荐(0) 编辑
摘要: #include #include int main(){ char s[10000]; while(gets(s),s[0]!='#') { int i=0; while (i<strlen(s)) { if (s[i]==' ') printf("%s","%20"); else if (s[i]=='!') printf("%s","%21"); else if (s[i]=='$') printf("%s"," 阅读全文
posted @ 2013-12-20 16:36 forever97 阅读(140) 评论(0) 推荐(0) 编辑
摘要: 题解:这道题我们直接考虑数字的最后一位即可,又数字的最后一位最多只有10中情况,所以我们只要模拟最后一位相乘的过程,一旦出现循环,就直接输出下标为这个数字对循环节长度取模的结果的内存即可。#include int main(){ int n; scanf("%d",n); while(scanf("%d",&n)!=EOF) { int a[100]; bool go=true; int m=n%10; int x=n%10; a[1]=m; int tot=1; do ... 阅读全文
posted @ 2013-12-20 16:03 forever97 阅读(124) 评论(0) 推荐(0) 编辑
摘要: 翻译:assume 假设题解:题目的大意是找到一个只出现奇数次的数字,由于其它的数字成双出现,我们想到了xor的性质,将所有读入的数字xor起来,最后结果就是需要找的数字了。#include int main(){ int n,x,sum; while(scanf("%d",&n),n!=0) { sum=0; for(int i=1; i<=n; i++) { scanf("%d",&x); sum^=x; } printf("%d\n",sum); } ... 阅读全文
posted @ 2013-12-20 15:21 forever97 阅读(134) 评论(0) 推荐(0) 编辑