2018年1月18日 星期四

UVA Q10922 - 2 the 9s

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
char a[1001];
int b[1001],c[1001],bp,cp,d;
while(cin>>a){
    if(a[0]=='0'){
        break;
    }
    for(int x=0;x<strlen(a);x++){
        b[x]=a[x]-48;
    }
    bp=strlen(a);
    d=0;
    if(bp==1){
        d++;
    }
    while(bp>1){
        memset(c,0,sizeof(c));
        cp=1;
        for(int x=0;x<bp;x++){
            c[0]=c[0]+b[x];
            for(int y=0;y<cp;y++){
                if(c[y]>=10){
                    cp++;
                    c[y+1]=c[y+1]+(c[y]/10);
                    c[y]=c[y]%10;
                }
            }
        }
        memset(b,0,sizeof(b));
        for(int x=0;x<cp;x++){
            b[x]=c[x];
        }
        bp=cp;
        d++;
    }
    if(b[0]%9==0){
        cout<<a<<" is a multiple of 9 and has 9-degree "<<d<<".";
    }else{
        cout<<a<<" is not a multiple of 9.";
    }
    cout<<endl;
}


system("pause");
return 0;
}

UVA Q10878 - Decode the tape

#include<iostream>
#include<cmath>
#include<string>
using namespace std;
int main() {
    char a[11],d;
    int b,c;
    gets(a);
    while (gets(a)) {
        c=7;
        b=0;
        if (a[0] == '|') {
            for(int x=1;x<=9;x++){
                if(a[x]!='.'){
                    if(a[x]=='o'){
                        b=b+pow(2,c);
                    }
                    c--;   
                }   
            }
        d=b;
        cout<<d;
        }else if(a[0]=='_'){
            break;
        }
    }


    system("pause");
    return 0;
}

UVA Q10812 - Beat the Spread!

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
int a,b,c,d,e;
cin>>a;
for(int x=1;x<=a;x++){
    cin>>b>>c;
    if((b+c)%2==0&&(b-c)%2==0&&(b+c)>=0&&(b-c)>=0){
        cout<<(b+c)/2<<" "<<(b-c)/2;
    }else{
        cout<<"impossible";
    }
    cout<<endl;
}

system("pause");
return 0;
}

2018年1月14日 星期日

Win32程式 - 透過tcp傳送keylog的keylogger

    因為小編剛學 winsock,然後又看到無線傳輸的keylogger不是要錢就是崁病毒,靈機一動寫了個keylogger。(Github裡有說明)

 連結:(下載後用visual studio編譯)
https://github.com/chenlicpp/tcpkeylogger

2018年1月13日 星期六

UVA Q10784 - Diagonal

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int is(double a){
    return a==(int)a;
}
int main(){
long long int a,c=1,d;
long double b;
while(cin>>a){
    if(a==0){
        break;
    }
    b=((3+sqrt(9+(8*a)))/2);
    if(is(b)){
        d=((3+sqrt(9+(8*a)))/2);
    }else{
        d=((3+sqrt(9+(8*a)))/2)+1;   
    }
    cout<<"Case "<<c<<": "<<d<<endl;
    c++;
}


system("pause");
return 0;
}

UVA Q10783 - Odd Sum

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
long long int a,b,c,d;
cin>>a;
for(int x=1;x<=a;x++){
    cin>>b>>c;
    if(b%2==0){
        b++;
    }else if(c%2==0){
        c--;
    }
    d=0;
    for(int y=b;y<=c;y=y+2){
        d=d+y;
    }
    cout<<"Case "<<x<<": "<<d<<endl;
}


system("pause");
return 0;
}

UVA Q10633 - Rare Easy Problem

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
unsigned long long int a,b;
while(cin>>a){
    if(a==0){
        break;
    }
    b=0;
    for(int x=9;x>=0;x--){
    if(((10*a)-x)%9==0){
        if(b!=0){
            cout<<" ";
        }
        b++;
        cout<<((10*a)-x)/9;
    }
    }
    cout<<endl;
}


system("pause");
return 0;
}

Q 10591 - Happy Number

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
long long int a,b,c,d;
cin>>a;
for(int x=1;x<=a;x++){
    cin>>c;
    b=c;
    while(1){
        d=0;
        while(c){
            d=d+((c%10)*(c%10));
            c=c/10;
        }
        c=d;
        if(c==1){
            cout<<"Case #"<<x<<": "<<b<<" is a Happy number."<<endl;
            break;
        }else if(c==4){
            cout<<"Case #"<<x<<": "<<b<<" is an Unhappy number."<<endl;
            break;
        }
    }
}
return 0;
}

