函数专题
第一题
#include<bits/stdc++.h>
using namespace std;
int main() {
int s,t;
cin>>s>>t;
int ans = 0 ;
for (int i = s ; i <= t ; i ++ ) {
int x = i,cnt = 0;
while (x) {
cnt++;
x=x&(x-1);
}
if (cnt<=4) ans++;
}
cout<<ans<<endl;
return 0 ;
}
第二题
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b) {
while (b>0) {
int tmp = a;
a = b;
b = tmp%b;
}
return a;
}
int GCD(int a,int b) {
if (b==0) return a;
return GCD(b,a%b);
}
int main() {
//cout<<gcd(12,8)<<endl;
int n,ans;
cin>>n>>ans;
for (int i = 2; i <= n ; i ++) {
int x;cin>>x;
ans = gcd(ans,x);
}
cout<<ans<<endl;
return 0;
}
第三题
#include<bits/stdc++.h>
using namespace std;
int a,b;
bool iswq(int n){
int x = n;
int sum = 0;
//a是n的约数,n/a也是n的因子.
//a<=n/a a*a<=n
for (int i = 1; i*i <= n ; i ++) {
if (n%i==0) {
sum+=i;
if (i>1&&i*i!=n) sum+=n/i;
}
}
return sum==x;
}
string to16(int n) {
string s="";
while (n) {
if (n%16>9) s = char(n%16+'A'-10)+s;
else s = char('0'+n%16)+s;
n/=16;
}
return s;
}
int main() {
int s,t;
cin>>s>>t;
iswq(6);
while (s<=t) {
if (iswq(s)) {
cout<<to16(s)<<' ';
}
s++;
}
return 0;
}
第四题
#include<bits/stdc++.h>
using namespace std;
bool isprime(int x) {
if (x<2) return false;
if (x==2) return true;
if (x%2==0) return false;
for (int i = 3 ; i * i <= x ; i +=2) {
if (x%i==0) return false;
}
return true;
}
int suo(int x) {
int p = 10;
//123%1000=123
//123%100=23
//123%10=3
while (x/p!=0) p*=10;
p/=10;
return x%p;
}
int main() {
int n;
cin>>n;
for (int i = 2; i <= n ; i ++ ) {
int x = i;
bool flag = true;
while (x) {
if (!isprime(x)) {
flag=false;
break;
}
x=suo(x);
}
if (flag) cout<<i<<' ';
}
cout<<endl;
return 0;
}
第五题
#include<bits/stdc++.h>
using namespace std;
int a[10][10];
bool lie(int j) {
bool p[10];memset(p,0,sizeof(p));
for (int i = 1; i <= 9 ; i ++)
if (p[a[i][j]]||a[i][j]==0) return false;
else p[a[i][j]]=true;
return true;
}
bool hang(int i) {
bool p[10];memset(p,0,sizeof(p));
for (int j = 1; j <= 9 ; j ++)
if (p[a[i][j]]||a[i][j]==0) return false;
else p[a[i][j]]=true;
return true;
}
bool jgg(int x,int y) {
//(1,1)(1,4),(1,7)
//(4,1)(4,4),(4,7);
//(7,1)(7,4),(7,7)
bool p[10];memset(p,0,sizeof(p));
for (int i = x ; i <= x+2; i ++ )
for (int j = y ; j <= y + 2; j ++ )
p[a[i][j]]=true;
for (int i =1 ; i <= 9 ; i ++ ) if (p[i]==0) return false;
return true;
}
int main() {
int t;
cin>>t;
while (t--) {
for (int i = 1; i <= 9 ; i ++ )
for (int j = 1; j <= 9 ; j ++ ) cin>>a[i][j];
bool flag=true;
for (int i = 1; i <= 9 ; i ++ )
if (!(hang(i)&&lie(i))) flag=false;
for (int i = 1; i <= 7 ; i += 3)
for (int j = 1; j <= 7 ; j += 3)
if (!jgg(i,j)) flag=false;
if (flag) cout<<"Right"<<endl;
else cout<<"Wrong"<<endl;
}
}
第六题
#include<bits/stdc++.h>
using namespace std;
struct image{
char a[30][30];
};
image a;
int n;
image rotate90(image a) {
image b;
for (int i = 1; i <= n ; i ++) {
for (int j = 1; j <= n ; j ++ ) {
b.a[j][n-i+1]=a.a[i][j];
}
}
return b;
}
image add(image a,image b) {
image c;
for (int i = 1; i <= n ; i ++) {
for (int j = 1; j <= n ; j ++) {
int A = a.a[i][j]-48;
int B = b.a[i][j]-48;
c.a[i][j]=48+(A|B);
}
}
return c;
}
int calc(image a) {
int cnt = 0;
for (int i = 1; i <= n ; i ++ )
for (int j = 1; j <= n ; j ++ )
if (a.a[i][j]=='1') cnt++;
return cnt;
}
int main(){
cin>>n;
for (int i = 1; i <= n ; i ++ )
for (int j = 1; j <= n ; j ++ ) cin>>a.a[i][j];
image r90,r180,r270;
r90=rotate90(a);
r180=rotate90(r90);
r270=rotate90(r180);
cout<<calc(a)<<endl;
image x=add(a,r90);
cout<<calc(x)<<endl;
x = add(x,r180);
cout<<calc(x)<<endl;
x = add(x,r270);
cout<<calc(x)<<endl;
return 0;
}