iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 6
0
自我挑戰組

CPE 一星精選全攻略系列 第 6

Day6 #UVa 11332 & UVa 10252

UVa 11332 - Summing Digits

題目連結

題目說明

給定一段數字,將每一位數不停地相加直到只剩下個位數就是答案。

做法

因為要不停地相加到,所以我們用遞迴寫。

#include <bits/stdc++.h>

using namespace std;

int f(int n){
    if(n<10) return n;
    return f(f(n/10)+n%10);
}

int main(){
    int n;
    while(cin>>n,n)
        cout<<f(n)<<endl;
    return 0;
}

UVa 10252 - Common Permutation

題目連結

題目說明

給定兩字串,比較兩字串中出現相同字母且次數最少的那個。

做法

讀入兩字串後,先統計第一個字串後跟第二個字串比較,後將出現最少的輸出出來。

#include <bits/stdc++.h>

using namespace std;


int main(){
    string s1,s2;
    while(getline(cin,s1)){
        getline(cin,s2);
        int idx[2][30]={0};
        #define idx(x) idx[x][e-'a']
        for(auto&e:s1)
            ++idx(0);
        for(auto&e:s2) 
            idx(1) = min(idx(0),idx(1)+1);
        for(int&e:idx[1])cout<<string(e,char('a'+(&e-idx[1])));cout<<endl;
    }
    return 0;
}

上一篇
Day5 # UVa 10008 & UVa 10222
下一篇
Day7 #UVa 490 & UVa 272
系列文
CPE 一星精選全攻略9
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言