Codeforces 931A&1312A&172A

931A

题目

朋友聚会

链接:https://codeforces.com/problemset/problem/931/A

分析

俩个数间隔性的相互靠近,记录俩个数相等时候的step数。

代码实现

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int sum(int a)
 5 {
 6     int sum=0;
 7     for(int i=1;i<=a;i++)
 8         sum+=i;
 9     return sum;
10 }
11 int main()
12 {
13     int a,b,stepa=0,stepb=0;
14     cin>>a>>b;
15     if(a<b)
16     {
17         a++;
18         stepa++;
19         while(a!=b)
20         {
21             b--;
22             stepb++;
23             if(a==b)
24                 break;
25             a++;
26             stepa++;
27         }
28     }
29     else if(b<a)
30     {
31        b++;
32        stepb++;
33        while(a!=b)
34        {
35             a--;
36             stepa++;
37             if(a==b)
38                 break;
39             b++;
40             stepb++;
41        }
42     }
43     cout<<sum(stepa)+sum(stepb);
44 }

1312A

题目

链接:https://codeforces.com/problemset/problem/1312/A

分析

题目简单,看完题目就会

代码实现

#include<iostream>
using namespace std;
int main()
{
    int count;
    cin>>count;
    int a[count],b[count];
    for(int i=0;i<count;i++)
    {
        cin>>a[i]>>b[i];
    }
    for(int i=0;i<count;i++)
    {
        if(a[i]%b[i]==0)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

172A

题目

链接:https://codeforces.com/problemset/problem/172/A

分析

多个字符串互相相等的代码编写和字符串的截取,一开始我的思路是n个字符串从索引0开始往后走,只要遇到不相等的就退出输出输出索引+1,但是这个方法行不通代码编写很复杂由于N不确定,所以换一种思路:将N的字符串全部匹配看是否两两相等,不相等的话用substr函数截取(0,str.length()-1)如果全部相等输出字符串的长度即可。

代码实现

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int count,flag = 1,len;
 6     cin>>count;
 7     string a[count];
 8     for(int i=0;i<count;i++)
 9     {
10         cin>>a[i];
11     }
12 
13     while(a[0].length()!=0)
14     {
15 
16         for(int j=1;j<count;j++)
17             if(a[j-1]!=a[j])
18                 break;
19             else if(j==count-1)
20                 flag=0;
21 
22         if(flag==1)
23         {
24             for(int i=0;i<count;i++)
25             {
26                 a[i]=a[i].substr(0,a[i].length()-1);
27             }
28         }
29         else
30         {
31             cout<<a[0].length()<<endl;
32             break;
33         }
34 
35     }
36     if(a[0].length()<1)
37     cout<<0<<endl;
38 
39     return 0;
40 }
原文地址:https://www.cnblogs.com/g414056667/p/13752126.html