试题代码集合1

 

 

1、String Matching

题目描述

    Finding all occurrences of a pattern in a text is a problem that arises frequently in text-editing programs.     Typically,the text is a document being edited,and the pattern searched for is a particular word supplied by the user.       We assume that the text is an array T[1..n] of length n and that the pattern is an array P[1..m] of length m<=n.We further assume that the elements of P and  T are all alphabets(∑={a,b...,z}).The character arrays P and T are often called strings of characters.       We say that pattern P occurs with shift s in the text T if 0<=s<=n and T[s+1..s+m] = P[1..m](that is if T[s+j]=P[j],for 1<=j<=m).       If P occurs with shift s in T,then we call s a valid shift;otherwise,we calls a invalid shift.      Your task is to calculate the number of vald shifts for the given text T and p attern P.

输入描述:

   For each case, there are two strings T and P on a line,separated by a single space.You may assume both the length of T and P will not exceed 10^6.

输出描述:

    You should output a number on a separate line,which indicates the number of valid shifts for the given text T and pattern P.

示例1

输入

复制

abababab abab

输出

复制

3

code:

#include <iostream>

using namespace std;

int main()
{
    string t,p;
    cin >> t>> p;
    int pos = -1;
    int num = 0;
    while(1){
        pos = t.find(p,pos+1);
        if(pos != string::npos){
            num++;
        }
        else{
            break;
        }
    }
    cout << num << endl;
    return 0;
}

2、二次方程计算式

题目描述

设计一个二次方程计算器

输入描述:

每个案例是关于x的一个二次方程表达式,为了简单,每个系数都是整数形式。

输出描述:

每个案例输出两个实数(由小到大输出,中间由空格隔开),保留两位小数;如果无解,则输出“No Solution”。

示例1

输入

复制

x^2+x=3x+4

输出

复制

-1.24 3.24

 

code:

/*
二次方程计算器

三目运算符

数学公式

二分法


*/

#include<iostream>
#include<math.h>
#include<string>
#include<algorithm>

using namespace std;

int a,b,c,i,num;

void process(string s,int mode)
{
    int flag=1,i=num=0;
    while(i<s.size())
    {
        if(s[i]=='x')
        {
            if(i+1!=s.size()&&s[i+1]=='^')
            {
                a+=(num==0?(flag==1?(mode==0?1:-1):(mode==0?-1:1)):num);i+=2;
            }
            else
            {
                b+=(num==0?(flag==1?(mode==0?1:-1):(mode==0?-1:1)):num);
            }
            i+=1;
        }
        else if(s[i]=='+')
        {
            flag=1;num=0;i++;
        }
        else if(s[i]=='-')
        {
            flag=0;num=0;i++;
        }
        else if(s[i]>='0'&&s[i]<='9')
        {
            while(s[i]>='0'&&s[i]<='9')
            {
                num=num*10+s[i]-'0';
                i++;
            }
            if(flag==(mode==0?0:1))
            {
                num=-num;
            }
            if(s[i]!='x')
            {
                c+=num;
            }
        }

    }
}

int main()
{
    string s;
    while(cin>>s)
    {
        a=b=c=0;
        int pos=s.find('=');
        string pre=s.substr(0,pos);
        string ed=s.substr(pos+1);
        process(pre,0);
        process(ed,1);
        float derta=b*b-4*a*c;
        if(derta<0)
        {
            cout << "No Solution" << endl;
        }
        else
        {
            float x1=(-b+sqrt(derta))/2/a;
            float x2=(-b-sqrt(derta))/2/a;
            if(x1>x2)
            {
                printf("%.2f %.2f\n",x2,x1);
            }
            else
            {
                printf("%.2f %.2f\n",x1,x2);
            }
        }
    }
    return 0;
}

3、WERTYU

题目描述

    A common typing error is to place the hands on the keyboard one row to the right of the correct position. So "Q" is typed as "W" and "J" is typed as "K" and so on. You are to decode a message typed in this manner.