UVA Q10499 - The Land of Justice

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
    long long a;
    while(cin>>a){
        if(a==-1){
            break;
        }
        if(a==1){
            cout<<"0%"<<endl;
        }else{
            cout<<a*25<<"%"<<endl;
        }
    }
system("pause");
return 0;
}

UVA Q10370 - Above Average

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main(){
int a,b,c[1000];
double d,e,f;
cin>>a;
for(int x=1;x<=a;x++){
    cin>>b;
    f=0;
    for(int y=0;y<b;y++){
        cin>>c[y];
        d=d+c[y];
    }
    d=d/b;
    for(int y=0;y<b;y++){
        if(c[y]>d){
            f++;
        }
    }
    e=f/b*100;//(¹FŒÐ/Á`€HŒÆ)*100%
    cout<<setprecision(3)<<fixed<<e<<"%"<<endl;
}

system ("pause");
return 0;
}

UVA Q10346 - Peter's Smokes

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
unsigned long long int a,b,c;
while(cin>>a>>b){
    c=0;
    while(a>=b){
        c=c+b;
        a=a-b;
        a++;
    }
    cout<<c+a<<endl;
}


system("pause");
return 0;
}

UVA Q10340 - All in All


#include <iostream>
#include <cstring>
using namespace std;
int main() {
    char a[1000],b[1000];
    int c;
    bool d;
    while(cin>>a>>b){
        c=0;
        d=0;
        for(int x=0;x<strlen(b);x++){
            if(a[c]==b[x]){
                c++;
                if(c==strlen(a)){
                    d=1;
                    break;
                }
            }
        }
        if(d==0){
            cout<<"No"<<endl;
        }else{
            cout<<"Yes"<<endl;
        }
    }
    system("pause");
    return 0;
}

UVA Q10327 - Flip Sort

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
int a,b[1000],c,m,d,e;
while(cin>>a){
    for(int x=0;x<a;x++){
        cin>>b[x];
    }
    m=0;
    while(1){
        e=m;
        for(c=0;c<a-1;c++){
            if(b[c+1]<b[c]){
                d=b[c+1];
                b[c+1]=b[c];
                b[c]=d;
                m++;
            }
        }
         if(m==e){
             cout<<"Minimum exchange operations : "<<m<<endl;
             break;
         }
    }
}


system("pause");
return 0;
}

UVA Q10300 - Ecological Premium

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
long double a,b,c,d,e,f;
cin>>a;
for(int x=1;x<=a;x++){
    cin>>b;
    f=0;
    for(int y=1;y<=b;y++){
        cin>>c>>d>>e;
        f=f+(((c/d)*e)*d);
    }
    cout<<f<<endl;
}


system("pause");
return 0;
}

UVA Q10235 - Simply Emirp

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
long long int a,b,c,d,e;
while(cin>>a){
    d=a;
    for(b=2;b<=sqrt(a);b++){
        if(a%b==0){
            break;
        }
    }
    if(b>sqrt(a)){
        c=0;
        while(a>0){
            c=c+a%10;
            c=c*10;
            a=a/10;
        }
        c=c/10;
        for(e=2;e<=sqrt(c);e++){
            if(c%e==0){
                break;
            }
        }
        if(e>sqrt(c)&&c!=d){
            cout<<d<<" is emirp."<<endl;
        }else{
            cout<<d<<" is prime."<<endl;
        }
    }else{
        cout<<d<<" is not prime."<<endl;
    }
}


system("pause");
return 0;
}

UVA Q10222 - Decode the Mad man

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
char a[20000],b[48]{'`','1','2','3','4','5','6','7','8','9','0','-','=',
               'q','w','e','r','t','y','u','i','o','p','[',']','\\',
               'a','s','d','f','g','h','j','k','l',';','\'',
               'z','x','c','v','b','n','m',',','.','/'};
while(gets(a)){
    for(int x=0;x<strlen(a);x++){
        if(a[x]==' '){
            cout<<" ";
        }else{
            for(int y=0;y<48;y++){
                if(a[x]==b[y]){
                    cout<<b[y-2];
                }
            }
        }
    }
    cout<<endl;
}


system("pause");
return 0;
}

