国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

算法競賽入門經(jīng)典習(xí)題 第三章

2019-11-11 02:49:43
字體:
供稿:網(wǎng)友

算法競賽入門經(jīng)典習(xí)題  第三章

在下面的習(xí)題中,前一半的題目幾乎只需要“按照題目說的做”,但后面的題目需要一些思考甚至靈感。

為了保證學(xué)習(xí)效果,請至少獨(dú)立完成8道習(xí)題。 

習(xí)題3-1得分(Score, ACM/ICPC SEOul 2005, UVa1585)

給出一個由O和X組成的串(長度為1~80),統(tǒng)計得分。

每個O的得分為目前連續(xù)出現(xiàn)的O的個數(shù),X的得分為0。 

例如,OOXXOXXOOO的得分為1+2+0+0+1+0+0+1+2+3。

//輸入字符串判斷X與O然后處理得分。

//如果為X則重置為0,否則一直加1,每次將得到分值相加。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int main(){    int t;    char s[200];    int kase = 0;    scanf("%d", &t);    while (t--)    {        int score = 0, sum = 0;        scanf("%s", s);        int l = strlen(s);        for (int i = 0; i < l; i++)        {            if (s[i] == 'X')                score = 0;            if (s[i] == 'O')                score++;            sum += score;        }        PRintf("%d/n", sum);    }    return 0;}

#include <cstdio>#include <cstring>const int maxn = 150;int main(){    char s[maxn];    int t, kase = 0;    scanf("%d", &t);    while (t--)    {        int num = 0, score = 0;        scanf("%s", s);        int l = strlen(s);        for (int i = 0; i < l; i++)        {            if (s[i] == 'O')            {                num++;                score += num;            }            else                num = 0;        }        printf("%d/n", score);    }    return 0;}

習(xí)題3-2分子量(Molar Mass, ACM/ICPC Seoul 2007, UVa1586)

給出一種物質(zhì)的分子式(不帶括號),求分子量。 

本題中的分子式只包含4種原子,分別為C, H, O, N,原子量分別為12.01, 1.008, 16.00, 14.01(單位:g/mol)。 

例如,C6H5OH的分子量為94.108g/mol。 

【解題思路分析】

1. 建立字母到數(shù)值(原子量)的映射數(shù)組。 2. 原子后面數(shù)值可能為多位數(shù),一直往后判斷,直到不是數(shù)字,i值自增。 3. 碰到字母直接加原子量;碰到數(shù)字用(數(shù)字-1)乘以原子量。

#include <iostream>#include <cstring>#include <cstdio>#include <cctype>const double m[] = {0, 0, 12.01, 0, 0, 0, 0, 1.008, 0, 0, 0, 0, 0, 14.01, 16.00};int main(){    char s[150];    int t;    scanf("%d", &t);    while (t--)    {        memset(s, '0', sizeof(s));        double sum = 0;        scanf("%s", s);        char ch = s[0];        int l = strlen(s), n;        for(int i = 0; i < l; i++)        {            if (isalpha(s[i]))            {                ch = s[i];                sum += m[ch - 'A'];            }            else            {                n = s[i] - '0';                if (isdigit(s[i+1]))                {                    n = n*10 + (s[i+1] - '0');                    i++;                }                sum += m[ch - 'A'] * (n - 1);            }        }        printf("%.3lf/n", sum);    }    return 0;}----------------------其他方法------------------------

#include<stdio.h>#include<string.h>#include<ctype.h>//int isdigit(int,char)需要的頭文件#define MAX 20double getWeight(char c){    double w = 0;    switch (c) {        case 'c':        case 'C': w = 12.01;break;        case 'h':        case 'H': w = 1.008;break;        case 'o':        case 'O': w = 16.00;break;        case 'n':        case 'N': w = 14.01;break;    }    return w;}int main() {    char str[MAX];    scanf("%s", str);    if(isdigit(str[0])){//isdigit判斷字符是不是數(shù)字        printf("輸入格式錯誤!/n");        return 0;    }    double weight = 0;    double sum = 0;    for (int i=0; i<strlen(str);i++){        int num = 1;        for(int pre=0; isdigit(str[i]);i++){            num = pre*10+str[i]-'0';            pre = num;        }        sum += num * weight;   //weight = str[i-1]'s weight        if(i<strlen(str)) weight = getWeight(str[i]);  //weight = str[i]'s weight    }    sum += weight;//加上最后一次循環(huán)的原子量    printf("分子量為:%.3lfg/mol/n", sum);    return 0;}

習(xí)題3-3數(shù)數(shù)字(Digit Counting , ACM/ICPC Danang 2007, UVa1225)

把前n(n≤10000)個整數(shù)順次寫在一起:123456789101112…數(shù)一數(shù)0~9各出現(xiàn)多少次

(輸出10個整數(shù),分別是0,1,…,9出現(xiàn)的次數(shù))。

注意:  數(shù)個數(shù)時別漏0,且從1開始數(shù)。

#include <cstdio>#include <cstring>#include <iostream>using namespace std;int main(){    int t;    scanf("%d", &t);    while (t--)    {        int n, x;        scanf("%d", &n);        int a[10];        memset(a, 0, sizeof(a));        for (int i = 1; i <= n; i++)        {            x = i;            while (x)            {                a[x%10]++;                x /= 10;            }        }        printf("%d", a[0]);        for (int i = 1; i < 10; i++)            printf(" %d", a[i]);        printf("/n");    }    return 0;}注:本題可參考-騎金剛跨長城-解題過程,因崔思婷。

習(xí)題3-4周期串(Periodic Strings, UVa455)

如果一個字符串可以由某個長度為k的字符串重復(fù)多次得到,則稱該串以k為周期。

例如,abcabcabcabc以3為周期(注意,它也以6和12為周期)。

輸入一個長度不超過80的字符串,輸出其最小周期。

【解題思路分析】

1.如果i為最小周期,那么字符串長度必定是i的整數(shù)倍

2.再判斷i是否為周期。

#include <cstdio>#include <cstring>const int maxn = 82;int main(){    char s[maxn];    int t;    scanf("%d", &t);    while (t--)    {        memset(s, '0', sizeof(s));        scanf("%s", s);        int m = strlen(s);        for (int i = 1; i < m; i++)        {            if (m % i == 0)            {                bool f = true;                for (int j = i; j < 2*i; j++)                {                    if (s[j - i] != s[j])                        f = false;                }                if (f)                {                    printf("%d/n", i);                    break;                }            }        }    }    return 0;}

注意:類似abcdeabcabcdeabcabcdeabcabcdeabcabcdeabc的判斷以及最小周期。

To Be Continued.


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 梁山县| 银川市| 利川市| 台中市| 遂平县| 大埔区| 云阳县| 葵青区| 郑州市| 荆州市| 上饶市| 广丰县| 耿马| 肥东县| 望江县| 绩溪县| 客服| 墨脱县| 永安市| 沙坪坝区| 札达县| 无棣县| 昌平区| 友谊县| 双桥区| 阿坝| 汾阳市| 启东市| 龙海市| 登封市| 漠河县| 吉隆县| 莒南县| 德格县| 祁东县| 龙胜| 乐至县| 韩城市| 工布江达县| 香港 | 怀柔区|