[C++] henuACM summer training freshman training game 2

Question D eating chicken battlefield (HDU 1047)

topic

After a day and night of fighting, Special Forces Xiao A finally caught up with an airdrop on the top of the school. Just as Xiao A couldn’t wait to lick the airdrop, he found that there was a password box outside the airdrop. Enter the correct password to get a Geely Service 1. Set, one AWM, one 8x lens. The title is described as follows: Given a number of numbers, calculate the sum of these numbers. Since Xiao A has never gone to school, please help him solve this problem now. The exercise is about to start, come on, commando!

Input

An integer T in the first line represents the number of test groups.
The input will contain up to 100 lines of text, each line containing a number. The length of each number does not exceed 100 characters and contains only numbers (the numbers are all non-negative). The number entered in each line is regarded as an input block, and each block of blocks is inputted until the end of 0.

Output

For each set of tests, your program should output the sum of the numbers given in the input.
This question contains multiple test cases!
The first line of the multiple input is the integer N, then an empty line, followed by N input blocks. Each input block uses the format indicated in the problem description. There is a blank line between the input blocks.
The output format consists of N output blocks. There is a blank line between the output blocks.
(PS: Leading zeros are not allowed)

Simple Input

1
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Simple Output

370370367037037036703703703670

Question meaning This
question is the addition of multiple data mentioned in the previous solution, the code is as follows:

#include<cstdio>
#include<cstring>
using namespace std;
char input[209];
int sum[209];
int main()
{
    
    
    int t;
    scanf("%d",&t);//实例个数
    while(t--)
    {
    
    
        memset(sum,0,sizeof(sum));//初始化总和为0
        while(true)
        {
    
    
            scanf("%s",input);//输入的一个大数
            if( strcmp(input,"0")==0 )
                break;
            int len=strlen(input);
            int i=len-1;
            for(int j=i; j>=0; j--)//输入的大数最高位在左边,直接把大数值叠加到总和上去即可
                sum[i-j]+=input[j]-'0';
        }
        int c=0;
        for(int i=0; i<200; i++)//总和进位
        {
    
    
            int s = c+sum[i];
            c = s/10;
            sum[i] = s%10;
        }
        int i;
        for(i=199; i>=0; i--) //找到总和的最高位,总和的高位在右
            if(sum[i]!=0)
                break;
        for(; i>0; i--)
            printf("%d",sum[i]);
        printf("%d",sum[0]);
        printf("\n");
        if(t>0)printf("\n");
    }
    return 0;
}

E-question affinity string (HDU 2203)

topic

Pippi brings you the sign-in questions again! Pippi has several strings s1. He now wants to get a string s2. The definition of this string (s2) is as follows: Given two strings s1 and s2, if they can be cyclically shifted through s1 , So that s2 is included in s1, then s2 is the string that Pippi needs. Now Pippi has found a number of possible strings s2, and now he asks you to be smart to write a program to see if it is the string that Pippi needs, if it is, enter yes, if not, output no.

Input

This question has multiple sets of test data. The first line of each set of data contains the input string s1, and the second line contains the input string s2. The lengths of s1 and s2 are both less than 100,000.

Output

If s2 is a string required by Pippi, output "yes", otherwise, output "no". The output of each test group occupies one line.
Note that there are multiple sets of inputs! Be regular and draw. Remember to change the name of the next array

Simple Input

AADDAD
THERE IS
ABABABAA
THERE

Simple Output

yes
no

The meaning of the question is
actually the KMP application, using the KMP template. But the point to note is that this target string only needs to be able to get the pattern string through circular movement. Cyclic movement means that the first digit moves to the last digit, and then it keeps moving in a loop. During the exam, I never thought of how to integrate this idea into the overall code. Later, when listening to the problem solving, the big guy thought of a way: multiply the string by two times, such as abc into abcabc . Then use kmp in this new string.

But I saw on the blog that many other people’s codes did not use KMP, but used a find:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    
    
    string s1, s2;
    int pos;
 
    while(cin >> s1) {
    
    
        cin >> s2;
 
        if(s1.size() < s2.size())
            cout << "no" << endl;
        else {
    
    
            s1 += s1;//注意这一步,string类是支持这种写法的
            pos = s1.find(s2);
            if(pos >= 0)
                cout << "yes" << endl;
            else
                cout << "no" << endl;
        }
    }
 
    return 0;
}
 

Guess you like

Origin blog.csdn.net/qq_44899247/article/details/97636589