UVA Q10193 - All You Need Is Love

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stdio.h>
using namespace std;
int main(){
long long int n,s[2],p,a;
char s1[31],s2[31];
cin>>n;
for(int x=1;x<=n;x++){
    cin>>s1>>s2;
    s[0]=0;
    s[1]=0;
    p=0;
    for(int y=strlen(s1)-1;y>=0;y--){
        s[0]=s[0]+((s1[y]-48)*pow(2,p));
        p++;
    }
    p=0;
    for(int y=strlen(s2)-1;y>=0;y--){
        s[1]=s[1]+((s2[y]-48)*pow(2,p));
        p++;
    }
    while(s[1]!=0){
        a=s[0]%s[1];
        s[0]=s[1];
        s[1]=a;
    }
    cout<<"Pair #"<<x<<": ";
    if(s[0]==1){
        cout<<"Love is not all you need!";
    }else{
        cout<<"All you need is love!";
    }
    cout<<endl;
}
system("pause");
return 0;
}

UVA Q10189 - Minesweeper

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
int a,b,t=0;
char c[500][500];
while(cin>>a>>b){
    t++;
    if(a==0&&b==0){
        return 0;
    }
    if(t!=1){
        cout<<endl;
    }
    for(int x=0;x<a;x++){
        for(int y=0;y<b;y++){
            cin>>c[x][y];
            if(c[x][y]!='*'){
                c[x][y]=c[x][y]+2;
            }
        }
    }
    cout<<"Field #"<<t<<":"<<endl;
    for(int x=0;x<a;x++){
        for(int y=0;y<b;y++){
            if(c[x][y]=='*'){
                for(int z=x-1;z<=x+1;z++){
                    for(int zz=y-1;zz<=y+1;zz++){
                        if(c[z][zz]!='*'&&z>=0&&zz>=0&&z<a&&zz<b){
                            c[z][zz]++;
                        }
                    }
                }
            }
        }
    }
    for(int x=0;x<a;x++){
        for(int y=0;y<b;y++){
            if(c[x][y]!='*'){
                cout<<c[x][y];
            }else{
                cout<<"*";
            }
        }
        cout<<endl;
    }
}
}

UVA Q10106 - Product

#include<iostream>
#include<cstring>
using namespace std;
int main(){
char a[50000],b[50000];
int c[50000],d[50000],cc,dd,e[50000],p,pp,ppp;
while(cin>>a>>b){
    for(int x=0;x<strlen(a);x++){
        c[x]=a[x]-48;
    }
    cc=strlen(a)-1;
    for(int x=0;x<strlen(b);x++){
        d[x]=b[x]-48;
    }
    dd=strlen(b)-1;
    p=0;
    ppp=0;
    memset(e,0,sizeof(a));
    for(int x=cc;x>=0;x--){
        pp=p;
        for(int y=dd;y>=0;y--){
            e[pp]=e[pp]+(d[y]*c[x]);
            if(pp>ppp){
                ppp=pp;
            }
            pp++;
        }
        p++;
    }
    for(int x=0;x<ppp;x++){
        e[x+1]=e[x+1]+(e[x]/10);
        e[x]=e[x]%10;
    }
    if(e[ppp+1]>0){
        ppp++;
    }
    for(int x=ppp;x>=0;x--){
        cout<<e[x];
    }
    cout<<endl;
}

system("pause");
return 0;
}

UVA Q10082 - WERTYU

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
char a[10000],b[48]{'`','1','2','3','4','5','6','7','8','9','0','-','=',
               'Q','W','E','R','T','Y','U','I','O','P','[',']','\\',
               'A','S','D','F','G','H','J','K','L',';','\'',
               'Z','X','C','V','B','N','M',',','.','/'};
while(gets(a)){
    for(int x=0;x<strlen(a);x++){
        if(a[x]==' '){
            cout<<" ";
        }else{
            for(int y=0;y<strlen(b);y++){
                if(a[x]==b[y]){
                    cout<<b[y-1];
                }
            }
        }
    }
    cout<<endl;
}


system("pause");
return 0;
}

UVA Q10071 - Back to High School Physics

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
long long int a,b;
while(cin>>a>>b){
    cout<<a*b*2<<endl;
}
system("pause");
return 0;
}

UVA Q10055 - Hashmat the brave warrior

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
long long int a,b;
while(cin>>a>>b){
    cout<<abs(a-b)<<endl;
}


system("pause");
return 0;
}

UVA Q10041 - Vito's family

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
int a,b,c[500],d;
cin>>a;
for(int x=0;x<a;x++){
    cin>>b;
    for(int y=0;y<b;y++){
        cin>>c[y];
    }
    sort(c,c+b);
    d=0;
    for(int y=0;y<b;y++){
        d=d+abs(c[y]-c[b/2]);
    }
    cout<<d<<endl;
}
system("pause");
return 0;
}

UVA Q10038 - Jolly Jumpers

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
int a,b[3000],c[2999],y;
while(cin>>a){
    for(int x=0;x<a;x++){
        cin>>b[x];
    }
    for(int x=0;x<a-1;x++){
        c[x]=abs(b[x+1]-b[x]);
    }
    sort(c,c+(a-1));
    for(y=0;y<a-1;y++){
        if(c[y]!=y+1){
            break;
        }
    }
    if(y==a-1){
        cout<<"Jolly";
    }else{
        cout<<"Not jolly";
    }
    cout<<endl;
}

system("pause");
return 0;
}

UVA Q10018 - Reverse and Add

#include<iostream>
#include<iomanip>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;
int countd(int a){
    int d=0;
    for(int x=a;x>0;x=x/10){
        d++;
    }
    return d;
}
int checkrev(int a){
    int d=0,e,f,g;
    d=countd(a);
    for(int x=1;x<=d/2;x++){
        e=pow(10,x);
        g=pow(10,x-1);
        f=(a/pow(10,(d-x)));
        if((a%e)/g!=(f%10)){
            return 0;
        }
    }
    return 1;
}
int main(){
long long n,a,t,b,c;
cin>>n;
for(int x=0;x<n;x++){
    cin>>a;
    t=0;
    while(1){
        t++;
        b=a;
        c=0;
        while(b>0){
            c=(c*10)+(b%10);
            b=b/10;
        }
        a=a+c;
        if(checkrev(a)){
            break;
        }
    }
    cout<<t<<" "<<a<<endl;
}


system("pause");
return 0;
}

UVA Q10008 - What's Cryptanalysis

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<ctype.h>
using namespace std;
int main(){
int a,c[26],l,lp;
cin>>a;
char b[2000],d;
memset (c,0,sizeof(c));
for(int x=0;x<=a;x++){
    gets(b);
    for(int y=0;y<strlen(b);y++){
        if(isalpha(b[y])){
            if(b[y]-65>25){
                b[y]=b[y]-32;
            }
            cout<<b[y]-65<<" ";
            c[b[y]-65]++;
        }
    }
}
while(1){
    l=0;
    lp=0;
    for(int y=0;y<26;y++){
        if(c[y]>l){
            l=c[y];
            lp=y;
        }
    }
    if(l==0){
        break;
    }
    d=lp+65;
    cout<<d<<" "<<l<<endl;
    c[lp]=0;
}

system("pause");
return 0;
}

UVA Q993 - Product of digits

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
long int a,b,c,d;
cin>>a;
for(int x=0;x<a;x++){
    cin>>b;
    c=0;
    d=0;
    if(b==1){
        c=1;
    }
    for(int y=9;y>=2;y--){
        while(b%y==0){
            b=b/y;
            c=c+(y*pow(10,d));
            d++;
        }
    }
    if(b==1){
        cout<<c;
    }else{
        cout<<"-1";
    }
    cout<<endl;
}


system("pause");
return 0;
}

UVA Q694 - The Collatz Sequence

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
    long long int a,b,c=1,d,e;
    while(cin>>a>>b){
        if(a<0&&b<0){
            break;
        }
        e=a;
        d=1;
        while(a!=1){
            if(a%2==0){
                a=a/2;
            }else{
                a=(3*a)+1;
            }
            if(a>b){
                break;
            }
            d++;
        }
        cout<<"Case "<<c<<": A = "<<e<<", limit = "<<b<<", number of terms = "<<d<<endl;
        c++;
    }

system("pause");
return 0;
}

UVA Q679 - Dropping Balls

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
    long long a,b,c,d;
    cin>>a;
    for(long long x=0;x<a;x++){
        cin>>b>>c;
        d=1;
        for(long long y=1;y<b;y++){
            if(c%2==0){
                d=d*2+1;
                c=c/2;
            }else{
                d=d*2;
                c=(c+1)/2;
            }
        }
        cout<<d<<endl;
    }
    cin>>a;

system("pause");
return 0;
}

