温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++ 从零单排(4)- ACM二

发布时间:2020-07-10 04:00:21 来源:网络 阅读:304 作者:拳四郎 栏目:移动开发

继续刷水题!!!

这次要搞zoj的题目,Let's go!


Quicksum

http://acm.zju.edu.cn/网上第 2812 题

Quicksum 是一行字符串(数据包)中每个字符的位置与该字符的值的乘积之和。空格
的值是 0,字母的值等于它在字母表中的位置。所以,A 的值是 1,B 的值是 2,依此类推,
Z 的值是 26。下面两个例子是求“ACM”和“MID CENTRAL”的 Quicksum:
ACM: 1*1 + 2*3 + 3*13 = 46
MID CENTRAL: 1*13 + 2*9 + 3*4 + 4*0 + 5*3 + 6*5 + 7*14 + 8*20 + 9*18 + 10*1 +
11*12 = 650

思路:用getline全部读进来,然后挨个处理就可以了。

C++实现(注意ifstream cin("aaa.txt");是在调试的时候才用的,提交代码的时候需要注释掉):

#include <iostream> #include <fstream>  using namespace std;  int main() {     ifstream cin("aaa.txt");     char ch[256];     int i = 1;     int sum = 0;     while (cin.getline(ch,256))     {         if(ch[0] == '#') break;         for(int i = 0;ch[i] != '\0';i++)         {             if(ch[i] != ' ') sum = sum+(i+1)*(ch[i]-64);         }         cout<<sum<<endl;         sum = 0;     }     return 0; } 


Encoding

http://acm.zju.edu.cn/网上第 2478 题

把一个字符串中连续重复的字母从左到右写成 kX 的形式,如果 k 是 1,那么,1 就要省略。

Sample Input
2
ABC
ABBCCC
Sample Output
ABC
A2B3C

思路:不断读取,不断判断是否与上一个字母相同,根据结果输出。

C++实现:

#include <iostream> #include <fstream>  using namespace std;  int main() {     //ifstream cin("aaa.txt");     string s,t;     int n;     cin >> n;     for(int i=0;i<n;i++)     {         cin>>s;         int c = 0;         t = s[0];         int tmp = 0;         for(int j=0;j<s.size();j++)         {         //When current letter equal last letter             if(s[j]==t[0])             {                 tmp++;                 //When comes to the end of s                 if(j == s.size()-1)                 {                     if(tmp==1) cout << t[0];                     else cout<<tmp<<t[0];                 }             }             //When current letter do not equal last letter             else             {                 if(tmp==1) cout<<t[0];                 else cout<<tmp<<t[0];                 t[0] = s[j];                 tmp = 1;                 if(j==s.size()-1)                 {                     if(tmp==1) cout<<t[0];                     else cout<<tmp<<t[0];                 }             }         }         cout<<endl;         s = "";     }     return 0; } 


Abbreviation

http://acm.zju.edu.cn/网上第 2947 题

比较两个缩写词是否相同,而缩写词又是从一个包含多个单词的名字中合成的。
每次读入一个单词,然后取出它的第一个字母,连接在字符串上,就组成了一个缩写词。

Sample Input
3
4
Super Harddisc Drive Cooler
4
Spade Heart Diamond Club
3
Shen Guang Hao
3
Shuai Ge Hao
3
Cai Piao Ge
4
C P C S


Sample Output
SAME
SAME
DIFFERENT


思路:挨个读取单词,将首字母存在string中,最后进行比较。

C++实现:

#include <iostream> #include <fstream> #include <string>  using namespace std;  int main() {     ifstream cin("aaa.txt");     string s,ssa,ssb;     int t,n,m;     cin>>t;     for(int i=0;i<t;i++)     {         cin>>n;         for(int j=0;j<n;j++)         {             cin>>s;             ssa = ssa + s[0];         }         cin>>m;         for(int k=0;k<m;k++)         {             cin>>s;             ssb = ssb+s[0];         }         if(ssa.compare(ssb)==0) printf("SAME\n");         else printf("DIFFERENT\n");         ssa = "";         ssb = "";     }     return 0; } 

printf比cout速度快!


Image Transformation

http://acm.zju.edu.cn/网上第 2857 题

把 RGB 图像转换为灰度图像的一
种最简便的方法是:把一个像素的红、绿和蓝的值都设置为一个相同的值(即(r+g+b)/3,
这里假定(r+g+b)总能被 3 整除)。
你决定编写一个程序来测试这种方法的有效性。

思路:统计每个通道的值,除以像素的个数。

C++实现:

#include <iostream> #include <fstream> #include <string> #include <vector> using namespace std;  int main() {     //ifstream cin("aaa.txt");     vector<int> r;     vector<int> g;     vector<int> b;     int n,m;     int rr,gg,bb;     int w = 0;     while(cin>>n>>m)     {         r.clear();         g.clear();         b.clear();         w++;         if(n==0 && m==0) break;         for(int i=0;i<n*m;i++)         {             cin>>rr;             r.push_back(rr);         }         for(int j=0;j<n*m;j++)         {             cin>>gg;             g.push_back(gg);         }          for(int k=0;k<n*m;k++)         {             cin>>bb;             b.push_back(bb);         }         cout<<"Case "<<w<<":"<<endl;         for(int p=0;p<n*m;p++)         {             cout<<(r[p]+g[p]+b[p])/3;             if((p+1)%m==0) cout<<endl;             else cout<<",";         }     }     return 0; } 


Error Correction

http://acm.zju.edu.cn/网上第 1949 题


一个布尔矩阵有一种奇偶性,即该矩阵所有行和所有列的和都是一个偶数。

编写一个程序,读入这个矩阵并检查它是否具有奇偶性。如果没有,你的程序应当再检查一下它是否可以通过修改一位(把 0 修改为 1,把 1 修改为 0)来使它具有奇偶性。如果不可能,这个矩阵就被认为是破坏了。


思路:统计矩阵中行和列中1的个数,判断是否为偶数。

都为偶数的话,说明有奇偶性,否则再判断,当只有一行的和是奇数且只有一列的和为奇数,那么,将这两行交点处的元素修改一下,就变成具有 parity property(奇偶性)的矩阵了。这里还要清楚的一点是,在一个矩阵中,任何一行与任何一列都有且仅有一个交点。

C++实现:

#include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; int matrix[100][100]; int SL[100]; int SC[100];  int main() {     ifstream cin("aaa.txt");     int i,j,PL,PC,CountL,CountC;     int n;     while(cin>>n)     {         if(n==0) break;         PL = 0;         PC = 0;         CountL = 0;         CountC = 0;         for(i=0;i<n;i++)         {             SL[i] = 0;             SC[i] = 0;         }         for(i=0;i<n;i++)         {             for(j=0;j<n;j++)             {                 cin>>matrix[i][j];                 SL[i]=SL[i]+matrix[i][j];                 SC[j]=SC[j]+matrix[i][j];             }         }         for(i=0;i<n;i++)         {             if(SL[i]%2!=0)             {                 PL=i;//Record the row                 CountL++;             }              if(SC[i]%2!=0)             {                 PC=i;//Record the line                 CountC++;             }         }         //Output result         if(CountL == 0&&CountC==0) cout<<"OK"<<endl;         else if(CountL==1 && CountC==1) cout<<"Change bit ("<<PL+1<<","<<PC+1<<")"<<endl;         else cout<<"Corrupt"<<endl;     }     return 0; } 


打完收工,故事还在继续。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI