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

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

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

时间: 2024-12-25 02:50:46

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

General Problem Solving Techniques [Intermediate-1]~A - Children&#39;s Game

There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be given N positive integer. (S)He can make a big integer by appending those in

hdu 5427 A problem of sorting

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5427 A problem of sorting Description There are many people's name and birth in a list.Your task is to print the name from young to old.(There is no pair of two has the same age.) Input First line contai

A problem of sorting

A problem of sorting 问题描述 给出一张许多人的年龄和生日表.你需要从年轻到年老输出人们的名字.(没有人年龄相同) 输入描述 第一行包含一个正整数T(T \leq 5)T(T≤5),表示数据组数. 对于每组数据,第一行包含一个正整数n(1 \leq n \leq 100)n(1≤n≤100),表示人数,接下来n行,每 行包含一个姓名和生日年份(1900-2015),用一个空格隔开.姓名长度大于0且不大于100.注意姓名中只包含字母, 数字和空格. 输出描述 对于每组数据,输出

bc #54 A problem of sorting

A problem of sorting Accepts: 445 Submissions: 1706 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 给出一张许多人的年龄和生日表.你需要从年轻到年老输出人们的名字.(没有人年龄相同) 输入描述 第一行包含一个正整数T(T \leq 5)T(T≤5),表示数据组数. 对于每组数据,第一行包含一个正整数n(1 \leq n \

【POJ 3265】Problem Solving

Problem Solving Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1645   Accepted: 675 Description In easier times, Farmer John's cows had no problems. These days, though, they have problems, lots of problems; they have P (1 ≤ P ≤ 300) pro

【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

[BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地说,他们有P (1 <= P <= 300) 道题目要做. 他们还离开了农场并且象普通人一样找到了工作. 他们的月薪是M (1 <= M <= 1000) 元. 他们的题目是一流的难题,所以他们得找帮手.帮手们不是免费的,但是他们能保证在一个月内作出任何题目.每做一道题需要两比付款,

poj3265(Problem Solving) dp

/*hdr ** Copyright ... ** AUTHOR MichaelMa ** DATE 4-May-2014 ** DESCRIPTION implement a mode of observer in C ** NOTE may be some bugs */ #include <stdio.h> #include <stdlib.h> #include <string.h> /***************************DEFINES****

HDU 5427 A problem of sorting 水题

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5427 A problem of sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1447    Accepted Submission(s): 554 Problem Description There are many p

ENGG1110 — Problem Solving

ENGG1110 — Problem Solving by Programming2019–2020 Term 1Project Specification — A Vending Machine Simulator1. IntroductionVending machines are automated machines that provide different products to consumers afterpayment is accepted. For simplicity,