UVA Q661 - Blowing Fuses

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
int n,m,c,t=0,nn[2][20],mm,total,totalmax;
bool boom=0;
while(cin>>n>>m>>c){
    boom=0;
    totalmax=0;
    total=0;
    if(n+m+c==0){
        break;
    }
    for(int x=0;x<n;x++){
        cin>>nn[0][x];
        nn[1][x]=0;
    }
    t++;
    cout<<"Sequence "<<t<<endl;
    for(int x=0;x<m;x++){
        cin>>mm;
        mm--;
        if(nn[1][mm]%2==0){
            nn[1][mm]++;
            total+=nn[0][mm];
            if(total>totalmax){
                totalmax=total;
            }
        }else{
            nn[1][mm]++;
            total-=nn[0][mm];
        }
        if(total>c&&boom==0){
            cout<<"Fuse was blown."<<endl;
            boom=1;
        }
    }
    if(boom==0){
        cout<<"Fuse was not blown."<<endl;
        cout<<"Maximal power consumption was "<<totalmax<<" amperes."<<endl;
    }
    cout<<endl;
}


system("pause");
return 0;
}

UVA Q591 - Box of Bricks

#include<iostream>
#include<math.h>
#include<cstring>
#include<iomanip>
using namespace std;
int main(){
    int a,b[50],c,d,e=0;
    while(cin>>a){
        e++;
        if(a==0){
            break;
        }
        c=0;
        d=0;
        for(int x=0;x<a;x++){
            cin>>b[x];
            c=c+b[x];
        }
        c=c/a;
        for(int x=0;x<a;x++){
            if(b[x]>c){
                d=d+b[x]-c;
            }
        }
        cout<<"Set #"<<e<<endl<<"The minimum number of moves is "<<d<<"."<<endl<<endl;
    }
system ("pause");
return 0;
}

UVA Q573 - The Snail

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
long double a,b,c,d,e,f,g;
while(cin>>a>>b>>c>>d){
    if(a==0){
        break;
    }
    g=0;
    f=0;
    e=b*(d/100);
    while(1){
        f++;
        if(b>0){
            g=g+b;
        }
        if(g>a){
            cout<<"success on day "<<f<<endl;
            break;
        }
        g=g-c;
        if(g<0){
            cout<<"failure on day "<<f<<endl;
            break;
        }
        b=b-e;
    }
}


system("pause");
return 0;
}

UVA Q483 - Word Scramble

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
char a[20000];
int b;
while(gets(a)){
    b=0;
    for(int x=0;x<strlen(a);x++){
        if(a[x]==' '){
            for(int y=x-1;y>=b;y--){
                cout<<a[y];
            }
            b=x+1;
            cout<<' ';
        }else if(x==strlen(a)-1){
            for(int y=x;y>=b;y--){
                cout<<a[y];
            }
        }
    }
    cout<<endl;
}

system("pause");
return 0;
}

UVA Q382 - Perfection

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
int a,b,c,d;
cout<<"PERFECTION OUTPUT"<<endl;
while(cin>>a){
    b=0;
    d=0;
    if(a==0){
        break;
    }
    for(int x=1;x<a;x++){
        if(a%x==0){
        b=b+x;
        }
    }
    c=a;
    while(c>0){
        c=c/10;
        d++;
    }
    for(int x=1;x<=(5-d);x++){
        cout<<" ";
    }
    cout<<a<<"  ";
    if(b==a){
        cout<<"PERFECT";
    }else if(b>a){
        cout<<"ABUNDANT";
    }else{
        cout<<"DEFICIENT";
    }
    cout<<endl;
}
cout<<"END OF OUTPUT"<<endl;

system("pause");
return 0;
}

UVA Q340 - Master-Mind Hints

#include<iostream>
using namespace std;
int main(){
int a,c[1000],d[1000],h[1000],e,f,g=1;
bool aa;
while(cin>>a){
    if(a==0){
        break;
    }
        for(int x=0;x<a;x++){
            cin>>c[x];
        }
    cout<<"Game "<<g<<":"<<endl;
    while(1){
        e=0;
        f=0;
        aa=1;
        for(int x=0;x<a;x++){
            cin>>d[x];
            if(d[x]!=0){
                aa=0;
            }
        }
        if(aa==1){
            break;
        }
        for(int x=0;x<a;x++){
            h[x]=c[x];
        }
        for(int x=0;x<a;x++){
            if(h[x]==d[x]){
                e++;
                h[x]=-2;
                d[x]=-1;
            }
        }
        for(int y=0;y<a;y++){
            for(int x=0;x<a;x++){
                    if(h[y]==d[x]){
                        h[y]=0;
                        d[x]=-3;
                        f++;
                    }
                }
        }
       
        cout<<"    ("<<e<<","<<f<<")"<<endl;
    } g++;
}


system("pause");
return 0;
}