输入描述:

    Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (except Q, A, Z), or punctuation shown above [except back-quote (`)]. Keys labelled with words [Tab, BackSp, Control, etc.] are not represented in the input.

输出描述:

    You are to replace each letter or punctuation symbol by the one immediately to its left on the QWERTY keyboard shown above. Spaces in the input should be echoed in the output.

示例1

输入

复制

O S, GOMR YPFSU/

输出

复制

I AM FINE TODAY.

code:

/*
题意:
键盘打错字,输入字母往往打字母的右边的输入到键盘里。
偏移一格输入。

字符串转换,string查找

*/

#include<iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    string key = "1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
    string s;
    while(getline(cin,s))
    {
        string res;
        int position;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]==' ')
            {
                res += s[i];
            }
            else
            {
                position = key.find(s[i]);
                res += key[position-1];
            }
        }
        cout << res << endl;
    }
    return 0;
}

4、Simple sorting

题目描述

You are given an unsorted array of integer numbers. Your task is to sort this array and kill possible duplicated elements occurring in it.

输入描述:

For each case, the first line of the input contains an integer number N representing the quantity of numbers in this array(1≤N≤1000). Next N lines contain N integer numbers(one number per each line) of the original array.

输出描述:

For each case ,outtput file should contain at most N numbers sorted in ascending order. Every number in the output file should occur only once.

示例1

输入

复制

6
8 8 7 3 7 7

输出

复制

3 7 8

code:

/*
排序
1、直接数组sort+判断输出
2、set输出

*/


#include<iostream>
#include<stdio.h>
#include<set>

using namespace std;

int main()
{
    int count=0;
    int temp=0;
    set<int> st;
    cin >> count;
    while((scanf("%d",&temp))!=EOF)
    {
        st.insert(temp);
    }
    for(set<int>::iterator it=st.begin();it!=st.end();it++)
    {
        printf("%d ",*it);
    }
    return 0;
}

5、Old Bill

 

题目描述

    Among grandfather's papers a bill was found.     72 turkeys $_679_     The first and the last digits of the number that obviously represented the total price of those turkeys are replaced here by blanks (denoted _), for they are faded and are illegible. What are the two faded digits and what was the price of one turkey?     We want to write a program that solves a general version of the above problem.     N turkeys $_XYZ_     The total number of turkeys, N, is between 1 and 99, including both. The total price originally consisted of five digits, but we can see only the three digits in the middle. We assume that the first digit is nonzero, that the price of one turkeys is an integer number of dollars, and that all the turkeys cost the same price.     Given N, X, Y, and Z, write a program that guesses the two faded digits and the original price. In case that there is more than one candidate for the original price, the output should be the most expensive one. That is, the program is to report the two faded digits and the maximum price per turkey for the turkeys.

输入描述:

    The first line of the input file contains an integer N (0<N<100), which represents the number of turkeys. In the following line, there are the three decimal digits X, Y, and Z., separated by a space, of the original price $_XYZ_.

输出描述:

    For each case, output the two faded digits and the maximum price per turkey for the turkeys.

示例1

输入

复制

72
6 7 9
5
2 3 7
78
0 0 5

输出

复制

3 2 511
9 5 18475
0

code:

/*

题目:old bill

75只火鸡$_678_
五位数求出,第一个数字和最后一个数字,时的价格最大,输出平均价格

*/


#include <iostream>

using namespace std;

int main()
{
    int x, y, z;
    int N;
    int flag=0;
    //int a, b, p;
    int sum = 0;
    while(scanf("%d",&N)!=EOF)
    {
        flag=0;
        scanf("%d %d %d",&x,&y,&z);
        sum = x*1000+y*100+z*10;
        for(int i=9;i>0&&flag==0;i--)
        {
            int temp = sum + i*10000;
            for(int j=9;j>=0&&flag==0;j--)
            {
                temp += j;
                if(temp%N==0)
                {
                    printf("%d %d %d\n",i,j,temp/N);
                    flag=1;
                }
                temp -= j;
            }
        }
        if(flag==0)
        printf("0\n");
    }

    return 0;
}

6、Fibonacci

 

题目描述

    The Fibonacci Numbers{0,1,1,2,3,5,8,13,21,34,55...} are defined by the recurrence:     F0=0 F1=1 Fn=Fn-1+Fn-2,n>=2     Write a program to calculate the Fibonacci Numbers.

输入描述:

    Each case contains a number n and you are expected to calculate Fn.(0<=n<=30) 。

输出描述:

   For each case, print a number Fn on a separate line,which means the nth Fibonacci Number.

示例1

输入

复制

1

输出

复制

1

code:

/*
fibonacci

把所有结果首先预存起来
*/

#include <iostream>

using namespace std;

int main()
{
    int n;
    long long f[31];
    f[0]=0;f[1]=1;f[2]=1;f[3]=2;f[4]=3;f[5]=5;f[6]=8;f[7]=13;f[8]=21;
    f[9]=34;f[10]=55;f[11]=89;f[12]=144;f[13]=233;f[14]=377;f[15]=610;
    f[16]=987;f[17]=1597;f[18]=2584;f[19]=4181;f[20]=6765;f[21]=10946;
    f[22]=17711;f[23]=28657;f[24]=46368;f[25]=75025;f[26]=121393;
    f[27]=196418;f[28]=317811;f[29]=514229;f[30]=832040;

    while(cin >> n)
    {
        cout << f[n] << endl;
    }
    return 0;
}

7、数字反转

题目描述

12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转。

输入描述:

每行两个正整数a和b(0<a,b<=10000)。

输出描述:

    如果满足题目的要求输出a+b的值,否则输出NO。

示例1

输入

复制

12 34
99 1

输出

复制

46
NO

code:

/*
题目:数字反转
巧妙解题
求余

*/


#include<iostream>
#include<string>
#include<sstream>
#include<bits/stdc++.h>
using namespace std;

int reverse(int x)
{
    int y=0;
    while(x%10!=0||x/10!=0)
    {
        y=y*10+x%10;
        x/=10;
    }
    // cout << y << endl;;
    return y;
}

int main()
{
    int a,b;
    while(cin >> a >> b)
    {
        if(reverse(a+b)==reverse(a)+reverse(b))
        {
            cout << a+b << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }

    //cout << reverse(34567) << endl;
    return 0;
}

8、棋盘游戏

 

题目描述

    有一个6*6的棋盘,每个棋盘上都有一个数值,现在又一个起始位置和终止位置,请找出一个从起始位置到终止位置代价最小的路径:     1、只能沿上下左右四个方向移动     2、总代价是没走一步的代价之和     3、每步(从a,b到c,d)的代价是c,d上的值与其在a,b上的状态的乘积     4、初始状态为1     每走一步,状态按如下公式变化:(走这步的代价%4)+1。

输入描述:

    每组数据一开始为6*6的矩阵,矩阵的值为大于等于1小于等于10的值,然后四个整数表示起始坐标和终止坐标。

输出描述:

    输出最小代价。

示例1

输入

复制

1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
0 0 5 5

输出

复制

23

code:

/*
题目:棋盘问题
根据最小代价,简单剪枝,dfs找一条路,遍历剪枝大于最小代价和越界的
*/
#include <iostream>
#include <algorithm>
#define INF 1e9

using namespace std;

int ex,ey;
int m[6][6];
int mark[6][6];
int go[4][2] = {{1,0},{0,1},{0,-1},{-1,0}};
int ans = INF;

void dfs(int x,int y,int s,int c)
{
    if(mark[x][y]==1||x<0||y<0||x>5||y>5||c>ans)
    {
        return;
    }
    else if(x==ex && y==ey)
    {
        ans = c;
        return;
    }
    else{
        mark[x][y]=1;
        for(int i=0;i<4;i++)
        {
            int newx = x + go[i][0];
            int newy = y + go[i][1];
            int newc = c + m[newx][newy]*s;
            int news = (m[newx][newy]*s)%4+1;
            dfs(newx,newy,news,newc);
        }
        mark[x][y] = 0;
    }
}
int main()
{
    for(int i=0;i<6;i++)
    {
        for(int j=0;j<6;j++)
        {
            cin >> m[i][j];
            mark[i][j]=0;
        }
        //cout << i << " ij " << endl;

    }
    int x,y;
    cin >> x >> y >> ex >> ey;
    dfs(x,y,1,0);
    cout << ans << endl;
    return 0;
}

9、Sum  of factoricals

题目描述

    John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions to the foundations of mathematics, logic, quantum physics, meteorology, science, computers, and game theory. He was noted for a phenomenal memory and the speed with which he absorbed ideas and solved problems. In 1925 he received a B.S. diploma in chemical engineering from Zurich Institute and in 1926 a Ph.D. in mathematics from the University of Budapest, His Ph.D. dissertation on set theory was an important contributions to the subject.     At the age of 20, von Neumann proposed a new definition of ordinal numbers that was universally adopted. While still in his twenties, he made many contributions in both pure and applied mathematics that established him as a mathematician of unusual depth. His Mathematical Foundation of Quantum Mechanics (1932) built a solid framework for the new scientific discipline.     During this time he also proved the mini-max theorem of GAME THEORY. He gradually expanded his work in game theory, and with coauthor Oskar Morgenstern he wrote Theory of Games and Economic Behavior (1944).     There are some numbers which can be expressed by the sum of factorials. For example 9, 9 = 1! + 2! + 3! . Dr. von Neumann was very interested in such numbers. So, he gives you a number n, and wants you to tell whether or not the number can be expressed by the sum of some factorials. Well, it is just a piece of case. For a given n, you will check if there are some xi, and let n equal to Σt (上标) i=1(下标) xi! (t≥1, xi≥0, xi = xj <==> i = j)            t      即 Σ  xi! (t≥1, xi≥0, xi = xj <==> i = j)           i=1     If the answer is yes, say "YES"; otherwise, print out "NO".

输入描述:

    You will get a non-negative integer n (n≤1,000,000) from input file.

输出描述:

    For the n in the input file, you should print exactly one word ("YES" or "NO") in a single line. No extra spaces are allowed.

示例1

输入

复制

9
2

输出

复制

YES
YES

code:

/*
ÌâÄ¿£ºsum of factorials

×¢Ò⣺9=1£¡+2£¡+3£¡

0£¡=1£»




/*

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int num[12];
    num[0]=1;
    for(int i=1;i<11;i++)
    {
        num[i]=num[i-1]*i;
    }
    int n;
    while(cin >> n){
        for(int i=10;i>=0;i--)
        {
            if(n>=num[i])
            {
                n-=num[i];
            }
        }
        if(n>0)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
    }


    return 0;
}

10、路径打印

题目描述

给你一串路径,譬如: a\b\c a\d\e b\cst d\ 你把这些路径中蕴含的目录结构给画出来,子目录直接列在父目录下面,并比父目录向右缩一格,就像这样: a   b     c   d      e b   cst d 同一级的需要按字母顺序排列,不能乱。

输入描述:

    每个测试案例第一行为一个正整数n(n<=10)表示有n个路径,当n为0时,测试结束,接下来有n行,每行有一个字串表示一个路径,长度小于50。

输出描述:

输出目录结构,每一个测试样例的输出紧跟一个空行。

示例1

输入

复制

4
a\b\c
a\d\e
b\cst
d\
0

输出

复制

a
  b
    c
  d
    e
b
  cst
d

code:

/*
2021/2/14
题目:路径打印

目录结构打印

*/


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string str[11];
    int i,j,n;
    while(cin>>n)
    {
        if(n==0) break;
        for(int l=0;l<n;l++)
        {
            cin >> str[l];
        }
        sort(str,str+n);
        string stra,strb;
        for(int l=0;l<n;l++)
        {
            strb=str[l];
            int count=0;
            for(i=0,j=0;i<stra.size()&&j<strb.size();i++,j++)
            {
                if(stra[i]==strb[j])
                    count++;
                else
                    break;
            }
            for(;j<strb.size();j++)
            {
                for(int k=0;k<count;k++)
                {
                    cout<<" ";
                }
                while(j<strb.size()&&strb[j]!='\\')
                {
                    cout<<strb[j++];
                    count++;
                }
                count++;
                cout<<endl;
            }
            stra=strb;
        }
       cout<<endl;
    }
    return 0;
}

 

n

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值