天天看點

Volume 0. Getting Started Uva10055, 10071,10300,458,494,490,414,445,488,489,694,457Uva 10055Uva 10071Uva 10300Uva 458Uva 494Uva 414Uva 490Uva 445Uva 488Uva 489Uva 694Uva 457

劉汝佳 算法入門 第一版 Uva題目集合(一)

Volume 0. Getting Started Uva10055, 10071,10300,458,494,490,414,445,488,489,694,457Uva 10055Uva 10071Uva 10300Uva 458Uva 494Uva 414Uva 490Uva 445Uva 488Uva 489Uva 694Uva 457

Uva 10055

/*#include <stdio.h>
int main(){
	unsigned int a,b;
	while(~scanf("%u %u",&a,&b)){
		printf("%u\n",a>b?a-b:b-a);
		
	}
	return 0;
} Wrong Answer unsigned 0-2^32-1
*/
 
#include <stdio.h>
int main(){
    long long a,b;
	while(~scanf("%lld %lld",&a,&b)){
		printf("%lld\n",a>b?a-b:b-a);
		
	}
	return 0;
} 
           

Uva 10071

#include <stdio.h>
int main(){
	int a,b;
	while(~scanf("%d %d",&a,&b)){
		printf("%d\n",a*b*2);
	}
	return 0; 
}
           

Uva 10300

#include <stdio.h>
int main(){
	int a,b,c;
	int k,nCase;
	scanf("%d",&nCase);
	while(nCase--){
		int sum=0;
		scanf("%d",&k);
		while(k--){
			scanf("%d %d %d",&a,&b,&c);
			sum+=a*c;
		}
		printf("%d\n",sum);
	}
	return 0;
} 
           

Uva 458

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str;
	while(cin>>str){
		for(int i=0;i<str.length();i++){
			str[i]+='*'-'1';
		}
		cout<<str<<endl; 
	}
	return 0;
}
           

Uva 494

#include <iostream>
#include <string> 
using namespace std;
int main(){
	string str;
	while(getline(cin,str)){
		int count=0;
		for(int i=0;i<str.length();i++){
			while(i<str.length()&&!isalpha(str[i])){
				i++;
			}
			if(isalpha(str[i]))count++;
			while(i<str.length()&&isalpha(str[i])){
				i++;
			}
			
		}
		cout<<count<<endl;
	}
	return 0;
}
           

Uva 414

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str;
	int n;
	int count[15];
/*
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
*/
	while(cin>>n,n){
		getline(cin,str);
		int max=0;
		int num=0,sum=0;
		while(n--){
			getline(cin,str);
			count[num]=0;
			for(int i=0;i<str.length();i++){
				if(str[i]=='X') count[num]++; 
			}
			if(max<count[num]) max=count[num];
			num++;
		}	
		for(int i=0;i<num;i++){
			sum+=max-count[i];
		}
		cout<<sum<<endl;
	}
	return 0;
	
}
           

Uva 490

#include <iostream>
#include <string>
using namespace std;
int main(){
    string str[120];
    int n=0;
    int max=0;
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    for(;getline(cin,str[n]);n++)
    	if(max<str[n].length()) max=str[n].length();
    for(int i=0;i<max;i++){
        for(int j=n-1;j>=0;j--)
    	if(i<str[j].length()) cout<<str[j][i];
    	else cout<<' ';
        cout<<endl;
	}
	return 0;
} 
           

Uva 445

#include <iostream>
#include <string>
#include <iomanip>
using namespace std; 
int main(){

    string s;
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    int i,j,sum,len;
    while(getline(cin,s)){
        sum=0;
        for(int i=0;i<s.length();i++){
            if(s[i]>='0'&&s[i]<='9') 
              sum+=s[i]-'0';
            else{
               if(s[i]=='!') 
                 cout<<endl;
                else if(s[i]=='b')
                	cout<<setfill(' ')<<setw(sum)<<' ';
                else
                	cout<<setfill(s[i])<<setw(sum)<<s[i];
              
                sum=0;//歸0 
            }
        }
        cout<<endl;
    }
    return 0;
}
           

