练习一A+B+C+D+E+F

来源:互联网 发布:淘宝店铺宝贝分类代码 编辑:程序博客网 时间:2024/06/05 16:19
Bessie asked her friend what day of the week she was born on. She knew that she was born on 2003 May 25, but didn't know what day it was. Write a program to help. Note that no cow was born earlier than the year 1800. 

Facts to know: 

* January 1, 1900 was on a Monday. 

* Lengths of months: 
    Jan 31          May 31      Sep 30    Feb 28 or 29    Jun 30      Oct 31    Mar 31          Jul 31      Nov 30    Apr 30          Aug 31      Dec 31

* Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year). 

* The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.
Input
* Line 1: Three space-separated integers that represent respectively the year, month (range 1..12), and day of a date.
Output
* Line 1: A single word that is the day of the week of the specified date (from the lower-case list: monday, tuesday, wednesday, thursday, friday, saturday, sunday).
Sample Input
2003 5 25
Sample Output
sunday
Hint
INPUT DETAILS: 
May 25, 2003 

OUTPUT DETAILS: 
      May 2003Su Mo Tu We Th Fr Sa             1  2  3 4  5  6  7  8  9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30 31
题意:已知1900.1.1是周一,问你随意某一天是星期几
思路:拿过题来,直接写了个又臭又长的模拟,自己给出的数据怎么测试都对,就是wa
后来想到可能是因为数据大了之后,处理不了,到网上找了个ac代码一测试,也出不了,
刚看到题解,差点没吐血,

