1 PTA-算法設計與分析 2 7-1 c++(g++) 3 #include<bits/stdc++.h> 4 using namespace std; 5 long max3(long a,long b,long c) 6 { if(a<b) a=b; 7 if(a>c) return a; 8 else return c; 9 } 10 long maxSubSum(int a[],int left,int right) 11 { int i,j; 12 long maxLeftSum,maxRightSum; 13 long maxLeftBorderSum,leftBorderSum; 14 long maxRightBorderSum,rightBorderSum; 15 if(left==right) 16 { if(a[left]>0) 17 return a[left]; 18 else 19 return 0; 20 } 21 int mid=(left+right)/2; 22 maxLeftSum=maxSubSum(a,left,mid); 23 maxRightSum=maxSubSum(a,mid+1,right); 24 maxLeftBorderSum=0,leftBorderSum=0; 25 for(i=mid;i>=left;i--) 26 { leftBorderSum+=a[i]; 27 if(leftBorderSum>maxLeftBorderSum) 28 maxLeftBorderSum=leftBorderSum; 29 } 30 maxRightBorderSum=0,rightBorderSum=0; 31 for(j=mid+1;j<=right;j++) 32 { rightBorderSum+=a[j]; 33 if(rightBorderSum>maxRightBorderSum) 34 maxRightBorderSum=rightBorderSum; 35 } 36 return max3(maxLeftSum,maxRightSum,maxLeftBorderSum+maxRightBorderSum); 37 } 38 39 int main() 40 { int n; 41 cin>>n; 42 int a[n]; 43 for(int i=0;i<n;i++) 44 { cin>>a[i]; 45 } 46 cout<<maxSubSum(a,0,n-1)<<endl; 47 } 48 7-2 c++(g++) 49 #include<bits/stdc++.h> 50 using namespace std; 51 52 int n; 53 int flag[10000000+1]; 54 int count(int n) { 55 int cnt = 0; 56 flag[2] = 1; 57 for (int i = 3; i <= n; ++i) { 58 flag[i++] = 1; // 奇數位 59 flag[i] = 0; // 偶數位直接過濾 60 } 61 for (int i = 3; i <= sqrt(n*1); ++i) { 62 if (flag[i] != 1) continue; 63 for (int j = i*i; j <= n; j += i) { 64 // 將倍數轉換爲加法 65 flag[j] = 0; 66 } 67 } 68 for (int i = 1; i <= n; ++i) 69 cnt += flag[i]; 70 return cnt; 71 } 72 int main() 73 { cin>>n; 74 cout<<count(n); 75 } 76 7-3 c++(g++) 77 #include <stdio.h> 78 #include <string.h> 79 int main() 80 { 81 char a[6],b[81]; 82 int i,j,k,m,n,c[6]={0}; 83 while(gets(a)&&a[0]!='#') 84 { 85 gets(b); 86 m=strlen(a); 87 n=strlen(b); 88 for(i=0;i<m;i++) 89 { 90 c[i]=0; 91 for(j=0;j<n;j++) 92 { 93 if(a[i]==b[j]) 94 c[i]++; 95 } 96 } 97 for(i=0;i<m;i++) 98 printf("%c %d\n",a[i],c[i]); 99 } 100 return 0; 101 } 102 103 7-4 c++(g++) 104 #include<stdio.h> 105 long long d[102]; 106 int main() 107 { 108 int T,n,i; 109 scanf("%d",&T); 110 for(i=0;i<102;i++) d[i]=0; 111 d[1]=0; 112 d[2]=1; 113 for(i=3;i<=100;i++) 114 d[i]=((i-1)*(d[i-1]%1000000007+d[i-2]%1000000007))%1000000007; 115 /* 116 right now: MOD=1000000007; 117 118 for(i=3;i<=100;i++) 119 { 120 a[i]=a[i-1]+a[i-2]; 121 a[i]%=MOD; 122 a[i]*=i-1; 123 a[i]%=MOD; 124 } 125 */ 126 while(T--) 127 { 128 129 scanf("%d",&n); 130 printf("%lld\n",d[n]); 131 } 132 return 0; 133 } 134 7-5 c++(g++) 135 136 #include"iostream" 137 #include"cstdio" 138 #include"cstring" 139 using namespace std; 140 const int maxn = 50; 141 142 int num[maxn + 1]; 143 int c1[maxn + 1], c2[maxn + 1]; 144 145 int main(){ 146 int T; 147 scanf("%d",&T); 148 while(T --){ 149 for(int i = 1;i <= 26;i ++) 150 scanf("%d", &num[i]); 151 memset(c1, 0, sizeof c1); 152 memset(c2, 0, sizeof c2); 153 for(int i = 0;i <= num[1];i ++) 154 c1[i] = 1; 155 for(int i = 2;i <= 26;i ++){ //共有26個多項式 156 if(num[i] == 0) continue; 157 for(int j = 0;j <= maxn;j ++){ //共有maxn+1項 158 for(int k = 0;k <= num[i] && j + k*i <= maxn;k ++) 159 c2[j + k*i] += c1[j]; 160 } 161 for(int j = 0;j <= maxn;j ++){ 162 c1[j] = c2[j]; 163 c2[j] = 0; 164 } 165 } 166 int sum = 0; 167 for(int j = 1;j <= maxn;j ++) 168 sum += c1[j]; 169 printf("%d\n",sum); 170 } 171 return 0; 172 } 173 7-6 c++(g++) 174 #include <iostream> 175 using namespace std; 176 const int MAXA=100; 177 int a[MAXA]; 178 179 int max(int x, int y) 180 { 181 if (x < y) 182 { 183 cout << "max(" << x << "," << y << ")=" << y << " "; 184 return y; 185 } 186 else 187 { 188 cout << "max(" << x << "," << y << ")=" << x << " "; 189 return x; 190 } 191 } 192 193 /** 194 * @brief Use digui to find the maxnumber of the array 195 * arraymax(a,n)=max(a[0],a[1]); n==2; 196 * arraymax(a,n)=max(arraymax(n-2),a[n-1]); other situation; 197 * 198 * @param a 199 * @param n sizeof(a)/sizeof(a[0]) 200 * 201 * @return the number_value of array 202 */ 203 int arraymax(int a[], int n) 204 { 205 int b; 206 if (n == 1) 207 return max(a[0], a[1]); 208 else if(n>1&&n<=30) 209 { b=arraymax(a,n-1); 210 return max(b, a[n]); 211 } 212 } 213 214 int main() 215 { 216 int n; 217 cin >> n; 218 for (int i = 0; i < n; i++) 219 { 220 cin >> a[i]; 221 } 222 cout << arraymax(a, n-1) << endl;//0~n-1 223 return 0; 224 } 225 7-7 c++(g++) 226 #include<iostream> 227 #include<algorithm> 228 using namespace std; 229 /** 230 * recursion && Division algorithm 231 */ 232 233 /** 234 * @brief recursion algroithm 235 * 236 * gcd(x,y)=y; x%y=0; 237 * gcd(x,y)=disp;if(x%y>0)gcd(x%y,min(x,y)) else gcd(max(x,y),min(x,y)); other sitation; 238 * 239 * @param x 240 * @param y 241 * @return the number_value 242 */ 243 244 int gcd(int x,int y) 245 { 246 // int a,a1,a2,a3,a4; 247 if(x%y==0) 248 { cout<<"gcd("<<x<<","<<y<<") "; 249 return y; 250 } 251 else 252 { 253 cout<<"gcd("<<x<<","<<y<<") "; 254 // a=x%y; 255 // a1=max(x,y); 256 // a2=min(x,y); 257 // a3=min(a,y); 258 // a4=min(x,a) 259 if(x/y>0) 260 return gcd(y,x%y); 261 else 262 return gcd(max(x,y),min(x,y)); 263 } 264 265 266 } 267 268 int main() 269 { 270 int x,y; 271 cin>>x;cin>>y; 272 cout<<gcd(x,y); 273 } 274 7-8 c++(g++) 275 #include <stdio.h> 276 277 void swap(int a[], int i, int j) 278 { 279 // 交換數組a中元素i與元素j的值 280 int tmp; 281 tmp = a[i]; 282 a[i] = a[j]; 283 a[j] = tmp; 284 } 285 286 int partition(int a[], int lo, int hi) 287 { 288 int i = lo-1, pivot = a[lo]; 289 swap(a, lo, hi); 290 while(lo<hi) 291 { 292 // 循環開始的時候i指向的是小於pivot的最後的一個位置 293 if(a[lo] < pivot) 294 { 295 i ++; 296 swap(a, i, lo); 297 } 298 lo ++; 299 } 300 i ++; 301 swap(a, i, hi); 302 return i; 303 } 304 305 int find(int a[], int left, int right, int k) 306 { 307 int index; 308 index = partition(a, left, right); 309 if(index == k) 310 return a[index]; 311 else if(index < k) 312 return find(a, index+1, right, k); 313 else 314 return find(a, left, right-1, k); 315 } 316 317 int main() 318 { 319 int a[10000]; 320 int n, k, i; 321 scanf("%d", &n); 322 scanf("%d", &k); 323 for(i=0; i<n; i++) 324 scanf("%d", &a[i]); 325 printf("%d\n", find(a, 0, n-1, k-1)); 326 } 327 7-9 c++(g++) 328 #include<bits/stdc++.h> 329 using namespace std; 330 331 void swap(int a,int b) 332 { int tmp; 333 if(a<b) 334 { tmp=a; 335 a=b; 336 b=tmp; 337 } 338 339 } 340 int num(int n,int a[]) 341 { int count=0; 342 for(int j=0;j<n-1;j++) 343 for(int i=0;i<=n-2;i++) 344 { if(a[i]>a[i+1]) 345 { swap(a[i],a[i+1]); 346 count++; 347 break; 348 } 349 } 350 return count; 351 } 352 // i==2 353 // 0 1 d 1 2 2083 s 2 3 2803 s 354 355 int main() 356 { int n; 357 cin>>n; 358 int a[n]; 359 for(int i=0;i<n;i++) 360 { cin>>a[i]; 361 } 362 cout<<num(n,a); 363 } 364 7-10 c++(g++) 365 #include<stdio.h> 366 #include<string.h> 367 int main() 368 { 369 int n,i,j,max=0;//max爲所用箱子的數目 370 int a[1005];//用來接收物品的 371 int b[1005];//即用來描述物品,又用來記錄裝物品的箱子(看完代碼後天然會理解) 372 int pox[10005];//箱子的位置 373 memset(pox,-1,sizeof(pox)); 374 scanf("%d",&n); 375 for(i=0;i<n;i++) 376 { 377 scanf("%d",&a[i]); 378 b[i]=a[i]; 379 } 380 pox[0]=0;//將箱子的位置初始化爲0,是爲了與數組的下標匹配,更方便標記位置 381 for(i=1;i<n;i++) 382 { 383 for(j=0;j<i;j++) 384 { 385 if(a[i]+b[j]<=100) 386 { 387 b[j]=b[j]+a[i];//將兩個物品放在一個箱子中 388 b[i]=0;//用於同步物品的信息 389 pox[i]=j; 390 break; 391 } 392 else 393 { 394 pox[i]=i; 395 } 396 } 397 } 398 for(i=0;i<n;i++) 399 { 400 if(pox[i]>max) 401 { 402 max=pox[i]; 403 } 404 } 405 for(i=0;i<n;i++) 406 { 407 printf("%d %d\n",a[i],pox[i]+1); 408 } 409 printf("%d",max+1); 410 return 0; 411 } 412 7-11 c++(g++) 413 #include <iostream> 414 using namespace std; 415 int a[1010]; 416 int main() 417 { 418 int n, k, i, h = 0, j = 0, b; 419 cin >> n >> k; 420 b = n; 421 for (i = 0; i <= k; i++) //共有k+1個數,由於它們都是表示一個性質的,因此能夠一塊兒處理 422 cin >> a[i]; 423 for (i = 0; i <= k; i++) 424 if (a[i] > n) 425 { 426 cout << "No Solution!\n"; //只要有一段路程大於n就沒法到達 427 j = 1; //此處標記 428 break; 429 } 430 // else 431 { 432 for (i = 1; i <= k; i++) 433 { 434 b -= a[i - 1]; 435 if (b < a[i]) 436 { 437 h++; 438 b = n; 439 } 440 } 441 } 442 if (!j) //利用標記法,要否則會錯過h=0時的狀況致使一個測試點沒法經過(5分) 443 cout << h << endl; 444 return 0; 445 } 446 7-12 c++(g++) 447 #include<bits/stdc++.h> 448 using namespace std; 449 void greedy(int n,int start[],int end[]); 450 int a[1000],b[1000]; 451 int main() 452 { 453 int n; 454 cin>>n; 455 for(int i=0;i<n;i++) 456 { 457 cin>>a[i]>>b[i]; 458 } 459 sort(a,a+n); //對開始時間進行上升排序 460 sort(b,b+n); //對結束時間進行上升排序 461 greedy(n,a,b); //調用函數求最小會場數 462 } 463 void greedy(int n,int start[],int end[]) 464 { 465 int j=0; 466 int count=0; 467 for(int i=0;i<n;i++) 468 { 469 if(start[i]<end[j]){ 470 count++;//若是開始時間小於結束時間則另開闢一個會場 471 }else{ 472 j++;//不然和下一個結束時間進行比較 473 } 474 } 475 printf("%d",count); 476 } 477 7-13 c++(g++) 478 #include <iostream> 479 #include <algorithm> 480 using namespace std; 481 482 int main() 483 { 484 int k; 485 cin>>k; 486 int a[k],b[k]; 487 for(int i=0;i<k;i++) 488 { 489 cin>>a[i]; 490 } 491 sort(a,a+k); 492 for(int i=k-1,j=0;i>=0;i--,j++) 493 { 494 b[j]=a[i]; 495 } 496 int sum1=0,sum2=0; 497 for(int i=0;i<k-1;i++) 498 { 499 a[i+1]=a[i]+a[i+1]; 500 sum1+=a[i+1]; 501 sort(a+i,a+k); 502 } 503 for(int j=0;j<k-1;j++) 504 { 505 b[j+1]=b[j]+b[j+1]; 506 sum2+=b[j+1]; 507 } 508 cout<<sum2-k+1<<" "; 509 cout<<sum1-k+1<<endl; 510 return 0; 511 } 512 7-14 c++(g++) 513 #include <iostream> 514 #include <cstdio> 515 #include <cstring> 516 #include <algorithm> 517 #include <set> 518 using namespace std; 519 typedef struct ab{ 520 int s; 521 int f; 522 }; 523 int cmp(ab a,ab b) 524 { 525 return a.f<b.f; 526 } 527 int main() 528 { 529 int n; 530 ab x[110]; 531 while(~scanf("%d",&n)) 532 { 533 if(n==0) 534 { 535 break; 536 } 537 for(int i=0;i<n;i++) 538 { 539 scanf("%d %d",&x[i].s,&x[i].f); 540 } 541 sort(x,x+n,cmp); 542 int sum=1; 543 int fina=x[0].f; 544 for(int i=1;i<n;i++) 545 { 546 if(x[i].s>=fina) 547 { 548 sum++; 549 fina=x[i].f; 550 } 551 } 552 printf("%d\n",sum); 553 } 554 return 0; 555 } 556 7-15 c++(g++) 557 #include<iostream> 558 559 using namespace std; 560 561 int a[12][12] = {0,1,0,0,0,1,1,1,0,1,0,1, 562 563 0,0,0,1,0,0,0,0,1,0,0,1, 564 565 0,1,0,1,0,1,1,1,0,1,0,0, 566 567 0,1,0,0,0,0,0,1,0,0,1,1, 568 569 0,0,0,0,1,0,0,0,0,0,0,0, 570 571 0,0,1,0,0,0,1,0,0,0,1,0, 572 573 0,0,1,0,0,0,0,0,1,0,0,0, 574 575 1,0,0,1,0,1,0,0,0,1,0,1, 576 577 0,0,1,0,1,0,1,0,1,0,0,0, 578 579 0,0,0,0,0,1,0,0,0,1,1,0, 580 581 0,0,0,0,0,1,0,0,0,0,0,0, 582 583 0,1,0,1,0,0,0,1,0,1,0,0}; 584 int x1, x2, y1, y2, min1 = 210000000, t = 0, b[12][12] = {0}, c[12][12] = {0}; 585 586 587 int ff(int x, int y, int k)//當前的位置,k是當前的步數; 588 { 589 int a1, a2, a3, a4; 590 if(x < 0 || y < 0 ||x >= 12 || y >= 12) 591 { 592 return 0; 593 } 594 else 595 { 596 597 if(x == x2 && y == y2) 598 { 599 t++; 600 if(min1 > k) min1 = k; 601 return 1; 602 } 603 else 604 { 605 if(k >= min1) return 1; 606 if(a[x][y] == 0) 607 { 608 if(b[x][y] == 0) 609 { 610 if(c[x][y] == 0) 611 { 612 b[x][y] = 1; 613 a1 = ff(x+1, y, k+1); 614 a2 = ff(x, y+1, k+1); 615 a3 = ff(x-1, y, k+1); 616 a4 = ff(x, y-1, k+1); 617 b[x][y] = 0; 618 if(a1 == 0&&a2 == 0&&a3 == 0&&a4 == 0) 619 { 620 c[x][y] = 1; 621 return 0; 622 } 623 else return 1; 624 } 625 return 0; 626 } 627 else return 1; 628 } 629 else 630 { 631 return 0; 632 } 633 } 634 } 635 } 636 637 int main() 638 { 639 int i, j; 640 cin>>x1>>y1>>x2>>y2; 641 if(a[x2][y2] == 1) cout<<"10000"; 642 else 643 { 644 ff(x1, y1, 1); 645 if(t == 0) cout<<"10000"; 646 else cout<<min1-1; 647 } 648 649 650 return 0; 651 } 652 7-16 c++(g++) 653 #include<iostream> 654 655 using namespace std; 656 657 int a[10000], n, t, b[10000] = {0}, z = 0; 658 int c[10000]; 659 void print(int k) 660 { 661 int i; 662 z++; 663 for(i = 1; i <= k; i++) 664 { 665 cout<<c[i]<<" "; 666 } 667 cout<<endl; 668 } 669 670 int ff() 671 { 672 int i = 1, sum = 0, j = 1; 673 while(i > 0) 674 { 675 if(b[i] == 0) 676 { 677 b[i] = 1; 678 sum += a[i]; 679 c[j] = a[i]; 680 j++; 681 i++; 682 if(sum == t) return j; 683 if(sum > t) 684 { 685 sum -= a[i-1]; 686 b[i-1] = 0; 687 j--; 688 } 689 if(i > n) 690 { 691 i = n - 1; 692 if(b[n] == 1) 693 { 694 sum -= a[n]; 695 b[n] = 0; 696 j--; 697 698 } 699 700 while(b[i] == 0) 701 { 702 i--; 703 if(i < 0) return 0; 704 } 705 j--; 706 sum -= a[i]; 707 b[i] = 0; 708 i++; 709 } 710 } 711 } 712 return 0; 713 } 714 715 int main() 716 { 717 int i, j, tol = 0; 718 cin>>n>>t; 719 for(i = 1; i <= n; i++) 720 { 721 cin>>a[i]; 722 tol += a[i]; 723 } 724 if(tol >= t) 725 { 726 j = ff(); 727 if(j) 728 { 729 print(j-1); 730 } 731 else cout<<"No Solution!"; 732 } 733 else cout<<"No Solution!"; 734 return 0; 735 } 736 7.17 c++(g++) 737 #include<iostream> 738 #include<cstdlib> 739 using namespace std; 740 741 742 int n, a[1000], x, b[100] = {0}, Min = 2100000000; 743 int compare(void const * q, void const * p) 744 { 745 return *(int *)q - *(int *)p; 746 } 747 748 int ff(int k) 749 { 750 int i, Max = 0, t; 751 if(k <= n) 752 { 753 for(i = 1; i <= x; i++) 754 { 755 if((b[i] + a[k]) > Min) 756 return 0; 757 b[i] += a[k]; 758 759 t = ff(k+1); 760 //if(t == 0) return 0; 761 b[i] -= a[k]; 762 } 763 } 764 if(k > n) 765 { 766 for(i = 1; i <= x; i++) 767 { 768 if(b[i] > Max) Max = b[i]; 769 } 770 if(Min > Max) Min = Max; 771 } 772 } 773 774 int main() 775 { 776 int i; 777 cin>>n>>x; 778 for(i = 1; i <= n; i++) 779 { 780 cin>>a[i]; 781 } 782 qsort(&a[1], n, sizeof(int), compare); 783 ff(1); 784 cout<<Min; 785 return 0; 786 } 787 7.18 c++(g++) 788 #include<iostream> 789 790 using namespace std; 791 792 int a[10005], n, t, b[10005] = {0}, z = 0; 793 794 void print(int k) 795 { 796 int i; 797 z++; 798 for(i = 1; i < k; i++) 799 { 800 if(i == 1) cout<<b[i]; 801 else cout<<" "<<b[i]; 802 } 803 cout<<endl; 804 } 805 806 void ff(int k, int w, int num)//k是前一個數的下表,w是當前要存入的下標 ,num是當前總數 807 { 808 int i; 809 if(num <= t) 810 { 811 if(num == t) 812 { 813 print(w); 814 } 815 else 816 { 817 for(i = k+1; i <= n; i++) 818 { 819 b[w] = a[i]; 820 ff(i, w+1, num+a[i]); 821 } 822 } 823 } 824 } 825 826 827 int main() 828 { 829 int i; 830 cin>>n; 831 for(i = 1; i <= n; i++) 832 cin>>a[i]; 833 cin>>t; 834 ff(0, 1, 0); 835 if(z == 0) cout<<"None"; 836 return 0; 837 } 838 7.19 c++(g++) 839 #include <stdio.h> 840 #define inf 0x3f3f3f3f 841 842 int n,ans; 843 int c[25][25]; 844 int vis[25]; 845 846 void dfs(int i,int sum)//i是行號 847 { 848 if(sum>ans) //剪枝 849 return ; 850 if(i==n+1 && sum<ans) 851 { 852 ans=sum; 853 return ; 854 } 855 for(int j=1;j<=n;j++) 856 { 857 if(!vis[j])//遍歷第i行 沒有被遍歷過列號j 的元素 858 { 859 vis[j]=1; 860 dfs(i+1,sum+c[i][j]); 861 vis[j]=0; 862 } 863 } 864 } 865 866 int main() 867 { 868 scanf("%d",&n); 869 for(int i=1;i<=n;i++) 870 for(int j=1;j<=n;j++) 871 scanf("%d",&c[i][j]); 872 ans=inf; 873 dfs(1,0); 874 printf("%d\n",ans); 875 return 0; 876 } 877 7-20 c++(g++) 878 #include<stdio.h> 879 #define MAX 100 880 #define max(x,y) ((x)>(y)?(x):(y)) 881 882 int dp[MAX]; 883 int ans = 0; 884 885 void solve(int a[],int n) 886 { 887 int i,j; 888 for(i=0;i<n;i++) 889 { 890 dp[i]=1; 891 for(j=0;j<i;j++) 892 { 893 if(a[i]>a[j]) 894 dp[i]=max(dp[i],dp[j]+1); 895 } 896 } 897 ans=dp[0]; 898 for(i=1;i<n;i++) 899 ans=max(ans,dp[i]); 900 } 901 902 int main() 903 { 904 int n; 905 scanf("%d",&n); 906 int a[n]; 907 for(int i=0;i<n;i++) 908 scanf("%d",&a[i]); 909 solve(a,n); 910 printf("%d",ans); 911 return 0; 912 } 913 7-21 c++(g++) 914 #include<stdio.h> 915 #include"cstdio" 916 #include"iostream" 917 #include"string.h" 918 #include"algorithm" 919 using namespace std; 920 char str[1001]; 921 int dp[1001][1001]; 922 int main() 923 { 924 925 memset(dp,0,sizeof(dp)); 926 char str1[1001]; 927 scanf("%s",str); 928 int len=strlen(str); 929 int j=len-1; 930 for(int i=0;i<len;i++) 931 str1[j--]=str[i]; 932 for(int i=1;i<=len;i++) 933 { 934 for(int j=1;j<=len;j++) 935 if(str[i-1]==str1[j-1]) 936 dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1); 937 else 938 dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 939 } 940 printf("%d\n",len-dp[len][len]); 941 } 942 7-22 c++(g++) 943 #include<bits/stdc++.h> 944 using namespace std; 945 long max3(long a,long b,long c) 946 { if(a<b) a=b; 947 if(a>c) return a; 948 else return c; 949 } 950 long maxSubSum(int a[],int left,int right) 951 { int i,j; 952 long maxLeftSum,maxRightSum; 953 long maxLeftBorderSum,leftBorderSum; 954 long maxRightBorderSum,rightBorderSum; 955 if(left==right) 956 { if(a[left]>0) 957 return a[left]; 958 else 959 return 0; 960 } 961 int mid=(left+right)/2; 962 maxLeftSum=maxSubSum(a,left,mid); 963 maxRightSum=maxSubSum(a,mid+1,right); 964 maxLeftBorderSum=0,leftBorderSum=0; 965 for(i=mid;i>=left;i--) 966 { leftBorderSum+=a[i]; 967 if(leftBorderSum>maxLeftBorderSum) 968 maxLeftBorderSum=leftBorderSum; 969 } 970 maxRightBorderSum=0,rightBorderSum=0; 971 for(j=mid+1;j<=right;j++) 972 { rightBorderSum+=a[j]; 973 if(rightBorderSum>maxRightBorderSum) 974 maxRightBorderSum=rightBorderSum; 975 } 976 return max3(maxLeftSum,maxRightSum,maxLeftBorderSum+maxRightBorderSum); 977 } 978 979 int main() 980 { int n; 981 cin>>n; 982 int a[n]; 983 for(int i=0;i<n;i++) 984 { cin>>a[i]; 985 } 986 cout<<maxSubSum(a,0,n-1)<<endl; 987 } 988 7-23 c++(g++) 989 #include<bits/stdc++.h> 990 using namespace std; 991 int main() { 992 int n, k; 993 while (~scanf("%d,%d", &n, &k)) { 994 int dp[200][200]; 995 int i, j; 996 for (i = 1; i <= n; i++) { 997 for (j = 1; j <= k; j++) { 998 if (j == 1 || i == 1) 999 dp[i][j] = 1; 1000 else if (j == i) { 1001 dp[i][j] = dp[i][j - 1] + 1; 1002 } 1003 else if (i < j) { 1004 dp[i][j] = dp[i][i]; 1005 } 1006 else if (i > j) { 1007 dp[i][j] = dp[i][j - 1] + dp[i - j][j]; 1008 } 1009 } 1010 } 1011 printf("%d\n", dp[n][k]); 1012 } 1013 return 0; 1014 } 1015 7-24 c++(g++) 1016 #include<iostream> 1017 using namespace std; 1018 1019 int f(int n) 1020 1021 { 1022 1023 int num1 = 1, num2 = 1, num3 = 0, i = 0; 1024 1025 if (n <= 1) 1026 1027 { 1028 1029 return num1; 1030 1031 } 1032 1033 for (i = 1; i < n; i++) 1034 1035 { 1036 1037 num3 = num1 + num2; 1038 1039 num1 = num2; 1040 1041 num2 = num3; 1042 1043 } 1044 1045 return num3; 1046 1047 } 1048 1049 1050 int main() 1051 { 1052 1053 int n,i; 1054 cin>>n; 1055 int a[n]; 1056 for(i=0;i<n;i++) 1057 cin>>a[i]; 1058 for(i=0;i<n;i++) 1059 cout<<f(a[i])<<endl; 1060 return 0; 1061 1062 } 1063 7-25 c++(g++) 1064 #include<stdio.h> 1065 int father[30001],num[30001],tmp,vis[30001],cnt[30001]; 1066 int Find(int x) 1067 { 1068 if(x != father[x]) 1069 father[x] = Find(father[x]); 1070 1071 return father[x]; 1072 } 1073 int main(void) 1074 { 1075 int N,M,flag,fcur,ans=0; 1076 scanf("%d%d",&N,&M); 1077 for(int i=0;i<=N;i++) 1078 father[i] = i; 1079 1080 for(int i = 1;i<=M;i++) 1081 { 1082 int cnt,lf; 1083 scanf("%d",&cnt); 1084 flag = 1; 1085 for(int j=1;j<=cnt;j++){ 1086 int cur; 1087 scanf("%d",&cur); 1088 if(num[cur]==0){ 1089 if(j==1){ 1090 num[cur] = cur; 1091 lf = father[cur]; 1092 } 1093 num[cur] = cur; 1094 father[cur] = lf; 1095 1096 } 1097 else { 1098 if(j==1) 1099 lf = Find(cur); 1100 int fcur = Find(cur); 1101 father[fcur] = lf; 1102 } 1103 } 1104 1105 } 1106 1107 for(int i=1;i<=N;i++){ 1108 1109 int fx = Find(i); 1110 cnt[fx]++; 1111 1112 if(ans < cnt[fx]) 1113 ans = cnt[fx]; 1114 1115 } 1116 1117 printf("%d\n",ans); 1118 1119 return 0; 1120 } 1121 7-26 c++(g++) 1122 #include<iostream> 1123 #include<cstring> 1124 using namespace std; 1125 typedef struct Node{ 1126 char sex; 1127 int fId; 1128 int mId; 1129 }Node; 1130 Node p[100001]; 1131 int flag,visited[100001]; 1132 void f(int a,int sum){ 1133 if(sum > 5 || a == -1 || a == 0)//若是代數大於5代,或者a==-1,說明都不在人世, 1134 //或者a==0說明沒有被賦值,直接返回 1135 return; 1136 visited[a]++;//記錄被訪問的次數 1137 if(visited[a] >= 2)//若是五代之內的同一我的被訪問超過2次,說明在五代以內有共同的祖先 1138 flag = 0; 1139 f(p[a].fId,sum + 1);//遞歸調用,a的父親,代數+1 1140 f(p[a].mId,sum + 1);//遞歸調用,a的母親,代數+1 1141 return; 1142 } 1143 int main(){ 1144 int n,selfId,fId,mId,k,a,b; 1145 char c; 1146 scanf("%d",&n); 1147 for(int i = 0;i < n;i++){ 1148 scanf("%d %c%d%d",&selfId,&c,&fId,&mId); 1149 p[selfId].sex = c;//記錄本身的性別 1150 p[selfId].fId = fId;//記錄父親的Id 1151 p[selfId].mId = mId; 1152 p[fId].sex = 'M';//記錄本身父親的性別,不然,可能會漏掉 1153 p[mId].sex = 'F';//記錄本身母親的性別,不然,可能會漏掉 1154 } 1155 scanf("%d",&k); 1156 while(k--){ 1157 flag = 1;//標記變量,默認爲1 1158 memset(visited,0,sizeof(visited));//每循環一次,必須進行一次初始化 1159 scanf("%d%d",&a,&b); 1160 if(p[a].sex == p[b].sex){ 1161 printf("Never Mind\n"); 1162 continue; 1163 } 1164 f(a,1);//以a爲起點,代數爲一代,進行尋找 1165 f(b,1);//以a爲起點,代數爲一代,進行尋找 1166 if(flag) 1167 printf("Yes\n"); 1168 else 1169 printf("No\n"); 1170 } 1171 return 0; 1172 } 1173 7-27 c++(g++) × 1174 1175 7-28 c++(g++) 1176 #include<bits/stdc++.h> 1177 using namespace std; 1178 const int MAXN = 1e5 + 5; 1179 int d[MAXN]; 1180 vector<int>edge[MAXN]; 1181 void dfs(int x,int o) 1182 { 1183 d[x]=o; 1184 for(int i=0;i<edge[x].size();i++) 1185 dfs(edge[x][i],o+1); 1186 } 1187 int main() 1188 { 1189 int n,k,x,s; 1190 cin>>n; 1191 s=(n+1)*n/2; 1192 for(int i=1;i<=n;i++) 1193 { 1194 cin>>k; 1195 while(k--) 1196 { 1197 cin>>x; 1198 s-=x; 1199 edge[i].push_back(x); 1200 } 1201 } 1202 dfs(s,1); 1203 s=0; 1204 for(int i=1;i<=n;i++) 1205 if(d[s]<d[i]) 1206 s=i; 1207 cout<<s<<endl; 1208 return 0; 1209 } 1210 7-29 c++(g++) 1211 #include <bits/stdc++.h> 1212 1213 using namespace std; 1214 const int maxn=500+10; 1215 const int INF=0x3f3f3f3f; 1216 int V,E,s,t,num=0; 1217 int cost[maxn][maxn]; 1218 int d[maxn]; 1219 int prev1[maxn]; 1220 int value[maxn]; 1221 int roadnum[maxn]; 1222 int amount[maxn]; 1223 bool used[maxn]; 1224 1225 1226 1227 void dijkstra(int s) 1228 { 1229 memset(d,INF,sizeof(d)); 1230 memset(used,false,sizeof(used)); 1231 memset(prev1,-1,sizeof(prev1)); 1232 memset(roadnum,0,sizeof(roadnum)); 1233 memset(amount,0,sizeof(amount)); 1234 d[s]=0; 1235 roadnum[s]=1; 1236 amount[s]=value[s]; 1237 1238 while(true) 1239 { 1240 int v=-1; 1241 for(int u=0;u<V;u++) 1242 { 1243 if(!used[u]&&(v==-1||d[u]<d[v])) v=u; 1244 } 1245 1246 if(v==-1) break; 1247 used[v]=true; 1248 1249 for(int u=0;u<V;u++) 1250 { 1251 1252 if(d[u]>d[v]+cost[v][u]) 1253 { 1254 d[u]=d[v]+cost[v][u]; 1255 amount[u]=amount[v]+value[u]; 1256 roadnum[u]=roadnum[v]; 1257 prev1[u]=v; 1258 //cout<<"gg1: "<<amount[u]<<" "<<u<<" "<<v<<endl; 1259 } 1260 else if(d[u]==d[v]+cost[v][u]) 1261 { 1262 roadnum[u]+=roadnum[v]; 1263 if(amount[u]<amount[v]+value[u]) 1264 { 1265 amount[u]=amount[v]+value[u]; 1266 prev1[u]=v; 1267 //cout<<"gg2: "<<amount[u]<<" "<<u<<" "<<v<<endl; 1268 1269 } 1270 1271 } 1272 1273 } 1274 } 1275 } 1276 1277 1278 int main() 1279 { 1280 scanf("%d %d %d %d",&V,&E,&s,&t); 1281 for(int i=0;i<V;i++) memset(cost[i],INF,sizeof(cost[i])); 1282 1283 for(int i=0;i<V;i++) scanf("%d",&value[i]); 1284 for(int i=0;i<E;i++) 1285 { 1286 int a,b,c; scanf("%d %d %d",&a,&b,&c); 1287 cost[a][b]=cost[b][a]=c; 1288 } 1289 dijkstra(s); 1290 1291 int prevt[maxn]; 1292 int pos=0; 1293 1294 int tt=t; 1295 for(;tt!=-1;tt=prev1[tt]) 1296 { 1297 prevt[pos++]=tt; 1298 } 1299 1300 printf("%d %d\n",roadnum[t],amount[t]); 1301 for(int i=pos-1;i>=0;i--) 1302 { 1303 if(i) printf("%d ",prevt[i]); 1304 else printf("%d",prevt[i]); 1305 } 1306 1307 return 0; 1308 } 1309 7-30 c++(g++) 1310 #include<iostream> 1311 #include<cstring> 1312 1313 1314 using namespace std; 1315 int f[550],book[550]; 1316 1317 struct edge{ 1318 int x,y; 1319 }; 1320 edge edges[6000]; 1321 int n,m,sum; 1322 int find(int x) 1323 { 1324 return f[x]<0?x:f[x]=find(f[x]); 1325 } 1326 void merge(int x,int y) 1327 { 1328 int rx=find(x); 1329 int ry=find(y); 1330 if(rx!=ry) 1331 { 1332 f[rx]+=f[ry]; 1333 f[ry]=rx; 1334 } 1335 } 1336 int main() 1337 { 1338 cin>>n>>m; 1339 memset(f,-1,sizeof(f)); 1340 for(int i=1;i<=m;i++) 1341 { 1342 int x,y;cin>>x>>y; 1343 edges[i]=edge{x,y}; 1344 merge(x,y); 1345 } 1346 for(int i=0;i<n;i++) 1347 { 1348 if(f[i]<0)sum++; 1349 } 1350 1351 1352 int k;cin>>k; 1353 1354 1355 for(int i=1;i<=k;i++) 1356 { 1357 int cnt=0; 1358 memset(f,-1,sizeof(f)); 1359 int x;cin>>x;book[x]=1; 1360 for(int j=1;j<=m;j++) 1361 { 1362 if(!book[edges[j].x]&&!book[edges[j].y]) 1363 { 1364 merge(edges[j].x,edges[j].y); 1365 } 1366 } 1367 for(int i=0;i<n;i++) 1368 { 1369 if(!book[i]&&f[i]<0)cnt++; 1370 } 1371 //sum=cnt; 1372 1373 if(cnt>sum) cout<<"Red Alert: City "<<x<<" is lost!"<<endl; 1374 else cout<<"City "<<x<<" is lost."<<endl; 1375 sum=cnt; 1376 if(i==n)cout<<"Game Over."<<endl; 1377 } 1378 return 0; 1379 } 1380 7-31 c++(g++) 1381 #include <bits/stdc++.h> 1382 using namespace std; 1383 typedef long long ll; 1384 const int INF=0x3f3f3f3f; 1385 const int maxn=1e3+10; 1386 struct node 1387 { 1388 int x,y; 1389 }a[maxn]; 1390 1391 int n,d; 1392 int di[maxn][maxn],via[maxn][maxn]; 1393 vector<int> apos; 1394 stack<int> first; 1395 1396 void DFS(int i,int j) 1397 { 1398 if(via[i][j]==-1) 1399 { 1400 if(i!=0) 1401 first.push(i); 1402 } 1403 else if(via[i][j]!=INF) 1404 { 1405 DFS(via[i][j],j); 1406 DFS(i,via[i][j]); 1407 } 1408 1409 return ; 1410 } 1411 1412 1413 int main() 1414 { 1415 scanf("%d %d",&n,&d); 1416 1417 if(d>=50) 1418 { 1419 printf("1"); 1420 return 0; 1421 } 1422 1423 for(int i=1;i<=n;i++) 1424 { 1425 scanf("%d %d",&a[i].x,&a[i].y); 1426 1427 if(abs(a[i].x)+d>=50||abs(a[i].y)+d>=50) 1428 { 1429 apos.push_back(i); 1430 } 1431 } 1432 a[0].x=a[0].y=0; 1433 1434 memset(di,INF,sizeof(di)); 1435 memset(via,INF,sizeof(via)); 1436 1437 for(int i=0;i<=n;i++) 1438 { 1439 di[i][i]=0; 1440 for(int j=i+1;j<=n;j++) 1441 { 1442 double x=0; 1443 if(i==0) x=7.5; 1444 1445 double tx=a[i].x-a[j].x; 1446 double ty=a[i].y-a[j].y; 1447 double td=d+x; 1448 1449 if(tx*tx+ty*ty<=td*td) 1450 { 1451 di[i][j]=di[j][i]=1; 1452 via[i][j]=via[j][i]=-1; 1453 } 1454 } 1455 } 1456 1457 for(int k=0;k<=n;k++) 1458 { 1459 for(int i=0;i<=n;i++) 1460 { 1461 if(di[i][k]==INF) continue; 1462 for(int j=0;j<=n;j++) 1463 { 1464 if(di[k][j]==INF) continue; 1465 if(di[i][j]>di[i][k]+di[k][j]) 1466 { 1467 di[i][j]=di[j][i]=di[i][k]+di[k][j]; 1468 via[i][j]=via[j][i]=k; 1469 } 1470 } 1471 } 1472 } 1473 1474 1475 int ans=INF; 1476 for(int i=0;i<apos.size();i++) 1477 ans=min(ans,di[0][apos[i]]); 1478 1479 if(ans==INF) 1480 { 1481 printf("0"); 1482 return 0; 1483 } 1484 1485 int pos=-1,dmin=INF; 1486 for(int i=0;i<apos.size();i++) 1487 { 1488 if(ans==di[0][apos[i]]) 1489 { 1490 DFS(0,apos[i]); 1491 int t=first.top(); 1492 double tx=a[t].x; 1493 double ty=a[t].y; 1494 1495 if(dmin>tx*tx+ty*ty) 1496 { 1497 pos=apos[i]; 1498 dmin=tx*tx+ty*ty; 1499 } 1500 1501 while(!first.empty()) first.pop(); 1502 } 1503 } 1504 1505 first.push(pos); 1506 DFS(0,pos); 1507 printf("%d\n",ans+1); 1508 1509 while(!first.empty()) 1510 { 1511 int t=first.top(); first.pop(); 1512 printf("%d %d\n",a[t].x,a[t].y); 1513 } 1514 1515 1516 return 0; 1517 } 1518 7-32 c++(g++) 1519 #include<iostream> 1520 #include<vector> 1521 #include<map> 1522 using namespace std; 1523 1524 const int INF = 0x3f3f3f3f; 1525 const int maxn = 200 + 10; 1526 map<string,int> mp; 1527 1528 int n, k, w; 1529 struct edge{ 1530 int to,cost; 1531 }; 1532 vector <edge> G[maxn]; 1533 int num[maxn]; 1534 1535 int dis[maxn]; 1536 int cnt[maxn]; 1537 int sumnum[maxn]; 1538 int pred[maxn]; 1539 int cntshort[maxn]; 1540 int book[maxn]; 1541 vector <int> ljd; 1542 int s, des; 1543 1544 void dijsk() 1545 { 1546 1547 for(int i = 0;i < n;i++) 1548 dis[i] = INF; 1549 1550 dis[s] = 0; 1551 cntshort[s] = 1; 1552 book[s] = 1; 1553 1554 for(int i = 0;i < G[s].size();i++) 1555 { 1556 int next = G[s][i].to; 1557 dis[next] = G[s][i].cost; 1558 1559 cntshort[next] = 1; 1560 sumnum[next] = num[next]; 1561 pred[next] = s; 1562 } 1563 1564 int min, minindex; 1565 for(int i = 1;i <= n - 1;i++) 1566 { 1567 min = INF; 1568 minindex = 0; 1569 for(int i = 0;i < n;i++) 1570 { 1571 if(dis[i] < min && book[i] != 1) 1572 { 1573 min = dis[i]; 1574 minindex = i; 1575 } 1576 } 1577 book[minindex] = 1; 1578 1579 for(int i = 0;i < G[minindex].size();i++) 1580 { 1581 edge& e = G[minindex][i]; 1582 int d2 = dis[minindex] + e.cost; 1583 1584 if(dis[e.to] > d2) 1585 { 1586 dis[e.to] = d2; 1587 pred[e.to] = minindex; 1588 cnt[e.to] = cnt[minindex] + 1; 1589 sumnum[e.to] = sumnum[minindex] + num[e.to]; 1590 cntshort[e.to] = cntshort[minindex]; 1591 } 1592 else if(dis[e.to] == d2) 1593 { 1594 cntshort[e.to] += cntshort[minindex]; 1595 if(cnt[e.to] < cnt[minindex] + 1) 1596 { 1597 1598 pred[e.to] = minindex; 1599 cnt[e.to] = cnt[minindex] + 1; 1600 sumnum[e.to] = sumnum[minindex] + num[e.to]; 1601 } 1602 else if(cnt[e.to] == cnt[minindex] + 1) 1603 { 1604 1605 if(sumnum[e.to] < sumnum[minindex] + num[e.to]) 1606 { 1607 sumnum[e.to] = sumnum[minindex] + num[e.to]; 1608 pred[e.to] = minindex; 1609 } 1610 } 1611 } 1612 } 1613 1614 } 1615 } 1616 1617 void dfs(int *p, int x,vector <int>& a) 1618 { 1619 1620 if(x == s) 1621 { 1622 a.push_back(x); 1623 return ; 1624 } 1625 dfs(p, p[x], a); 1626 a.push_back(x); 1627 } 1628 1629 int main() 1630 { 1631 string city[maxn]; 1632 string city1, city2; 1633 cin >> n >> k >> city1 >> city2; 1634 1635 city[0] = city1; 1636 mp[city1] = 0; 1637 1638 1639 for(int i = 1;i < n;i++) 1640 { 1641 cin >> city[i] >> w; 1642 mp[city[i]] = i; 1643 num[i] = w; 1644 } 1645 1646 s = 0; 1647 des = mp[city2]; 1648 1649 int u, v; 1650 1651 for(int i = 0;i < k;i++) 1652 { 1653 cin >> city1 >> city2 >> w; 1654 u = mp[city1]; v = mp[city2]; 1655 1656 G[u].push_back((edge){v, w}); 1657 G[v].push_back((edge){u, w}); 1658 } 1659 1660 dijsk(); 1661 dfs(pred, des, ljd); 1662 cout<<city[0]; 1663 for(int i = 1;i<ljd.size();++i){ 1664 cout<<"->"<<city[ljd[i]]; 1665 } 1666 printf("\n%d %d %d",cntshort[des],dis[des],sumnum[des]); 1667 return 0; 1668 } 1669 7-33 c++(g++) 1670 #include <stdio.h> 1671 #define MAXN 100 1672 1673 typedef struct 1674 { 1675 int arcs[MAXN][MAXN]; 1676 int vexnum,arcnum; 1677 }AMGraph; 1678 1679 int main() 1680 { 1681 int n; 1682 AMGraph G; 1683 1684 scanf("%d %d %d",&G.vexnum, &n, &G.arcnum ); 1685 int safecity[MAXN]; 1686 for(int i=0;i<n;i++) 1687 { 1688 scanf("%d",&safecity[i]); 1689 } 1690 for(int i=0;i<G.vexnum;i++) 1691 { 1692 for(int j=0;j<G.vexnum;j++) 1693 { 1694 G.arcs[i][j]=0; 1695 } 1696 } 1697 for(int i=0;i<G.arcnum;i++) 1698 { 1699 int x,y; 1700 scanf("%d %d",&x,&y); 1701 G.arcs[x][y]=1; 1702 G.arcs[y][x]=1; 1703 } 1704 int now,destination,flag=0; 1705 scanf("%d %d",&now,&destination); 1706 for(int i=0;i<n;i++) 1707 { 1708 if(destination==safecity[i]) 1709 { 1710 flag=1; 1711 break; 1712 } 1713 } 1714 if(G.arcs[now][destination]==1) 1715 { 1716 if(flag) 1717 printf("The city %d can arrive safely!\n",destination); 1718 else 1719 { 1720 printf("The city %d is not safe!\n",destination); 1721 1722 } 1723 } 1724 else 1725 { 1726 if(flag) 1727 printf("The city %d can not arrive safely!\n",destination); 1728 else printf("The city %d is not safe!\n",destination); 1729 } 1730 return 0; 1731 } 1732 7-34 c++(g++) 1733 #include<stdio.h> 1734 #include<string.h> 1735 #include<algorithm> 1736 #include<queue> 1737 #include<math.h> 1738 using namespace std; 1739 const int maxm = 105; 1740 const int maxn = 5005; 1741 const int INF = 1000009; 1742 int flag[maxm][maxm], key[maxm*maxm], dis[maxn][maxm]; 1743 int id[maxm][maxm], vis[maxm*maxm], head[maxm*maxm], map[maxm][maxm]; 1744 int dx[4] = { 0,0,1,-1 }; 1745 int dy[4] = { 1,-1,0,0 }; 1746 struct node 1747 { 1748 int v, w, next; 1749 }edge[maxm*maxm * 2]; 1750 struct point 1751 { 1752 int x, v; 1753 point(int a, int b) :x(a), v(b) {} 1754 point() {} 1755 }; 1756 int n, m, s, t, cnt; 1757 void init() 1758 { 1759 cnt = 0, s = 1, t = n*m; 1760 memset(head, -1, sizeof(head)); 1761 memset(map, -1, sizeof(map)); 1762 memset(key, 0, sizeof(key)); 1763 } 1764 void add(int u, int v, int w) 1765 { 1766 edge[cnt].v = v, edge[cnt].w = w; 1767 edge[cnt].next = head[u], head[u] = cnt++; 1768 } 1769 int bfs() 1770 { 1771 queue<point>q; 1772 memset(dis, 0x3f, sizeof(dis)); 1773 dis[1][s] = 0; 1774 q.push(point(1, s)); 1775 while (!q.empty()) 1776 { 1777 point now = q.front();q.pop(); 1778 int u = now.v; 1779 dis[now.x | key[u]][u] = min(dis[now.x | key[u]][u], dis[now.x][u]); 1780 now.x |= key[u]; 1781 for (int i = head[u];i != -1;i = edge[i].next) 1782 { 1783 int v = edge[i].v; 1784 if ((now.x&edge[i].w) == edge[i].w) 1785 { 1786 if (dis[now.x][v] > dis[now.x][u] + 1) 1787 { 1788 dis[now.x][v] = dis[now.x][u] + 1; 1789 q.push(point(now.x, v)); 1790 } 1791 } 1792 } 1793 } 1794 int ans = INF; 1795 for (int i = 1;i < maxn;i++) 1796 ans = min(ans, dis[i][t]); 1797 if (ans == INF) return -1; 1798 return ans; 1799 } 1800 int main() 1801 { 1802 int i, j, k, sum, p, len = 0; 1803 int x1, x2, y1, y2, z, u, v; 1804 scanf("%d%d%d%d", &n, &m, &p, &k); 1805 init(); 1806 for (i = 1;i <= n;i++) 1807 for (j = 1;j <= m;j++) 1808 id[i][j] = ++len; 1809 for (i = 1;i <= k;i++) 1810 { 1811 scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &z); 1812 u = id[x1][y1], v = id[x2][y2]; 1813 map[u][v] = map[v][u] = z; 1814 if (z) add(u, v, 1 << z), add(v, u, 1 << z); 1815 } 1816 scanf("%d", &k); 1817 for (i = 1;i <= k;i++) 1818 { 1819 scanf("%d%d%d", &x1, &y1, &z); 1820 key[id[x1][y1]] |= 1 << z; 1821 } 1822 for (i = 1;i <= n;i++) 1823 { 1824 for (j = 1;j <= m;j++) 1825 { 1826 u = id[i][j]; 1827 for (k = 0;k < 4;k++) 1828 { 1829 int x = i + dx[k]; 1830 int y = j + dy[k]; 1831 v = id[x][y]; 1832 if (map[u][v] == -1 && id[x][y]) 1833 add(u, v, 1); 1834 } 1835 } 1836 } 1837 printf("%d\n", bfs()); 1838 return 0; 1839 } 1840 7-35 c++(g++) × 1841 1842 7-36 c++(g++) × 1843 1844 7-37 c++(g++) 1845 #include <stdio.h> 1846 #include <string.h> 1847 int main() 1848 { 1849 char str[20][10],t[20],str1[10]; 1850 int i,j,n=0; 1851 while(1) 1852 { 1853 scanf("%s",str1); 1854 if(str1[0]=='#') 1855 { 1856 break; 1857 } 1858 else 1859 { 1860 strcpy(str[n],str1); 1861 n++; 1862 } 1863 } 1864 for(i=0;i<n-1;i++) 1865 for(j=0;j<n-i-1;j++) 1866 { 1867 if(strlen(str[j])>strlen(str[j+1])) 1868 { 1869 strcpy(t,str[j]); 1870 strcpy(str[j],str[j+1]); 1871 strcpy(str[j+1],t); 1872 } 1873 } 1874 for(i=0;i<n;i++) 1875 { 1876 printf("%s ",str[i]); 1877 } 1878 return 0; 1879 } 1880 7-38 c++(g++) 1881 #include<iostream> 1882 #include<algorithm> 1883 #include<cstring> 1884 #include<climits> 1885 using namespace std; 1886 bool cmp(double a,double b){ 1887 return a > b; 1888 } 1889 int main(){ 1890 int n,k,m,a,mx,mn,kx; 1891 double sum,arr[10001]; 1892 scanf("%d%d%d",&n,&k,&m); 1893 memset(arr,0,sizeof(arr)); 1894 kx = 0; 1895 for(int i = 0;i < n;i++){ 1896 mx = INT_MIN,mn = INT_MAX,sum = 0; 1897 for(int d = 0;d < k;d++){ 1898 scanf("%d",&a); 1899 mx = max(a,mx); 1900 mn = min(a,mn); 1901 sum += a; 1902 } 1903 sum = sum - mx - mn; 1904 arr[kx++] = sum / (k - 2); 1905 } 1906 sort(arr,arr + kx,cmp); 1907 for(int i = m - 1;i >= 0;i--){ 1908 if(i) 1909 printf("%.3lf ",arr[i]); 1910 else 1911 printf("%.3lf\n",arr[i]); 1912 } 1913 return 0; 1914 } 1915 1916 7-39 c++(g++) 1917 #include <iostream> 1918 using namespace std; 1919 int main() 1920 { 1921 int n,i,j,k,p; 1922 int a[100][100]; 1923 cin>>n; 1924 for(i=1;i<=n;i++) 1925 { 1926 a[i][i]=0; 1927 } 1928 for(i=1;i<=n;i++) 1929 { 1930 for(j=i+1;j<=n;j++) 1931 { 1932 cin>>a[i][j]; 1933 } 1934 } 1935 for(i=2;i<=n;i++) 1936 { 1937 for(j=i+1;j<=n;j++) 1938 { 1939 k=j-i; 1940 for(p=k;p<j;p++) 1941 { 1942 if(a[k][j]>a[k][p]+a[p][j]) 1943 a[k][j]=a[k][p]+a[p][j]; 1944 } 1945 } 1946 } 1947 cout<<a[1][n]<<endl; 1948 return 0; 1949 } 1950 7-40 c++(g++) 1951 #include<iostream> 1952 #include<queue> 1953 using namespace std; 1954 int main() 1955 { 1956 int n,m; 1957 priority_queue< int, vector<int>, greater<int> > q; 1958 cin>>n; 1959 for(int i=0;i<n;i++) 1960 { 1961 cin>>m; 1962 q.push(m); 1963 } 1964 int sum =0; 1965 while(!q.empty()) 1966 { 1967 int x = q.top(); 1968 q.pop(); 1969 if(q.empty()) 1970 break; 1971 int y = q.top(); 1972 q.pop(); 1973 x+=y; 1974 sum+=x; 1975 q.push(x); 1976 } 1977 cout<<sum<<endl; 1978 return 0; 1979 }