八个比特

HOJ笔记

字数统计: 699阅读时长: 4 min
2019/04/07 Share

1001 - A+B

1
2
3
4
5
6
7
#include <stdio.h>
int main(void){
int a, b;
while (scanf("%d %d", &a, &b)==2)
printf("%d\n",a+b);
return 0;
}


1002 - A+B+C

1
2
3
4
5
6
7
8
#include <stdio.h>
int main(){
long long int a,b,c;
while(scanf("%lld %lld %lld",&a,&b,&c) == 3){
printf("%lld\n", a+b+c);
}
return 0;
}


Mixing Milk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string.h>
#include <stdio.h>
int milk[1000];
int main(){
int N, M, a, b, total = 0;
scanf("%d%d", &N, &M);
memset(milk, 0, sizeof(milk));
for(int i = 1; i <= M; i++){
scanf("%d%d", &a, &b);
milk[a] += b;
}
for(int i = 0; N > 0; i++){
if(milk[i] < N){
total += i * milk[i];
N -= milk[i];
}else{
total += N * i;
break;
}
}
printf("%d\n", total);
return 0;
}


Prime Palindromes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
int main(){
int m,a[1000],b[1000],tmp1,tmp,i,j;
int n,sum=0;
scanf("%d%d",&n,&m);
for(i=1; i<=m; i++)
scanf("%d%d",&a[i],&b[i]);
for(i=1; i<=m; i++){
for(j=i+1; j<=m; j++){
if(a[j]<a[i]){ tmp=a[j],a[j]=a[i],a[i]=tmp;
tmp1=b[j],b[j]=b[i],b[i]=tmp1;
}
}
n-=b[i];
sum+=(b[i]*a[i]);
if(n<=0){
sum+=n*a[i];
break;
}
}
printf("%d\n",sum);
}

----------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <math.h>
/**
int is_prime(int num){
int ret = 1;
for (int i = 2; i < num - 1; i++){
if (num % i == 0){
ret = 0;
break;
}
}
return ret;
}
**/
int is_prime(int n){
float n_sqrt;
if(n==2 || n==3) return 1;
if(n%6!=1 && n%6!=5) return 0;
n_sqrt=floor(sqrt((float)n));
for(int i=5;i<=n_sqrt;i+=6){
if(n%(i)==0 | n%(i+2)==0) return 0;
}
return 1;
}
int is_reval(int num){
int c=0;
int tmp_num = num;
do{
c=c*10+tmp_num%10;
tmp_num/=10;
}
while(tmp_num>0);
if(num==c) return 1;
return 0;
}
int main(){
int a,b;
scanf("%d%d",&a,&b);
for(int i=a;i<b;i++){
if((is_prime(i)==1)&&(is_reval(i)==1)){
printf("%d\n",i);
}
}
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
#include<string.h>
#define maxn 220
#define INF 99999999
int dis[maxn],dp[maxn][maxn],cost[maxn][maxn];
int abs(int x){
return x<0?-x:x;
}
int min(int a,int b){
return a<b?a:b;
}
int main(){
//freopen("input.txt","r",stdin);
int n,k;
int cases=0;
while(scanf("%d%d",&n,&k)){
int i,j,m;
if(n==0 && k==0) break;
for(i=1;i<=n;i++)
scanf("%d",&dis[i]);
for(i=1;i<=n;i++)
for(j=i;j<=n;j++){
cost[i][j]=0;
for(m=i;m<=j;m++)
cost[i][j]+=abs(dis[m]-dis[(i+j)/2]);
}
for(i=1;i<=n;i++)
dp[1][i]=cost[1][i];
for(i=2;i<=k;i++)
for(j=i;j<=n;j++){
dp[i][j]=INF;
for(m=i-1;m<=j-1;m++)
dp[i][j]=min(dp[i][j],dp[i-1][m]+cost[m+1][j]);
}
printf("Chain %d\nTotal distance sum = %d\n\n",++cases,dp[k][n]);
}
return 0;
}
CATALOG