Codeforces 629A&118A

629A

题目

亲戚的生日蛋糕

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

分析

这道题目很简单,只需要按行遍历二维字符数组判断每一行的C的个数n,很容易算出通项公式是n*(n-1)/2,用一个宿命变量记录,然后再按同样的方法按列遍历一次,即可得出所求结果。

代码实现

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int n,line,sum=0;
 6     cin>>n;
 7     char str[n][n];
 8     for(int i=0;i<n;i++)
 9         for(int j=0;j<n;j++)
10         cin>>str[i][j];
11     for(int i=0;i<n;i++)
12     {
13         line = 0;
14       for(int j=0;j<n;j++)
15     {
16         if(str[i][j]=='C')
17             line++;
18     }
19     sum+=line*(line-1)/2;
20     }
21 
22     for(int j=0;j<n;j++)
23     {
24         line = 0;
25       for(int i=0;i<n;i++)
26     {
27         if(str[i][j]=='C')
28             line++;
29     }
30     sum+=line*(line-1)/2;
31     }
32 cout<<sum<<endl;
33     return 0;
34 }

118A

题目

字符串任务

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

分析

这道题考察的是字符串容器的基本操作,算法设计不难,其中分而治之的思想很重要,不可以用一个循环实现所以,否则思路很乱。去除元音字母采用了刚刚学习的反向包含思想,就是将所有的元音字母放在一个字符串数组U中,用U.find()函数来判断,其他的字符串基本操作包含插入和删除。

 

 代码实现

 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 using namespace std;
 5 int main()
 6 {
 7     string str;
 8     string U="aAoOyYeEuUiI";
 9     cin>>str;
10     string::size_type idx;
11     for(int i=0;i<str.length();i++)
12     {
13         int idx=U.find(str[i]);
14         if(idx!=string::npos)  //找到了
15         {
16             str.erase(i,1);
17             i--;
18         }
19     }
20     //cout<<str<<endl;
21     int len = str.length();
22     for(int i=0;i<len;i++)
23     {
24         if(str[i]>='A'&&str[i]<='Z')
25             str[i] = str[i]+32;
26     }
27     for(int i=0;i<str.length();i+=2)
28     {
29         str.insert(i,".");
30     }
31     cout<<str<<endl;
32     return 0;
33 }
原文地址:https://www.cnblogs.com/g414056667/p/13762769.html