1、二進(jìn)制中的"101"
輸入一個整數(shù),求其二進(jìn)制序列中用多少個'101',以及第一個'101'出現(xiàn)的位置,二進(jìn)制序列右端為起始位置。
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
string Base2Bin(string n){
char a[32];
int len_n = n.size()*4;
int m;
stringstream ss;
ss<<n;
ss>>m;
for (int i=0; i<32; i++){
a[i] = '0';
}
int j = 0;
while(m){
a[j] = m%2 + '0';
m /= 2;
j = j+1;
}
//cout<<endl;
string str = "";
for (int i=len_n-1; i>=0; i--){
str += a[i];
}
return str;
}
int main(){
string n;
int num = 0;
int begin = -1;
int last_begin = 0;
cin>>n;
string str = Base2Bin(n);
string p = "101";
while((begin = str.find(p, begin+1)) != string::npos){
if (begin != -1){
last_begin = begin;
num++;
begin = begin+1;
}
}
if (num == 0){
cout<<num<<" "<<-1<<endl;
}else{
cout<<num<<" "<<str.size()-3-last_begin<<endl;
}
return 0;
}
2、 自定義數(shù)據(jù)存儲格式
題目太長,待補(bǔ)充。。。
/*
測試用例:
,"abc",1,"b,","a,""","6""",bk,,op,"fa","cd
*/
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
string cnt[100];
freopen("in.txt", "r", stdin);
getline(cin, s);
int count = 0, flag = 0, len = s.size(), error = 0;
for (int i=0; i<len; i++){
if (s[i] == '"'){
if (flag == 0) {
if (cnt[count].size() > 0) error = 1; //如果"出現(xiàn)在字段中間 ,字段頭卻沒有
else flag = 1;
}
else if (flag == 1){
if (i < len-1 && s[i+1] != ','){
if (s[i+1] == '"') {//如果下一個還是"
cnt[count] += '"';
i += 1;
}else{//如果不是", error
error = 1;
}
}
else {
flag = 0;
if (i != len-1){//如果沒結(jié)束
count++;
i++;
}
}
}
}
else if (s[i] == ','){
if (flag == 0) count++;
else{
cnt[count] += s[i];
}
}
else {
cnt[count] += s[i];
}
}
if (flag || error){
cout<<"ERROR"<<endl;
}else{
cout<<count + 1<<endl;
for (int i=0; i<=count; i++){
if (cnt[i] == "") cout<<"--"<<endl;
else cout<<cnt[i]<<endl;
}
}
return 0;
}
//個人理解,題目中的不明確信息太多,很難全A
3、 好友推薦
有一款社交軟件APP,假設(shè)注冊用戶m人(0<m<50), 用戶編號為0~m-1,
用r[i][j]表示用戶i和用戶j之間的好友關(guān)系
(r[i][j]=0代表i和j不是好友,r[i][j]=1~9代表是好友且數(shù)值代表熟悉程度,其中r[i][j] = r[j][i]),
編制程序,輸出特定用戶i的n度好友(1<=n<=10, 1表示直接好友,2表示好友的好友),
并按推薦值降序輸出好友列表(若推薦值相同,則按好友編號升序, 推薦值是指關(guān)系熟悉度之和),
若此用戶沒有n度好友輸出-1.
輸入:
輸入一個整數(shù)T,表示有T組數(shù)據(jù)(0<T<100)
對于每組數(shù)據(jù),輸入兩行:
第一行, 輸入3個整數(shù)m, i, n, m表示用戶數(shù), 某個特定的用戶編號i, n度好友,
即表示本組數(shù)據(jù)需要輸出用戶i的n度好友
第二行, 輸入一個整型k, 接下來輸入3*k個整數(shù), 每三個組成一個關(guān)系對,
每個關(guān)系有3個整數(shù)i, j, w, 表示i和j的好友熟悉程度, 沒有輸入的關(guān)系默認(rèn)為非好友
輸出:
輸出T行, 每行對應(yīng)每組測試數(shù)據(jù)的用戶i的n度好友, 按推薦值降序輸出,
若推薦值相同, 則按好友編號升序, 沒有n度好友輸出-1
測試用例:
輸入:
2
10 5 2
13 0 3 5 0 4 9 0 6 8 0 7 5 1 2 6 1 6 3 2 9 7 3 4 3 3 5 3 3 8 3 3 9 3 5 8 9 7 8 9
10 0 2
13 0 3 5 0 4 9 0 6 8 0 7 5 1 2 6 1 6 3 2 9 7 3 4 3 3 5 3 3 8 3 3 9 3 5 8 9 7 8 9
輸出:
7 0 4 9
1 5 8 9
/*
思路:
BFS至第n層, 中間可達(dá)的需標(biāo)記
*/
#include <iostream>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
int r[55][55];
int vis[55];
typedef struct ND_Friend{
int x, sum, degree;
}NDF;
bool compare (NDF left, NDF right)
{
if (left.sum != right.sum) {
return left.sum > right.sum;
}
else return left.x < right.x;
}
queue<NDF> cq;
void BFS(int m, NDF index){
vis[index.x] = 1;
for (int i=0; i < m; i++){
if (r[index.x][i] != 0 && vis[i] != 1){
//cout<<i<<" "<<index.degree<<"degree"<<endl;
NDF nd;
nd.x = i;
nd.sum = index.sum + r[index.x][i];
nd.degree = index.degree - 1;
cq.push(nd);
vis[i] = 1;
}
}
}
int main(){
int T;
freopen("in.txt", "r", stdin);
cin>>T;
while(T--){
int m, index, n;
cin>>m>>index>>n;
memset(r, 0, sizeof(r));
memset(vis, 0, sizeof(vis));
int k, i, j, w;
cin>>k;
for (int p=0; p<k; p++){
cin>>i>>j>>w;
r[i][j] = w;
r[j][i] = w;
}
NDF root;
root.x = index;
root.sum = 0;
root.degree = n;
cq.push(root);
while(!cq.empty()){
NDF ndf = cq.front();
if (ndf.degree == 0){
break;
}
cq.pop();
BFS(m, ndf);
}
if (cq.empty()){
cout<<"-1"<<endl;
break;
}
int p = 0;
NDF nd_friend[55];
while(!cq.empty()){
NDF ndf = cq.front();
cq.pop();
nd_friend[p++] = ndf;
//cout<<ndf.x<<" "<<ndf.sum<<endl;
}
sort(nd_friend, nd_friend+p, compare);
for (int pp=0; pp<p; pp++){
cout<<nd_friend[pp].x<<" ";
}
cout<<endl;
}
return 0;
}
/*
//遺留問題:
第二個測試用例:
0->3->8 推薦值8
0->7->8 推薦值14
都可達(dá),推薦值不同,測試用例取了第一種,沒有給出解釋
*/