UVA Q299 - Train Swapping

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
int a,b,c[100],d,e,f;
cin>>a;
for(int x=1;x<=a;x++){
    cin>>b;
    for(int y=0;y<b;y++){
        cin>>c[y];
    }
    e=0;
    while(1){
        f=e;
        for(int y=0;y<b-1;y++){
        if(c[y]>c[y+1]){
            d=c[y];
            c[y]=c[y+1];
            c[y+1]=d;
            e++;
            }
        }   
        if(e==f){
            break;
        }
    }
   
    cout<<"Optimal train swapping takes "<<e<<" swaps."<<endl;
}


system("pause");
return 0;
}

UVA Q272 - TeX Quotes

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
    char a[9999];
    bool b=0;
     while(gets(a)){
         for(int x=0;x<strlen(a);x++){
             if(a[x]=='"'&&b==0){
                 cout<<"``";
                 b=1;
            }else if(a[x]=='"'&&b==1){
                cout<<"''";
                b=0;
            }else{
                cout<<a[x];
            }
        }
        cout<<endl;
    }


system("pause");
return 0;
}

UVA Q118 - Mutant Flatworld Expolrers

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<fstream>
using namespace std;
int r[2][197],rr=0,a,c,b,d,nd;
int dont(int x,int y){
    for(int xx=0;xx<rr;xx++){
        if(x==r[0][xx]&&y==r[1][xx]){
            return 1;
        }
    }
    return 0;
}
int dontgo(int x,int y){
    if(x<0){
        c++;
    }
    if(y<0){
        d++;
    }
    if(x>a){
        c--;
    }
    if(y>b){
        d--;
    }
}
int main(){
char e,s[101];
bool l;
cin>>a>>b;
while(cin>>c>>d>>e>>s){
    l=0;
    if(e=='N'){
        nd=0;
    }else if(e=='S'){
        nd=2;
    }else if(e=='E'){
        nd=1;
    }else if(e=='W'){
        nd=3;
    }
    for(int x=0;x<strlen(s);x++){
        if(s[x]=='F'){
            if(nd==0){
                if(dont(c,d)){
                    d++;
                    dontgo(c,d);
                }else{
                    d++;
                }
            }else if(nd==1){
                if(dont(c,d)){
                    c++;
                    dontgo(c,d);
                }else{
                    c++;
                }
            }else if(nd==2){
                if(dont(c,d)){
                    d--;
                    dontgo(c,d);
                }else{
                    d--;
                }
            }else if(nd==3){
                if(dont(c,d)){
                    c--;
                    dontgo(c,d);
                }else{
                    c--;
                }
            }
        }else if(s[x]=='R'){
            nd++;
        }else if(s[x]=='L'){
            nd--;
        }
        if(nd>3){
            nd=0;
        }
        if(nd<0){
            nd=3;
        }
        if(d<0||c<0||c>a||d>b){
            l=1;
            dontgo(c,d);
            r[0][rr]=c;
            r[1][rr]=d;
            rr++;
            break;
        }
    }
    cout<<c<<" "<<d<<" ";
    if(nd==0){
        cout<<"N";
    }else if(nd==1){
        cout<<"E";
    }else if(nd==2){
        cout<<"S";
    }else if(nd==3){
        cout<<"W";
    }
    if(l==1){
        cout<<" LOST";
    }
    cout<<endl;
}


system("pause");
return 0;
}

UVA Q12405 - Scarecrow

這題是貪婪解法,在陣列依依搜尋,如果遇到良田(.),就在良田後一塊地建一個稻草人,所以被搜尋到的那塊良田與接下來的兩塊地都被保護到了,所以一搜尋到良田就直接跳到後三格並繼續搜尋直到結束,這樣就知道最少要放幾個稻草人了。


程式碼:

#include<iostream>

using namespace std;


int main(){
int a,b,d;
char c[100];
cin>>a;//輸入有幾組測資
for(int x=1;x<=a;x++){//連續輸入
        cin>>b;//輸入陣列有幾項
        cin>>c;//輸入陣列
        d=0;//初始化d(稻草人數)
        for(int y=0;y<b;y++){//搜尋陣列裡的良田
              if(c[y]=='.'){//當遇到良田
                    d++;//有幾個稻草人
                    y=y+2;//跳三格(包括for迴圈裡的y++)
                }
         }
         cout<<"Case "<<x<<": "<<d<<endl;//輸出這是第幾組測資(x)與最少要見幾個稻草人(d)
}
system("pause");
return 0;
}