码迷,mamicode.com
首页 > 其他好文 > 详细

Volume 1. Elementary Problem Solving :: Sorting/SearchingUva 340,10420,10474,152,299,120,156,400,755

时间:2014-08-17 10:28:12      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:uva 刘汝佳 算法入门   algorithm   

刘汝佳 算法入门 第一版 Uva题目集合(四)




bubuko.com,布布扣

Uva 340

#include<stdio.h>
#include<string.h>
int h[1001][2],g[1001]={0};
int n,m=0,i,j,k,a,b,o;
int main()
{
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    scanf("%d",&n);
    while (n != 0 ){
          m++;
          memset(h,0,sizeof(h));
          printf("Game %d:\n",m);
          for (i=1;i<=n;i++) scanf("%d",&h[i][1]);
          while (o!=n){
                o = 0;
                memset(g,0,sizeof(g));
                for (i=1;i<=n;i++) {
                   scanf("%d",&g[i]); 
                    if (g[i]==0) o++;
                    h[i][2]=0;
                }
                if (o != n){
                      a = 0; b = 0;
                      for (i=1;i<=n;i++){
                          if ( g[i]==h[i][1]) {
                               a++;
                               h[i][2]++;
                          }
                      }
                      for (i=1;i<=n;i++)
                      if (g[i] != h[i][1]){
                               for (k=1;k<=n;k++)
                               if ( g[i]==h[k][1] && h[k][2]==0){
                                    b++;
                                    h[k][2]++;
				    break;
                               }
                      }         
                      printf("    (%d,%d)\n",a,b);
                }      
          }
		  o = 0;
          scanf("%d",&n); 
    }
    return 0;
}
/*
#include<stdio.h>
#include<string.h>
int main()
{
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    int n,i,j,a[1001],b[1001],aa[10],bb[10],now=0;
    while (scanf("%d",&n)==1)
    {
          if (n==0) break;
          printf("Game %d:\n",++now);
          for (i=1;i<=n;i++) scanf("%d",&a[i]);
          while (true)
          {
                for (i=1;i<=n;i++) scanf("%d",&b[i]);
                if (b[1]==0) break;
                int x=0,y=0;
                memset(aa,0,sizeof(aa));
                memset(bb,0,sizeof(bb));
                for (i=1;i<=n;i++) if (a[i]==b[i]) x++; else {aa[a[i]]++;bb[b[i]]++;}
                for (i=1;i<=9;i++) if (aa[i]<bb[i]) y+=aa[i]; else y+=bb[i];
                printf("    (%d,%d)\n",x,y);
          }
    }
    return 0;
}
*/ 


Uva 10420

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int cmp(const void *a, const void *b) {
    char *_a = (char *)a;
    char *_b = (char *)b;

    return strcmp(_a, _b);
}

int main() {
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    char str[2005][80];
    int n;

    scanf("%d", &n);

    for (int i=0; i<n; i++) {
        scanf("%s", str[i]);
        gets(str[i+1]); // 把女人姓名忽略掉
    }

    qsort(str, n, sizeof (str[0]), cmp);

    int tmp = 1;
    for (int i=1; i<=n; i++) {
        if (0==strcmp(str[i], str[i-1]) && i<n) {
            tmp++;
        }
        else {
            printf("%s %d\n", str[i-1], tmp);
            tmp = 1;
        }
    }

    return 0;
}


Uva 10474

#include<stdio.h>
int main(){
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    int n,m,test=0;
    while (scanf("%d%d",&n,&m)==2){
          if (n==0)break;
          printf("CASE# %d:\n",++test);
          int i,x,a[10001]={0},b[10001]={0};
          for (i=1;i<=n;i++) {scanf("%d",&x);a[x]++;}
          for (i=1;i<=10000;i++) b[i]=b[i-1]+a[i];
          for (i=1;i<=m;i++){
              scanf("%d",&x);
              if (a[x]==0) printf("%d not found\n",x);
              else printf("%d found at %d\n",x,b[x-1]+1);
          }
    }
    return 0;
}


Uva152