/* 蔡勒公式 适合于1582年(中国明朝万历十年)10月15日之后的情形

公式 w = y + y/4 + c/4 - 2*c + 26 * (m+1)/10 + d - 1;
m如果是1 2 月份 y要倒退1年 m += 12
y是年份的后两位 y = year%100
c是世纪 c = year/100 */
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int judge(int x){    if((x%4==0&&x%100!=0)||(x%400==0))    return 1;    else return 0;    }int month[]={31,28,31,30,31,30,31,31,30,31,30,31};int main(){    int x,y,z;    int i,j,k;    int now=70001;    int num;    while(cin>>x>>y>>z)    {        now=2;        if(x>=1800)        {            for(i=1800;i<x;i++)            {                if(judge(i)==1)                now+=366;                else now+=365;                if(now>70000)                now-=70000;                }                int t=0;            for(j=0;j<y-1;j++)            {                t+=month[j];               // cout<<month[j]<<endl;                }            if(judge(x)==1)            {                t+=(z+1);                }            else t+=(z);            //cout<<t<<endl;            num=(now+t)%7;            //cout<<num<<endl;            }       /* else        {            for(i=1900;i>x;i--)            {                if(judge(i)==1)                now-=366;                else now-=365;                }            int t=0;            for(j=11;j>y;j--)            {                t+=month[j];               // cout<<month[j]<<endl;                }            t=t+(month[y-1]-z+1);            num=num%7;            }*/            //cout<<num<<endl;            if(num==1)            cout<<"monday"<<endl;            else  if(num==2)            cout<<"tuesday"<<endl;            else  if(num==3)            cout<<"wednesday"<<endl;            else  if(num==4)            cout<<"thursday"<<endl;            else  if(num==5)            cout<<"friday"<<endl;            else  if(num==6)            cout<<"saturday"<<endl;            else  if(num==0)            cout<<"sunday"<<endl;        }    return 0;    }
题解:
#include<iostream>#include<stdio.h>#include<cstring>using namespace std;char a[7][10]={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};int main(){int x,y,z;while(cin>>x>>y>>z){if(y<3){x--;y+=12;}int c=x/100;x=x%100;int w=x+x/4+c/4-2*c+26*(y+1)/10+z-1;        cout<<a[(w%7+7)%7]<<endl;}return 0;}
The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this: 
          7        3   8      8   1   0    2   7   4   4  4   5   2   6   5
Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame. 

Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.
Input
Line 1: A single integer, N 

Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.
Output
Line 1: The largest sum achievable using the traversal rules
Sample Input
573 88 1 02 7 4 44 5 2 6 5
Sample Output
30
Hint
Explanation of the sample: 

          7         *        3   8       *      8   1   0       *    2   7   4   4       *  4   5   2   6   5
The highest score is achievable by traversing the cows as shown above.
题意:数塔最佳路径
思路:动态规划  水
#include<iostream>#include<string.h>using namespace std;int max(int a,int b){    return a>b?a:b;    }int main(){    int i,j;    int n;    int map[1001][1001];    while(cin>>n)    {        memset(map,0,sizeof(map));        for(i=1;i<=n;i++)            for(j=1;j<=i;j++)            {                cin>>map[i][j];                }        for(i=1;i<=n;i++)            for(j=1;j<=i;j++)            {                map[i][j]=max(map[i-1][j-1],map[i-1][j])+map[i][j];        }    int maxx=0;    for(j=1;j<=n;j++)    if(maxx<map[n][j])        maxx=map[n][j];    cout<<maxx<<endl;    }    return 0;}
Farmer John purchased satellite photos of W x H pixels of his farm (1 <= W <= 80, 1 <= H <= 1000) and wishes to determine the largest 'contiguous' (connected) pasture. Pastures are contiguous when any pair of pixels in a pasture can be connected by traversing adjacent vertical or horizontal pixels that are part of the pasture. (It is easy to create pastures with very strange shapes, even circles that surround other circles.) Each photo has been digitally enhanced to show pasture area as an asterisk ('*') and non-pasture area as a period ('.'). Here is a 10 x 5 sample satellite photo: ..*.....** .**..***** .*...*.... ..****.*** ..****.*** This photo shows three contiguous pastures of 4, 16, and 6 pixels. Help FJ find the largest contiguous pasture in each of his satellite photos.
Input
* Line 1: Two space-separated integers: W and H * Lines 2..H+1: Each line contains W "*" or "." characters representing one raster line of a satellite photograph.
Output
* Line 1: The size of the largest contiguous field in the satellite photo.
Sample Input
10 5..*.....**.**..*****.*...*......****.***..****.***
Sample Output
16
题意:给出map 求最大连通区域
思路:  dfs  每个星华都作为起点搜一遍
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;char map[1001][1001];int maxx,nowmax;int dx[]={1,0,-1,0};int dy[]={0,1,0,-1};int w,h;void dfs(int x,int y){    if(x<0||x>=h||y<0||y>=w)return ;    if(map[x][y]=='.')return ;    if(map[x][y]=='*')    {        nowmax++;        map[x][y]='.';    }    for(int i=0;i<4;i++)    {        dfs(x+dx[i],y+dy[i]);        }    }int main(){    int i,j;    while(cin>>w>>h)    {        maxx=0;        memset(map,0,sizeof(map));        for(i=0;i<h;i++)        scanf("%s",&map[i]);        for(i=0;i<h;i++)        for(j=0;j<w;j++)        {            nowmax=0;            if(map[i][j]=='*')            dfs(i,j);            if(maxx<nowmax)            maxx=nowmax;            }        cout<<maxx<<endl;        }    return 0;    }
The cows play the child's game of hopscotch in a non-traditional way. Instead of a linear set of numbered boxes into which to hop, the cows create a 5x5 rectilinear grid of digits parallel to the x and y axes. They then adroitly hop onto any digit in the grid and hop forward, backward, right, or left (never diagonally) to another digit in the grid. They hop again (same rules) to a digit (potentially a digit already visited). With a total of five intra-grid hops, their hops create a six-digit integer (which might have leading zeroes like 000201). Determine the count of the number of distinct integers that can be created in this manner.
Input
* Lines 1..5: The grid, five integers per line
Output
* Line 1: The number of distinct integers that can be constructed
Sample Input
1 1 1 1 11 1 1 1 11 1 1 1 11 1 1 2 11 1 1 1 1
Sample Output
15
Hint
OUTPUT DETAILS: 
111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, and 212121 can be constructed. No other values are possible.
题意:给出一张map 可以走五步 起点不限 问每一步走过的位置组成的字符串最多几种
思路:又是深搜   比较字符串的方法:将其转化为int型数据,记录下当前val是否出现过不存在则标记即可
#include<iostream>#include<stdio.h>using namespace std;int map[5][5];int st[6];int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};int vis[1000000];int ans;void dfs(int nowx,int nowy,int num){    int i,j;    if(num==6)    {        int val=0;        for(i=0;i<6;i++)        {            val*=10;            val+=st[i];            }        if(!vis[val])        {            vis[val]=1;            ans++;            }        return ;    }    st[num]=map[nowx][nowy];    for(i=0;i<4;i++)    {        int nextx=nowx+dir[i][0],nexty=nowy+dir[i][1];        if(nextx>=0&&nextx<5&&nexty>=0&&nexty<5)        {            dfs(nextx,nexty,num+1);            }        }    }int main(){    int i,j;    for(i=0;i<5;i++)        for(j=0;j<5;j++)            cin>>map[i][j];    for(i=0;i<5;i++)    {        for(j=0;j<5;j++)        {            dfs(i,j,0);            }        }    cout<<ans<<endl;    return 0;    }
Farmer John has installed a new security system on the barn and now must issue a valid password to the cows in the herd. A valid password consists of L (3 <= L <= 15) different lower-case characters (from the traditional latin character set 'a'...'z'), has at least one vowel ('a', 'e', 'i', 'o', or 'u'), at least two consonants (non-vowels), and has characters that appear in alphabetical order (i.e., 'abc' is valid; 'bac' is not). Given a desired length L along with C lower-case characters, write a program to print all the valid passwords of length L that can be formed from those letters. The passwords must be printed in alphabetical order, one per line.
Input
* Line 1: Two space-separated integers, L and C * Line 2: C space-separated lower-case characters that are the set of characters from which to build the passwords
Output
* Lines 1..?: Each output line contains a word of length L characters (and no spaces). The output lines must appear in alphabetical order.
Sample Input
4 6a t c i s w
Sample Output
acisacitaciwacstacswactwaistaiswaitwastwcistciswcitwistw
Hint
INPUT DETAILS: 
Passwords of length 4 chosen from the given six characters
题意:给出c个字母 用这些字母组成长度为l的串,每个串中至少有一个愿意字母和两个辅音字母
按照字典序输出
思路:因为要按照字典序输出  先排下序
特别标记下元音字母和读音字母的个数   详见代码
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int l,c;int num1,num2;int lenth;char map[16],ans[16];bool judge(char x){    if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u')    return 1;    return 0;}void dfs(int x){    if(lenth==l)    {        if(num1&&num2>=2)        {            for(int i=1;i<=l;i++)            cout<<ans[i];            cout<<endl;            }        return;    }    for(int i=x;i<=c;i++)    {        lenth++;        if(judge(map[i]))num1++;        else num2++;        ans[lenth]=map[i];        dfs(i+1);        lenth--;        if(judge(map[i]))num1--;        else num2--;    }}int main(){    cin>>l>>c;    for(int i=1;i<=c;i++)    cin>>map[i];    sort(map+1,map+1+c);    dfs(1);}
阅读全文
1 0
原创粉丝点击