一、
#include<iostream> #include<stack> #include<string> using namespace std; int reverse(int x) { int a = 0; do { a = a * 10 + (x % 10); x /= 10; } while(x > 0); return a; } int reverseAdd(int a, int b) { if (a < 1 || a > 70000 || b < 1 || b > 70000) { return -1; } return reverse(a) + reverse(b); } #include "stdio.h" int main() { int a, b; scanf("%d,%d", &a, &b); printf("%d\n", reverseAdd(a, b)); return 0; }
#include<iostream> #include<stack> #include<string> using namespace std; int Rever(int x) { int y=0; for(;x>0;) { y=y*10+x%10; x=x/10; } return y; } int main() { int a,b; cin>>a; cin>>b; int c; c=Rever(a)+Rever(b); cout<<c; return 0; }
二、
搞清楚每次變化導致的位置變化即可,考察多次的條件判斷。
三、
#include <stdio.h> #include <stdlib.h> #include <string.h> const int inf = 0x7f7f7f7f; int d[50]; int a[][7] = { {0}, {0, 0, 2, 10, 5, 3, -1}, {0, -1, 0, 12, -1, -1, 10}, {0, -1, -1, 0, -1, 7, -1}, {0, 2, -1, -1, 0, 2, -1}, {0, 4, -1, -1, 1, 0, -1}, {0, 3, -1, 1, 0, 2, 0} };//時間花費矩陣 int x, y;//目的地,大霧城市 int res; int used[10];//是否標記 int ans = inf; int ansp; int ansd[50]; void dfs(int now, int cost, int p)//5 0 1 ,出發地,花費時間,路徑個數 { if (now == x) //到達目的地 { if (cost < ans) //花費少了就更改新的路徑 { ans = cost; for (int i = 0; i < p; i++) { ansd[i] = d[i];//走過的路 } ansp = p;//路個數 } return;//結束標志 } for (int i = 1; i <= 6; i++) { if (i == y) //大霧城市 { continue;//時間為無線大,回到循環 } if (used[i] == 0 && a[now][i] >= 0) //沒走過,並且能通 { used[i] = 1;//標記 走過 d[p] = i;//記錄走過的路 dfs(i, cost + a[now][i], p + 1);//遞歸 used[i] = 0;//標記撤銷 } } } int main() { static char emp[] = ""; static char col[] = ", "; scanf("%d%d", &x, &y);//出差城市,大霧城市 if (x == 5) { printf("0\n[]\n"); return 0;//出差為5的話,0不可達 } if (y == 5) { printf("1000\n[]\n"); return 0;//大霧為5的話,1000不可達 } d[0] = 5; used[5] = 1; dfs(5, 0, 1);//核心算法 char *p = emp; if (ans == inf) { printf("1000\n[]\n"); } else { printf("%d\n[", ans); for (int i = 0; i < ansp; i++) { printf("%s%d", p, ansd[i]); p = col; } printf("]\n"); } }
深度優先搜索的問題,難度有點大~
四、
題目描述
請設計一個算法完成兩個超長正整數的加法。
輸入描述:
輸入兩個字符串數字
輸出描述:
輸出相加后的結果,string型
#include<iostream> #include<string> #include<cmath> #include<algorithm> using namespace std; int main() { //string a="9999999999999",b="99"; string a,b; cin>>a; cin>>b; int n1=a.length(),n2=b.length(); string x,y; for(int i=0;i<n1;i++) { x[i]=a[n1-i-1]; } for(int i=0;i<n2;i++) { y[i]=b[n2-i-1]; } if(n1<n2) { for(int i=n1;i<n2;i++) x[i]='0'; } else { for(int i=n2;i<n1;i++) y[i]='0'; } int n=max(n1,n2); int s[100]; int f=0; for(int i=0;i<n;i++) { int c=x[i]-48+y[i]-48+f; if(c<10) { s[i]=c; f=0; } else { s[i]=c-10; f=1; } } if(f==1) { s[n]=1; n=n+1; } for(int j=0;j<n;j++) cout<<s[n-1-j]; cout<<endl; string r[100]; for(int j=0;j<n;j++) { r[j]=s[n-1-j]+'0'; } for(int j=0;j<n;j++) cout<<r[j]; return 0; }
#include<iostream> #include<string> #include<cmath> #include<algorithm> using namespace std; int main() { //string a="9999999999999",b="99"; string a,b; cin>>a; cin>>b; int n1=a.length(),n2=b.length(); string x,y; for(int i=0;i<n1;i++) { x[i]=a[n1-i-1]; } for(int i=0;i<n2;i++) { y[i]=b[n2-i-1]; }//x並不是string形式了 if(n1<n2) { for(int i=n1;i<n2;i++) x[i]='0'; } else { for(int i=n2;i<n1;i++) y[i]='0'; } int n=max(n1,n2); int s[100]; int f=0; for(int i=0;i<n;i++) { int c=x[i]-48+y[i]-48+f; if(c<10) { s[i]=c; f=0; } else { s[i]=c-10; f=1; } } if(f==1) { s[n]=1; n=n+1; } string r,l; for(int j=0;j<n;j++) { r=s[n-1-j]+'0'; l=l+r; } cout<<l<<endl;<<l.length(); return 0; }
可以看到最后l才化為string形式,才可以用l.length()函數。
注意string 的加減法,string只能和string形式的加,類似於python 中的append。
#include<iostream> #include<string> #include<cmath> #include<algorithm> using namespace std; int main() { string a,b,s; while(cin>>a>>b) { while(a.length()<b.length()) a='0'+a; s=b; while(a.length()>b.length()) b='0'+b; s=a; int temp=0,n=0; for(int i=a.length()-1;i>=0;i--) { temp=a[i]-'0'+b[i]-'0'+n;//整數 if(temp<10) { s[i]=temp+'0'; n=0 ; } else { s[i]=temp-10+'0'; n=1; } } if(n==1) s='1'+s; cout<<s<<endl; } return 0; }
加了循環輸入之后,牛客可以通過。
五、
輸入描述:
輸入一個字符串。
輸出描述:
輸出字符串中最長的數字字符串和它的長度。如果有相同長度的串,則要一塊兒輸出,但是長度還是一串的長度
輸入
abcd12345ed125ss123058789
輸出
123058789,9
1、
#include<iostream> #include<string> using namespace std; int main() { string a,temp={},s={}; while(cin>>a) { int n=0; for(int i=0;i<a.length();i++) { if(a[i]>='0' && a[i]<='9')//開始標志 { s+=a[i]; } else { if(n<s.length()) { temp=s; n=s.length(); } else if(n==s.length()) { temp+=s; } s={}; } } if(n<s.length()) { temp=s; n=s.length(); } else if(n==s.length()) { temp+=s; } s={}; cout<<temp<<","<<n<<endl; } return 0; }
一定要注意循環輸入里對變量的初始化。
再有string 的初始化,a={}。
字符串當成整體的思想。
部分比較的思想。
#include <iostream> #include <string> using namespace std; int main() { string str; while( cin>>str ) { int i; int max = 0; string ss; string out; for(i = 0; i < str.size(); i++) { if(str[i] >= '0' &&str[i] <= '9') { ss += str[i]; while(str[i+1] >= '0' &&str[i+1] <= '9') { i++; ss += str[i]; } if(ss.size() > max) { max = ss.size(); out = ss; } else if(ss.size() == max) out += ss; } ss.clear(); } cout<<out<<','<<max<<endl; } return 0; }
ss.clear()的應用。
五、
功能: 求一個byte數字對應的二進制數字中1的最大連續數,例如3的二進制為00000011,最大連續2個1
輸入: 一個byte型的數字
輸出: 無
返回: 對應的二進制數字中1的最大連續數
輸入描述:
輸入一個byte數字
輸出描述:
輸出轉成二進制之后連續1的個數
輸入
3
輸出
2
1、位運算
2、
#include<iostream> #include<string> #include<cmath> using namespace std; int main() { int n; while(cin>>n) { int count=0,maxCount=0; while(n) { if(n&1)//表示有1存在,下一次循環如果還有1,也會繼續。 { ++count; maxCount=max(count,maxCount); } else count=0; n=n>>1; } cout<<maxCount<<endl; } return 0; }
利用了位運算
3、
#include <iostream> #include <string> using namespace std; int main() { int x; while(cin>>x) { int s[50]={0}; int i=0; while(x>0) { s[i]=x%2;//逆序存放的二進制數 x=x/2; i++; } int m=0,n=0; for(int j=0;j<i;j++) cout<<s[j]<<" "; cout<<endl; for(int j=0;j<i;j++) { if(s[j]==1) { n++; while(s[j+1]==1) { j++; n++; } } else { n=0; } if(m<n) m=n; } cout<<m<<endl; } return 0; }
六、
題目描述
對字符串中的所有單詞進行倒排。
說明:
1、每個單詞是以26個大寫或小寫英文字母構成;
2、非構成單詞的字符均視為單詞間隔符;
3、要求倒排后的單詞間隔符以一個空格表示;如果原字符串中相鄰單詞間有多個間隔符時,倒排轉換后也只允許出現一個空格間隔符;
4、每個單詞最長20個字母;
輸入描述:
輸入一行以空格來分隔的句子
輸出描述:
輸出句子的逆序
輸入
I am a student
輸出
student a am I
1、字符串處理
插入
#include <iostream> #include <string> using namespace std; int main() { string s1, s2, s3; s1 = s2 = "1234567890"; s3 = "aaa"; s1.insert(5, s3); cout << s1 << endl; s2.insert(5, "bbb"); cout << s2 << endl; }
刪除
#include <iostream> #include <string> using namespace std; int main() { string s1, s2, s3; s1 = s2 = s3 = "1234567890"; s2.erase(5); s3.erase(5, 3); cout<< s1 <<endl; cout<< s2 <<endl; cout<< s3 <<endl; }
提取字符串
#include <iostream> #include <string> using namespace std; int main() { string s1 = "first second third"; string s2; s2 = s1.substr(6, 6); cout << s1 << endl; cout << s2 << endl; }
查找
#include <iostream> #include <string> using namespace std; int main() { string s1 = "apple google apple iphone"; //從0開始查找"google"的位置 int idx = s1.find("google", 0); cout << idx << endl; //統計apple出現的次數 int idx_app = s1.find("apple",0);//從0開始查找 //npos大於任何有效下標的值 int num = 0; while (idx_app != string::npos) { num++; cout << "找到的索引:" << idx_app << endl; idx_app+=5; idx_app = s1.find("apple", idx_app);//從idx_app開始查找 } cout << num << endl; }
2、
#include<iostream> using namespace std; int main() { string a; while(getline(cin,a)) { cout<<a<<endl; int s,e; e=a.length(); for(int i=a.length()-1;i>=0;i--) { if(a[i]==' ') { s=i; for(int j=s+1;j<e;j++) { cout<<a[j]; } cout<<" "; e=s; } } for(int j=0;j<e;j++) cout<<a[j]; cout<<endl; } return 0; }
3、
#include<iostream> #include<vector> #include<string> using namespace std; int main() { string str; while(getline(cin,str)) { vector<string>svec; svec.clear(); string temp=""; for(int i=0;i<str.size();++i) { if(str[i]>='a' && str[i]<='z' || str[i]>='A' && str[i]<='Z') temp+=str[i];//單個單詞的讀入 else { if(temp.size()>0) { svec.push_back(temp);//利用vector容器,挨個放入,最后逆序輸出 temp=""; } } } if(temp.size()>0) svec.push_back(temp); for(int i=svec.size()-1;i>0;--i) cout<<svec[i]<<' '; cout<<svec[0]<<endl; } return 0; }
該方法利用vector的特性,可以處理特殊字符。
4、
#include<iostream> using namespace std; int main() { string b; while(getline(cin,b)) { string a; int n=0; for(int i=0;i<b.length();i++) { if(b[i]>='a'&&b[i]<='z'||b[i]>='A'&&b[i]<='Z') a=a+b[i]; else { if(n<=a.length())//設置標志,加空格 a=a+' '; n=a.length()+1; } } int l;//邊界空格輸出條件 if(a[a.length()-1]==' ') l=a.length()-1; else l=a.length(); int s,e; e=l; for(int i=l-1;i>=0;i--) { if(a[i]==' ') { s=i; for(int j=s+1;j<e;j++) { cout<<a[j]; } cout<<" "; e=s; } } for(int j=0;j<e;j++) cout<<a[j]; cout<<endl; } return 0; }
注意設置標志,加空格。
七、
戰爭游戲的至關重要環節就要到來了,這次的結果將決定王國的生死存亡,小B負責首都的防衛工作。首都位於一個四面環山的盆地中,周圍的n個小山構成一個環,作為預警措施,小B計划在每個小山上設置一個觀察哨,日夜不停的瞭望周圍發生的情況。 一旦發生外地入侵事件,山頂上的崗哨將點燃烽煙,若兩個崗哨所在的山峰之間沒有更高的山峰遮擋且兩者之間有相連通路,則崗哨可以觀察到另一個山峰上的烽煙是否點燃。由於小山處於環上,任意兩個小山之間存在兩個不同的連接通路。滿足上述不遮擋的條件下,一座山峰上崗哨點燃的烽煙至少可以通過一條通路被另一端觀察到。對於任意相鄰的崗哨,一端的崗哨一定可以發現一端點燃的烽煙。 小B設計的這種保衛方案的一個重要特性是能夠觀測到對方烽煙的崗哨對的數量,她希望你能夠幫她解決這個問題。
輸入描述:
輸入中有多組測試數據,每一組測試數據的第一行為一個整數n(3<=n<=10^6),為首都周圍的小山數量,第二行為n個整數,依次表示為小山的高度h(1<=h<=10^9).
輸出描述:
對每組測試數據,在單獨的一行中輸出能相互觀察到的崗哨的對數。
輸入例子1:
5 1 2 4 5 3
輸出例子1:
7
1、
#include<iostream> #include<string> using namespace std; int main() { while(1) { int n,m[100]={0}; cin>>n; for(int i=0;i<n;i++) cin>>m[i]; int num=0,h; for(int i=0;i<n-1;i++) { h=m[i+1]; num++; for(int j=i+2;j<n;j++) { if(m[j]>h) { num++; h=m[j]; } } } cout<<num<<endl; } return 0; }
卡在了70%,對題目理解感覺很迷惑,環形的話,給的例子可不止有7條。上述程序瞎貓碰到死耗子了。真正的題意兩山之間的山小於等於兩山之間的高度才可以看的到
2、
#include<iostream> #include<string> using namespace std; int main() { int b[10]={}; int a[]={1,2,3,4,5}; for(int i=0;i<5;i++) { b[i]=a[(i+2)%5]; cout<<b[i]<<" "; } }
利用余數,循環輸出。
#include<iostream> #include<string> using namespace std; int main() { int n; while(cin>>n) { int m[100]={0},x[100]={0}; for(int i=0;i<n;i++) cin>>m[i]; int Max=0,index=0; for(int i=0;i<n;i++) { if(m[i]>Max) { Max=m[i]; index=i; } } for(int i=0;i<n;i++) { x[i]=m[(i+index)%n];//將最大值移到前面 } int num=0; for(int i=0;i<n-1;i++) { num++; int h=x[i+1]; if(h<=x[i]) { for(int j=i+2;j<n;j++) { if(x[j]>=h&&x[j]<=x[i]) { num++; h=x[j]; } else if(x[j]>=h&&x[j]>x[i]) { num++; break; } /*else if(x[j]<h) { break; }*/ } } int flag=0; for(int i=1;i<n-1;i++) { if(x[i]>x[n-1]) { flag=1; break; } } if(flag==1) num++; } cout<<num; } return 0; }
還是70%的通過率。問題不知道出在哪。
八、
小明同學學習了不同的進制之后,拿起了一些數字做起了游戲。小明同學知道,在日常生活中我們最常用的是十進制數,而在計算機中,二進制數也很常用。現在對於一個數字x,小明同學定義出了兩個函數f(x)和g(x)。 f(x)表示把x這個數用十進制寫出后各個數位上的數字之和。如f(123)=1+2+3=6。 g(x)表示把x這個數用二進制寫出后各個數位上的數字之和。如123的二進制表示為1111011,那么,g(123)=1+1+1+1+0+1+1=6。 小明同學發現對於一些正整數x滿足f(x)=g(x),他把這種數稱為幸運數,現在他想知道,小於等於n的幸運數有多少個?
輸入描述:
每組數據輸入一個數n(n<=100000)
輸出描述:
每組數據輸出一行,小於等於n的幸運數個數。
輸入例子1:
21
輸出例子1:
3
#include<iostream> #include<string> #include<cmath> using namespace std; int Ten(int x) { int s=0; while(x>0) { s=s+x%10; x=x/10; } return s; } int Sec(int x) { int s=0; while(x>0) { s=s+x%2; x=x/2; } return s; } int main() { int n; while(cin>>n) { int num=0; for(int i=1;i<=n;i++) { if(Ten(i)==Sec(i)) { num++; } } cout<<num<<endl; } return 0; }
九、
給你兩個集合,要求{A} + {B}。 注:同一個集合中不會有兩個相同的元素。
輸入描述:
每組輸入數據分為三行,第一行有兩個數字n,m(0 ≤ n,m ≤ 10000),分別表示集合A和集合B的元素個數。后兩行分別表示集合A和集合B。每個元素為不超過int范圍的整數,每個元素之間有個空格隔開。
輸出描述:
針對每組數據輸出一行數據,表示合並后的集合,要求從小到大輸出,每個元素之間有一個空格隔開,行末無空格。
輸入例子1:
3 3 1 3 5 2 4 6
輸出例子1:
1 2 3 4 5 6
#include<iostream> #include<string> #include<cmath> using namespace std; int main() { int n,m; while(cin>>n>>m) { int n1[10000]={0},m1[10000]={0},s[20000]={0}; for(int i=0;i<n;i++) { cin>>n1[i]; s[i]=n1[i]; } for(int i=0;i<m;i++) { cin>>m1[i]; s[i+n]=m1[i]; } for(int i=0;i<n+m-1;i++) { int Min=i; for(int j=i+1;j<n+m;j++) { if(s[Min]>s[j]) { Min=j; } } int temp=0; temp=s[i]; s[i]=s[Min]; s[Min]=temp; } for(int i=0;i<n+m-1;i++) cout<<s[i]<<" "; cout<<s[n+m-1]<<endl; } return 0; }
這樣的話沒有考慮去重。
2、
#include<iostream> #include<iterator> #include<set> using namespace std; int main() { int n, m; while (cin >> n, cin >> m) { set<int> st; int num; for (int i = 0; i < n + m; ++i) { cin >> num; st.insert(num);//自動插入排序 } int size = st.size(); set<int>::iterator it = st.begin(); for (int i = 0; i < size - 1; ++i) { cout << *it << " "; ++it; } cout << *it << endl; } return 0; }
利用set特性,去重.
3、set
#include<iostream> #include<set> using namespace std; int main() { int s[]={1,3,6,9,3,4,5}; set<int> st; for(int i=0;i<7;i++) { st.insert(s[i]); } int n; n=st.size(); cout<<n<<endl;//排好后的個數 set<int>::iterator it; for(it=st.begin();it!=st.end();it++) { cout<<*it<<" "; } cout<<endl; return 0; }
#include<iostream> #include<set> using namespace std; int main() { int s[]={1,3,6,9,3,4,5}; set<int> st; for(int i=0;i<7;i++) { st.insert(s[i]); } int n; n=st.size(); cout<<n<<endl;//排好后的個數 set<int>::iterator it=st.begin(); for(int i=0;i<n-1;i++) { cout<<*it<<" "; it++; } cout<<*it; cout<<endl; return 0; }
十、
小B感興趣的是,一個數A如果按2到A-1進制表達時,各個位數之和的均值是多少?她希望你能幫她解決這個問題? 所有的計算均基於十進制進行,結果也用十進制表示為不可約簡的分數形式。
輸入描述:
輸入中有多組測試數據,每組測試數據為一個整數A(1 ≤ A ≤ 5000)
輸出描述:
對每組測試數據,在單獨的行中以X/Y的形式輸出結果。
輸入
5 3
輸出
7/3 2/1
#include<iostream> #include<set> using namespace std; int Sum(int n)//余數和 { int s=0; for(int i=2;i<n;i++) { int y=n; while(y>0) { s=s+y%i; y=y/i; } } return s; } int Yue(int s,int n) { for(int i=n;n>=1;i--) { if(s%i==0&&n%i==0) { cout<<s/i<<'/'<<n/i<<endl; break; } } } int main() { int n; while(cin>>n) { Yue(Sum(n),n-2); } return 0; }
余數求和與最大公約數的算法。