#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
int main(){
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    int i,j,s,s1,x,y,z,n=0;
    int count[10]={0};
    int scan[5010][3];
    while(cin>>scan[n][0]>>scan[n][1]>>scan[n][2]){
        if(!scan[n][0]&&!scan[n][1]&&!scan[n][2])break;
        ++n;
    }
    for(i=0;i<n;i++){
        s=5010;
        for(j=0;j<n;j++){
            if(j!=i){
                x=scan[i][0]-scan[j][0];
                y=scan[i][1]-scan[j][1];
                z=scan[i][2]-scan[j][2];
                s1=(int)sqrt(x*x+y*y+z*z);
                if(s>s1)s=s1;
            }
        }
        if(s>=10)continue;
        count[s]++;
    }
    for(i=0;i<10;i++)
        cout<<setw(4)<<count[i];
    cout<<endl;
    return 0;
}


Uva 299

#include<stdio.h>
int main()
{
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    int a[51],n,i,j,w,min,test,ans;
    scanf("%d",&test);
    while (test--){
          ans=0;
          scanf("%d",&n);
          for (i=1;i<=n;i++) scanf("%d",&a[i]);
          for (i=1;i<n;i++){
              min=2000000000;
              for (j=i;j<=n;j++)
                  if (a[j]<min) {min=a[j];w=j;}
              for (j=w;j>=i+1;j--) a[j]=a[j-1];
              a[i]=min;
              ans+=w-i;
          }
          printf("Optimal train swapping takes %d swaps.\n",ans);
    }
    return 0;
}
/*
#include<stdio.h>
#include<string.h>
int h[10000];
int n,m,k,i,j,a,b;
int main()
{
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    scanf("%d",&n);
    for (k=1;k<=n;k++)
    {
        memset(h,0,sizeof(h));
        a = 0;
        scanf("%d",&m);
        if (m != 0)
        {
              for (i=1;i<=m;i++) scanf("%d",&h[i]);
 
              for (i=1;i<m;i++)
               for (j=i+1;j<=m;j++)
                if ( h[i]>h[j] )
                {
                     b=h[i]; h[i]=h[j]; h[j]=b;
                     a++;
                }
        }
        printf("Optimal train swapping takes %d swaps.\n",a);
    }
    return 0;
}
*/ 


Uva 120

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
 
using namespace std;
 
int Num[31],s[31];
int ans[10001];
 void swap(int n,int *a){
 	for(int f=0;f<n/2;f++){
 			int temp = a[f];
			a[f] = a[n-f-1];
			a[n-f-1] = temp;
 	}
 } 
bool cmp(const int ta, const int tb){
	return ta>tb;
}
int main(){
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
	int a,i=0,j,k,f; 
	int ct = 0,temp;
	char ch;
	while(scanf("%d%c",&a,&ch)!=EOF){
		s[i] = Num[i] = a;
		i++; 
		printf("%d ",a);
		if(ch == '\n'){
			printf("\n");
			sort(Num,Num+i,cmp);
			for(j = 0; j<i; j++){
				for(k = 0; k<i; k++){
					if(s[k] == Num[j] && k!=i-1-j){
						if(k == 0){
							ans[ct++] = j+1;
							swap(i-j,s);
						}
						else {
							ans[ct++] = i-k;
							swap(k+1,s);
							ans[ct++] = j+1;
							swap(i-j,s);
						}
					}
				}
			}
			j = ans[ct] = 0;
			i = ct = 0;
			while(ans[j]){
				printf("%d ",ans[j]);
				j++;
			}
			printf("0\n");
		}
	}
	return 0;
}/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int cmp(const void *_a, const void *_b) {
    int* a = (int *)_a;
    int* b = (int *)_b;

    return *a - *b;
}

void swap(int a[], int i, int j) {
    while (i < j) {
        int tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
        i++;
        j--;
    }
}

int main() {

    int a[35], b[35], n;


    while (scanf("%d", &a[0]) != EOF) {
        // 接收输入的数字
        n = 1;
        if (getchar() != '\n')
            while (scanf("%d", &a[n++]))
                if (getchar() == '\n')  break;

        // 输出原先的序列并复制一份给b数组
        for (int i=0; i<n; i++) {
            b[i] = a[i];
            printf("%d ", a[i]);
        }
        printf("\n");

        // 对b数组进行排序
        qsort(b, n, sizeof (int), cmp);

        // 从后往前遍历a数组
        for (int i=n-1; i>=0; i--) {
            // 如果a[i]的值在排好序的位置就不执行下面的for循环
            if (a[i] == b[i])   continue;
            for (int j=i-1; j>=0; j--) {
                // 如果a[i]的值与位置不符就从前面找到该位置的数
                if (b[i] == a[j]) {
                    // 如果该数在第0个位置就直接交换
                    if (j == 0) {
                        printf("%d ", n - i);
                        swap(a, 0, i);
                    }
                    // 如果不在第0个位置就先交换到第0位置再交换到相应位置
                    else {
                        printf("%d ", n - j);
                        swap(a, 0, j);
                        printf("%d ", n - i);
                        swap(a, 0, i);
                    }
                }
            }
        }

        printf("0\n");
    }

    return 0;
}
*/


