杭电oj2012-2021
2012 素数判定
1 #include <stdio.h> 2 #include <math.h> 3 4 int main() 5 { 6 int x,y,i,j,a,flag; 7 while(scanf("%d %d",&x,&y)!=EOF){ 8 flag = 0; 9 if(x==0&&y==0){break;} 10 for(i=x;i<=y;i++){ 11 a = i*i+i+41; 12 for(j=2;j<a/2;j++){ 13 if(a%j==0){flag++;break;} 14 } 15 if(flag!=0){break;} 16 } 17 if(flag==0){printf("OK\n");} 18 else{printf("Sorry\n");} 19 } 20 }
2013 蟠桃记
1 #include <stdio.h> 2 3 int main() 4 { 5 int n,sum; 6 while(scanf("%d",&n)!=EOF){ 7 sum = 1; 8 while(--n){ 9 sum = (sum+1)*2; 10 } 11 printf("%d\n",sum); 12 } 13 }
2014 青年歌手大奖赛_评委会打分
1 #include <stdio.h> 2 3 int main(){ 4 int arr[101]; 5 int n,i,j,k,temp; 6 double sum,p; 7 while(scanf("%d",&n)!=EOF){ 8 sum = 0; 9 for(i=0;i<n;i++){ 10 scanf("%d",&arr[i]); 11 } 12 13 for(i=0;i<n-1;i++){ 14 for(j=0;j<n-1-i;j++){ 15 if(arr[j]>arr[j+1]){ 16 temp = arr[j]; 17 arr[j] = arr[j+1]; 18 arr[j+1] = temp; 19 } 20 } 21 } 22 23 for(k=1;k<n-1;k++){sum += arr[k];} 24 p = sum/(n-2); 25 printf("%.2lf\n",p); 26 } 27 }
2015 偶数求和
1 #include <stdio.h> 2 3 int main(){ 4 int n,m,i,j,a,count,sum; 5 int s[101],c[101]; 6 while(scanf("%d %d",&n,&m)!=EOF){ 7 a = 0; 8 count = 0; 9 for(i=1;i<=n;i++){a += 2;s[i] = a;} 10 for(i=1;i<=n;i++){ 11 if(i%m==0){ 12 sum = 0; 13 for(j=i-m+1;j<=i;j++){sum += s[j];} 14 c[count] = sum/m; 15 count++; 16 } 17 if(i==n&&i%m!=0){ 18 sum = 0; 19 for(j=i-(n%m)+1;j<=i;j++){sum += s[j];} 20 c[count] = sum/(n%m); 21 count++; 22 } 23 } 24 for(i=0;i<count;i++){ 25 if(i==count-1){printf("%d\n",c[i]);} 26 else{printf("%d ",c[i]);} 27 } 28 } 29 }
2016 数据的交换输出
1 #include <stdio.h> 2 3 int main(){ 4 int s[101]; 5 int n,i,flag,temp,a; 6 while(scanf("%d",&n)!=EOF){ 7 if(n==0){break;} 8 for(i=0;i<n;i++){ 9 scanf("%d", &s[i]); 10 } 11 temp = 10000; 12 for(i=0;i<n;i++){ 13 if(temp>s[i]){flag=i;temp = s[i];} 14 } 15 a = s[0]; 16 s[0] = s[flag]; 17 s[flag] = a; 18 for(i=0;i<n;i++){ 19 if(i==n-1){printf("%d\n", s[i]);} 20 else{printf("%d ",s[i]);} 21 } 22 } 23 }
2017 字符串统计
1 #include <stdio.h> 2 #include <string.h> 3 4 int main(){ 5 char s[10000]; 6 int n,len,i,count; 7 while(scanf("%d", &n)!=EOF){ 8 getchar(); 9 while(n--){ 10 count = 0; 11 12 gets(s); 13 len = strlen(s); 14 15 for(i=0;i<len;i++){ 16 if(s[i]>='0'&&s[i]<='9'){count++;} 17 } 18 printf("%d\n",count); 19 } 20 21 } 22 }
2018 母牛的故事
1 #include <stdio.h> 2 3 int main(){ 4 int n,i; 5 int s[55]; 6 while(scanf("%d",&n)!=EOF){ 7 if(n==0){break;} 8 s[1]=1;s[2]=2; 9 s[3]=3;s[4]=4; 10 for(i=5;i<=n;i++){ 11 s[i] = s[i-1]+s[i-3]; 12 } 13 printf("%d\n",s[n]); 14 } 15 }
2019 数列有序!
1 #include <stdio.h> 2 3 int main(){ 4 int s[101],c[101]; 5 int n,x,i,j; 6 while(scanf("%d %d",&n,&x)!=EOF){ 7 if(n==0&&x==0){break;} 8 for(i=0;i<n;i++){scanf("%d", &s[i]);} 9 for(i=0;i<n;i++){ 10 c[i] = s[i]; 11 if(s[i]<=x&&x<=s[i+1]){ 12 c[i+1] = x; 13 for(j=i+2;j<n+1;j++){c[j]=s[j-1];} 14 break; 15 16 } 17 } 18 for(i=0;i<n+1;i++){ 19 if(i==n){printf("%d\n",c[i]);} 20 else{printf("%d ",c[i]);} 21 } 22 } 23 }
2020 绝对值排序
1 #include <stdio.h> 2 int ab(int a){ 3 if(a<0){a=-a;} 4 return a; 5 } 6 7 int main(){ 8 int n,i,j,temp; 9 int s[101]; 10 while(scanf("%d",&n)!=EOF){ 11 if(n==0){break;} 12 for(i=0;i<n;i++){scanf("%d", &s[i]);} 13 for(i=0;i<n-1;i++){ 14 for(j=0;j<n-1-i;j++){ 15 if(ab(s[j]) < ab(s[j+1])){ 16 temp = s[j]; 17 s[j] = s[j+1]; 18 s[j+1] = temp; 19 } 20 } 21 } 22 for(i=0;i<n;i++){ 23 if(i==n-1){printf("%d\n",s[i]);} 24 else{printf("%d ",s[i]);} 25 } 26 } 27 }
2021 发工资咯:)
1 #include <stdio.h> 2 3 int main(){ 4 int s[101]; 5 int n,i,sum,temp,count; 6 while(scanf("%d",&n)!=EOF){ 7 if(n==0){break;} 8 count = 0; 9 for(i=0;i<n;i++){scanf("%d",&s[i]);} 10 for(i=0;i<n;i++){ 11 sum = 0; 12 if(s[i]/100!=0){sum += s[i]/100;} 13 if((s[i]%100)/50!=0){sum += (s[i]%100)/50;} 14 if(s[i]%100%50/10!=0){sum += s[i]%100%50/10;} 15 if(s[i]%100%50%10/5!=0){sum += s[i]%100%50%10/5;} 16 if(s[i]%100%50%10%5/2!=0){sum += s[i]%100%50%10%5/2;} 17 sum += s[i]%100%50%10%5%2; 18 count += sum; 19 } 20 printf("%d\n",count); 21 } 22 }