/*查找非素数 #include<iostream> using namespace std; int fun(int m) { int i; for(i=2;i<m;i++) { if(m%i==0) return 0; } return 1; } void main() { int m,i; cin>>m; for (i=0;i<=m;i++) { if(fun(i)==0) cout<<i<<" "; } } */ /* 查找二进制0 #include <iostream> using namespace std; void main() { int a[30],m; int i=0; int j,k; int count = 0; cin>>m; while (m) { a[i]=m&0001; i++; m=m/2; } j=i; for (i=j-1;i>=0;i--) { if(a[i]==1) { k=i; break; } } for(i=0;i<k;i++) { if(a[i] == 0) count++; } cout<<count; } */ /* 最大公约数 #include <iostream> using namespace std; void main() { int m,n; cin>>m>>n; int r=m%n; while (r) { m=n; n=r; r=m%n; } cout<<n; } */ /* 字符串中不同字符的个数 #include <iostream> using namespace std; void main() { char a,s[100]; int count = 0; int i,j; gets(s); for (i=0;i<strlen(s)-1;i++) { for(j=0;j<strlen(s)-1-i;j++) if(s[j+1]<s[j]) { a = s[j]; s[j] = s[j-1]; s[j-1] = a; } } for (i=0;i<strlen(s);i++) { if(s[i+1] != s[i]) { count++; } } cout<<count; } */ /* 计算整数的位数 #include <iostream> #include <string.h> using namespace std; #define N 6 void main() { char a[10][N]; int i,j; i=0; while (scanf("%s",a[i])!= EOF) { cout<<strlen(a[i])<<" "; for (j=strlen(a[i])-1;j>=0;j--) { cout<<a[i][j]; } cout<<endl; } } */ /* 图片整理 #include <iostream> #include <string.h> using namespace std; void main() { char a[1024],s; int i,j,k; scanf("%s",a); for (i=0;i<strlen(a)-1;i++) { for (j=0;j<strlen(a)-1-i;j++) { if (a[j+1]<a[j]) { s = a[j]; a[j] = a[j+1]; a[j+1] = s; } } } puts(a); } */ /* 最高分查询 #include <iostream> using namespace std; void main() { int N,M; int *a; int i,j,k,m,temp; char s1; cin>>N>>M; a = (int *)malloc(N*sizeof(int)); for(i=0;i<N;i++) cin>>a[i]; for(j=0;j<M;j++) { cin>>s1; cin>>k>>m; if (s1 == 'Q') { temp=0; for (i=k;i<=m;i++) { if (a[i]>temp) { temp=a[i]; } } cout<<temp<<endl; } else if (s1 == 'U') { a[k] = m; } } free(a); } */ /* 字符变换 #include <iostream> #include <string.h> using namespace std; void main() { char a[50]; int i; gets(a); for (i=0;i<strlen(a);i++) { if (a[i]>='A' && a[i]<='U') { a[i] = a[i] + 37; } else if (a[i] =='V' ) { a[i] = 'a'; } else if (a[i] =='W' ) { a[i] = 'b'; } else if (a[i] =='X' ) { a[i] = 'c'; } else if (a[i] =='Y' ) { a[i] = 'd'; } else if (a[i] =='Z' ) { a[i] = 'e'; } } puts(a); } */ /* 尼科彻斯定理 #include <iostream> using namespace std; void main() { int m,i; cin>>m; int m3 = m*m*m; int m2=m*m; int ans; if (m%2 == 0) { ans = 0; for (i=0;i<m/2;i++) { ans = ans + m2-1-2*i; ans = ans + m2+1+2*i; } if (ans == m3) { for (i=m/2-1;i>=0;i--) { cout<<m2-1-2*i<<"+"; } for (i=0;i<m/2-1;i++) { cout<<m2+1+2*i<<"+"; } cout<<m2+1+2*(m/2-1); } else cout<<-1; } else { ans = m2; for (i=1;i<=m/2;i++) { ans = m2-2*i+ans; ans = m2+2*i+ans; } if (ans == m3) { for (i=m/2;i>0;i--) { cout<<m2-2*i<<"+"; } for (i=0;i<m/2;i++) { cout<<m2+2*i<<"+"; } cout<<m2+2*(m/2); } else cout<<-1; } } */ #include <iostream> using namespace std; void main() { int m,i; cin>>m; int m3 = m*m*m; int k= m*(m-1)+1; int sum = 0; for (i=0;i<m;i++) { sum = sum+k+2*i; } if (sum == m3) { for (i=0;i<m-1;i++) { cout<<k+2*i<<"+"; } cout<<k+2*i; } else cout<<-1; } /* 找零钱 #include <iostream> using namespace std; int a[7] = {1,2,5,10,20,50,100}; int fun(int n,int i) { if(n==1 || i==0) return 1; if(n<0 || i<0) return 0; return fun(n-a[i],i)+fun(n,i-1); } void main() { int n; int i; cin>>n; while (n != 0) { for (i=6;i>=0;i--) { if (n>=a[i]) { break; } } cout<<fun(n,i)<<endl; cin>>n; } } */
部分初级题集合