Uva 156

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
using namespace std;
struct pack
{
       char dat[25], r[25];
       pack()
       {
           memset(dat, '\0', sizeof(dat));
           memset(r, '\0', sizeof(r));
       }
 
       bool operator < (pack const y) const
       {
            return strcmp(dat, y.dat) == -1;
       }
};
pack a[1005];
char store[1005][25];
bool checked[1005];
int sort_function( const void *a, const void *b){
   return( strcmp((char *)a,(char *)b) );
}
int main(){
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
   int i, j, k = 0, n = 0, ck;
   while(cin >> a[k].dat && strcmp(a[k].dat,"#")){
     for(i = 0; i < strlen(a[k].dat); i++)
        a[k].r[i] = tolower(a[k].dat[i]);
     sort(a[k].r, a[k].r + strlen(a[k].dat));
     k++;
   }
   //sort(a, a + k);//显然这句话可以不加
   for(i = 0; i < k; i++){
      if(checked[i])  
        continue ;
      for(j = i + 1; j < k; j++)
         if(strcmp(a[i].r, a[j].r) == 0)
           checked[i] = checked[j] = 1;
      if(!checked[i])
        strcpy(store[n++], a[i].dat);
   }
   qsort((void *)store, n, 25, sort_function);
   for(i = 0; i < n; i++)
      cout << store[i] << endl;
   return 0;
} 


Uva 400

#include<iostream>
#include<string>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    int n, i, j, leng, column, row;
    string a[101];
    while(cin >> n){
         leng = -1;
         for(i = 0; i < n; i++){
            cin >> a[i];
            leng = max((int)a[i].length(), leng);
         }
         column = 60/(leng + 2);
         if(column == 0)
           column = 1;
         row = (int)n/column;
         if(n%column)
           row++;
         sort(a, a + n);
         cout << "------------------------------------------------------------" << endl;
         for(i = 0; i < row; i++) {
            for(j = 0; j < column; j++){
               if(j*row + i >= n)
                 break;
               if(j != column - 1)
                 cout << setw(leng + 2) << setiosflags(ios::left) << a[j*row + i];
               else
                   cout << setw(leng) << setiosflags(ios::left) << a[j*row + i];
            }
            cout << endl;
         }
    }
    return 0;
}


Uva 755

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char m[]="22233344455566677778889999";
char p[100000][9],buf[1000];
int i,j,k,count,duplicate;
int cmp(const void *a,const void *b){
    char *x,*y;
    x=(char*)a;
    y=(char*)b;
    return strcmp(x,y);
}
int main(){
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    int n;
    int kcase;
    scanf("%d",&kcase);
    while(kcase--){
	    scanf("%d\n",&n);
	    for(i=0;i<n&&gets(buf);i++){
	
	       for(k=j=0;buf[j];++j){
	            if(buf[j]=='-') continue;
	            if(buf[j]>='A'&&buf[j]<='Z'){
	                p[i][k]=m[buf[j]-'A'];
	            }
	            else if(buf[j]>='0'&&buf[j]<='9'){
	                p[i][k]=buf[j];
	            }
	            if(++k==3) p[i][k++]='-';
	        }
	     }
	     qsort(p,n,9,cmp);
	     duplicate=0;
	     count=0;
	     for(i=0;++i<=n;){
	         ++count;
	         if(strcmp(p[i-1],p[i])){
	             if(count>1){
	                 printf("%s %d\n",p[i-1],count);
	                 duplicate=1;
	             }
	             count=0;
	         }
	     }
	     if(duplicate==0)
	        puts("No duplicates.");
		if(kcase) printf("\n");
	}
     return 0; 
}  



Volume 1. Elementary Problem Solving :: Sorting/SearchingUva 340,10420,10474,152,299,120,156,400,755,布布扣,bubuko.com

Volume 1. Elementary Problem Solving :: Sorting/SearchingUva 340,10420,10474,152,299,120,156,400,755

标签:uva 刘汝佳 算法入门   algorithm   

原文地址:http://blog.csdn.net/u014767460/article/details/38636667

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!