Uva 488

#include <iostream>
#include <iomanip>
using namespace std; 
int main(){

    int kcase;
    int Am,Fr;
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    cin>>kcase;
    char ch;
    int flag=1;
    while(kcase--){
    	cin>>Am>>Fr;
    	while(Fr--){
    		if(!flag) {
    			cout<<endl;
    		}
    		for(int i=1;i<=Am;i++){
    		    ch='0'+i;
    			cout<<setfill(ch)<<setw(i)<<ch<<endl;
    		}
    		for(int i=Am-1;i>=1;i--){
    			ch='0'+i;
    			cout<<setfill(ch)<<setw(i)<<ch<<endl;
    		}
    	   flag=0;
    	}
    }
return 0;
}
           

Uva 489

#include <stdio.h>
#include <string.h>
int stroke;
int s[105]; /*記錄第一行字元串中的字母是否猜過*/
int JudgeCorrect(char *a,char *b){
    int cnt,i,j,flag;
    stroke=0; /*猜錯的次數*/
    cnt=0; /*猜對的次數*/
    memset(s,0,sizeof(s));
    for(i=0;i<strlen(b);i++){
        flag=0;
        if(stroke==7)
            break;
        for(j=0;j<strlen(a);j++){
            if(b[i]==a[j]&&!s[j]){
                s[j]=1;
                cnt++;
                flag=1;
            }
        }
        if(!flag)
            stroke++;
    }
    return cnt;
}
int main(){
    int i,j,cases,count;
    char guess[105],answer[105];
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    while(~scanf("%d%*c",&cases)&&cases!=-1){
		scanf("%s\n%s",answer,guess);
		count=JudgeCorrect(answer,guess);
		printf("Round %d\n",cases);
		if(count==strlen(answer))  /*全猜對了*/
			printf("You win.\n");
		else if(stroke==7) /*猜錯的次數達到7次*/
			printf("You lose.\n");
		else
			printf("You chickened out.\n");
	}
    return 0;
}
           

Uva 694

#include <iostream>
using namespace std;
int main(){
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
	long long a,limit,b;
	int nCase=1;
	while(cin>>a>>limit){
		b=a;
		if(a==-1&&limit==-1)break;
		int total=0;
		while(1){
			if(a==1){
				total++;
				break;
			}
			if(a>limit){
				break;
			}
			if(a%2==0)a=a/2;
			else a=a*3+1;
			total++;
		}
	 	cout<<"Case "<<nCase++<<": A = "<<b<<", limit = "<<limit<<", number of terms = "<<total<<endl;
	}
	return 0; 
} 
           

Uva 457

#include <iostream>
 using namespace std;
 int main(){
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
 	int nCase;
 	cin>>nCase;
	while(nCase--){
	 	int DNA[11];
	 	for(int i=0;i<10;i++)
	 		cin>>DNA[i];
	 	int dish[43];
	 	int t_dish[43];
	 	for(int i=0;i<=41;i++){
	 		dish[i]=(i==20?1:0);
	 		t_dish[i]=dish[i];
	 	}
	 	int n=50;
	 	while(n--){
	 		for(int i=1;i<=40;i++){
	 			t_dish[i]=dish[i];
		 		switch(dish[i]){
		 			case 0:cout<<' ';break;
		 			case 1:cout<<'.';break;
		 			case 2:cout<<'x';break;
		 			case 3:cout<<'W';break;
		 		}
		 	}
		 	cout<<endl;
		 	for(int i=1;i<=40;i++){
				dish[i]=DNA[t_dish[i-1]+t_dish[i]+t_dish[i+1]];	 	
	 		}
	 	}
	 	if(nCase) cout<<endl;
	 }	 	
	 return 0;
 }