1. 数字三角形,POJ1163,不多说。
code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<fstream>
#include<cstdlib>
#include<memory>
#include<algorithm>
#define read freopen("numtri.in", "r", stdin)
#define write freopen("numtri.out", "w", stdout)
#define rd ifstream cout("numtri.out") ;
#define wt ifstream cin("numtri.in") ;
#define max(a, b) a>b?a:b
int data[1001][1001] ;
int main(){
read ;
write ;
int i, j, n ;
scanf("%d", &n) ;
for(i=0; i<n; i++)
for(j=0; j<=i; j++)
scanf("%d", &data[i][j]) ;
for(i=n-2; i>=0; i--)
for(j=0; j<=i; j++)
data[i][j] += max(data[i+1][j], data[i+1][j+1]) ;
printf("%d\n", data[0][0]) ;
return 0 ;}
#include<iostream>
#include<cstring>
#include<fstream>
#include<cstdlib>
#include<memory>
#include<algorithm>
#define read freopen("numtri.in", "r", stdin)
#define write freopen("numtri.out", "w", stdout)
#define rd ifstream cout("numtri.out") ;
#define wt ifstream cin("numtri.in") ;
#define max(a, b) a>b?a:b
int data[1001][1001] ;
int main(){
read ;
write ;
int i, j, n ;
scanf("%d", &n) ;
for(i=0; i<n; i++)
for(j=0; j<=i; j++)
scanf("%d", &data[i][j]) ;
for(i=n-2; i>=0; i--)
for(j=0; j<=i; j++)
data[i][j] += max(data[i+1][j], data[i+1][j+1]) ;
printf("%d\n", data[0][0]) ;
return 0 ;}
2. 这题打素数表显然不可能了,所以要得到回文数字。两种方法:
(1) 5..10000000依次判断是否为回文,当然中间可以跳过很多,比如偶数,比如除11外的长度为偶数的。
(2) 直接生成所有长度为奇数的回文奇数再加上11。
先用第一种写的,写完加了各种优化各种剪枝,代码跟脓一样了还是个超时。第二种就好多了,虽然代码难看点但是效率挺高。最慢0.03
code:
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
#define read freopen("pprime.in", "r", stdin)
#define write freopen("pprime.out", "w", stdout)
#define rd ifstream cout("pprime.out") ;
#define wt ifstream cin("pprime.in") ;
int cnt, pa[1000001] ;
void get_palin(){
int i, j, k, l, g ;
cnt = 0 ;
pa[cnt++] = 5 ;
pa[cnt++] = 7 ;
pa[cnt++] = 11 ;
for(i=1; i<10; i+=2)
for(j=0; j<10; j++)
pa[cnt++] = 100*i+i+10*j ;
for(i=1; i<10; i+=2)
for(j=0; j<10; j++)
for(k=0; k<10; k++)
pa[cnt++] = 10000*i+i+1000*j+10*j+100*k ;
for(i=1; i<10; i+=2)
for(j=0; j<10; j++)
for(k=0; k<10; k++)
for(l=0; l<10; l++)
pa[cnt++] = 1000000*i+i+100000*j+10*j+10000*k+100*k+1000*l ;
}
int is_prim(int a){
for(int i=2; i<=sqrt(a); i++)
if(a%i==0) return 0 ;
return 1 ;
}
int main(){
read ;
write ;
int i, n, m ;
scanf("%d%d", &n, &m) ;
get_palin() ;
for(i=0; i<cnt ;i++){
if(pa[i]<n||pa[i]>m||!is_prim(pa[i])) continue ;
printf("%d\n", pa[i]) ;
}
return 0 ;
}
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
#define read freopen("pprime.in", "r", stdin)
#define write freopen("pprime.out", "w", stdout)
#define rd ifstream cout("pprime.out") ;
#define wt ifstream cin("pprime.in") ;
int cnt, pa[1000001] ;
void get_palin(){
int i, j, k, l, g ;
cnt = 0 ;
pa[cnt++] = 5 ;
pa[cnt++] = 7 ;
pa[cnt++] = 11 ;
for(i=1; i<10; i+=2)
for(j=0; j<10; j++)
pa[cnt++] = 100*i+i+10*j ;
for(i=1; i<10; i+=2)
for(j=0; j<10; j++)
for(k=0; k<10; k++)
pa[cnt++] = 10000*i+i+1000*j+10*j+100*k ;
for(i=1; i<10; i+=2)
for(j=0; j<10; j++)
for(k=0; k<10; k++)
for(l=0; l<10; l++)
pa[cnt++] = 1000000*i+i+100000*j+10*j+10000*k+100*k+1000*l ;
}
int is_prim(int a){
for(int i=2; i<=sqrt(a); i++)
if(a%i==0) return 0 ;
return 1 ;
}
int main(){
read ;
write ;
int i, n, m ;
scanf("%d%d", &n, &m) ;
get_palin() ;
for(i=0; i<cnt ;i++){
if(pa[i]<n||pa[i]>m||!is_prim(pa[i])) continue ;
printf("%d\n", pa[i]) ;
}
return 0 ;
}
3. 跟上一题一样也是直接生成,可以递归实现,我写成for的形式了。
code:
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
#define read freopen("sprime.in", "r", stdin)
#define write freopen("sprime.out", "w", stdout)
#define rd ifstream cout("sprime.out") ;
#define wt ifstream cin("sprime.in") ;
int ans[10][100001], cnt[11] ;
int is_prim(int a){
for(int i=2; i<=sqrt(a); i++)
if(a%i==0) return 0 ;
return 1 ;
}
int main(){
read ;
write ;
int n, i, j, k ;
scanf("%d", &n) ;
memset(cnt, 0, sizeof(cnt)) ;
cnt[1] = 4 ;
ans[1][0] = 2, ans[1][1] = 3, ans[1][2] = 5, ans[1][3] = 7 ;
for(i=2; i<9; i++)
for(k=0; k<cnt[i-1]; k++)
for(j=1; j<10; j+=2){
int t = ans[i-1][k] * 10 + j ;
if(is_prim(t)) ans[i][cnt[i]++] = t ;
}
for(i=0; i<cnt[n]; i++)
printf("%d\n", ans[n][i]) ;
return 0 ;}
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
#define read freopen("sprime.in", "r", stdin)
#define write freopen("sprime.out", "w", stdout)
#define rd ifstream cout("sprime.out") ;
#define wt ifstream cin("sprime.in") ;
int ans[10][100001], cnt[11] ;
int is_prim(int a){
for(int i=2; i<=sqrt(a); i++)
if(a%i==0) return 0 ;
return 1 ;
}
int main(){
read ;
write ;
int n, i, j, k ;
scanf("%d", &n) ;
memset(cnt, 0, sizeof(cnt)) ;
cnt[1] = 4 ;
ans[1][0] = 2, ans[1][1] = 3, ans[1][2] = 5, ans[1][3] = 7 ;
for(i=2; i<9; i++)
for(k=0; k<cnt[i-1]; k++)
for(j=1; j<10; j+=2){
int t = ans[i-1][k] * 10 + j ;
if(is_prim(t)) ans[i][cnt[i]++] = t ;
}
for(i=0; i<cnt[n]; i++)
printf("%d\n", ans[n][i]) ;
return 0 ;}
4. 八皇后,dfs+回溯。
code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<fstream>
#include<cstdlib>
#include<memory>
#include<algorithm>
#define read freopen("checker.in", "r", stdin)
#define write freopen("checker.out", "w", stdout)
#define rd ifstream cout("checker.out") ;
#define wt ifstream cin("checker.in") ;
int n, c[15], d1[30], d2[30], ans[15], cnt ;
void print(){
int i ;
for(i=1; i<n; i++)
printf("%d ", ans[i]) ;
printf("%d\n", ans[i]) ;
}
void dfs(int r){
if(r==n+1){
cnt ++ ;
if(cnt<4)
print() ;
return ;
}
for(int i=1; i<=n; i++){
if(c[i]||d1[r+i]||d2[r-i+n]) continue ;
c[i] = 1 ;
d1[r+i] = 1 ;
d2[r-i+n] = 1 ;
ans[r] = i ;
dfs(r+1) ;
c[i] = 0 ;
d1[r+i] = 0 ;
d2[r-i+n] = 0 ;
}
}
int main(){
read ;
write ;
scanf("%d", &n) ;
memset(c, 0, sizeof(c)) ;
memset(d1, 0, sizeof(d1)) ;
memset(d2, 0, sizeof(d2)) ;
cnt = 0 ;
dfs(1) ;
printf("%d\n", cnt) ;
return 0 ;}
#include<iostream>
#include<cstring>
#include<fstream>
#include<cstdlib>
#include<memory>
#include<algorithm>
#define read freopen("checker.in", "r", stdin)
#define write freopen("checker.out", "w", stdout)
#define rd ifstream cout("checker.out") ;
#define wt ifstream cin("checker.in") ;
int n, c[15], d1[30], d2[30], ans[15], cnt ;
void print(){
int i ;
for(i=1; i<n; i++)
printf("%d ", ans[i]) ;
printf("%d\n", ans[i]) ;
}
void dfs(int r){
if(r==n+1){
cnt ++ ;
if(cnt<4)
print() ;
return ;
}
for(int i=1; i<=n; i++){
if(c[i]||d1[r+i]||d2[r-i+n]) continue ;
c[i] = 1 ;
d1[r+i] = 1 ;
d2[r-i+n] = 1 ;
ans[r] = i ;
dfs(r+1) ;
c[i] = 0 ;
d1[r+i] = 0 ;
d2[r-i+n] = 0 ;
}
}
int main(){
read ;
write ;
scanf("%d", &n) ;
memset(c, 0, sizeof(c)) ;
memset(d1, 0, sizeof(d1)) ;
memset(d2, 0, sizeof(d2)) ;
cnt = 0 ;
dfs(1) ;
printf("%d\n", cnt) ;
return 0 ;}