ファーウェイ機試練手(c++コード)
3522 ワード
12336544、1750などの整数を入力し、最後のビットから逆さまに出力します.最後に0であれば出力しません.出力された数字は重複しないので、上の出力は456321と571です.入力-175のような負の数であれば、出力-571です.
プログラミングの場合、if条件の「(」、「)」カッコが一致しないことがよくありますが、作成プログラムはif文の丸カッコが正しく一致しているかどうかを検出してください.if((a==1)&(b==1))などの文に現れる左かっこと右かっこの数を同時に出力するのは正しいが、if((a==1))&&(b==1))は間違っている.if文の一番外側には少なくとも1対の括弧があることに注意してください.ヒント:スタックで行います.
入力:if((a=1)&&(b=1))
出力:RIGTH 3
入力:if((a=1)&&(b=1))
出力:WRONG 3 4
m個の文字列と整数nを入力し、文字列MをN単位のセグメントに変換し、不足したビット数を0で補正します.
n=8 m=9のように、
123456789区分:12345678 9000000
123:123,000,000
整数化は2進数、32ビット長です.そして逆順出力
1つの数を与え、例えば20を与え、その後、いくつかの数字1 3 5 7 8 1 3 5 7 8 0 1を与え、5+7+8=20である
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
string str1;
cin>>str;
string::iterator iter =str1.begin();
for(int i=str.length()-1;i>=0;i--) {
if(str[i]=='-')
cout<<"-";
else if(str[i]=='0'&&i==str.length()-1)
continue;
else if(str1.find(str[i])==-1)
{
str1.insert(iter,str[i]);
iter++;
}
}
cout<<str1<<endl;
return 0;
}
プログラミングの場合、if条件の「(」、「)」カッコが一致しないことがよくありますが、作成プログラムはif文の丸カッコが正しく一致しているかどうかを検出してください.if((a==1)&(b==1))などの文に現れる左かっこと右かっこの数を同時に出力するのは正しいが、if((a==1))&&(b==1))は間違っている.if文の一番外側には少なくとも1対の括弧があることに注意してください.ヒント:スタックで行います.
入力:if((a=1)&&(b=1))
出力:RIGTH 3
入力:if((a=1)&&(b=1))
出力:WRONG 3 4
#include <iostream>
#include <stack>
#include <string>
using namespace std;
stack <char> p;
string str;
int main()
{
cin>>str;
int num1=0,num2=0;
for(int i=0;i<str.length();i++) {
if(str[i]=='(')
{
p.push(str[i]);
num1++;
}
else if(str[i]==')')
{
num2++;
if(p.empty())
{
cout<<"WRONG"<<" "<<num1<<" "<<num2<<endl;
return 0;
}
else {
p.pop();
}
}
}
if(p.empty()==1)
cout<<"RIGHT"<<" "<<num1<<" "<<num2<<endl;
else
cout<<"WRONG"<<" "<<num1<<" "<<num2<<endl;
return 0;
}
m個の文字列と整数nを入力し、文字列MをN単位のセグメントに変換し、不足したビット数を0で補正します.
n=8 m=9のように、
123456789区分:12345678 9000000
123:123,000,000
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
string str1;
int n;
cin>>str;
cin>>n;
int m=str.length();
int k=m/n;
if(k==0) {
for(int i=m;i<n;i++)
str.append("0");
}
else {
for(int i=m;i<(k+1)*n;i++)
{
//str.reserve((k+1)*n);
str.append("0");
}
}
int j=0,l=n;
while(k>-1) {
str1=str.substr(j,l);
cout<<str1<<endl;
j=l;
l=l+n;
k--;
}
return 0;
}
整数化は2進数、32ビット長です.そして逆順出力
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
int num,n=0;
cin>>num;
while(num) {
str.push_back(num%2+'0');
num/=2;
n++;
}
while(n<32)
{
str.push_back('0');
n++;
}
for(int i=0;i<str.length();i++)
cout<<str[i];
return 0;
}
1つの数を与え、例えば20を与え、その後、いくつかの数字1 3 5 7 8 1 3 5 7 8 0 1を与え、5+7+8=20である
#include<iostream>
#include<string>
using namespace std;
int find(int m,int num[],bool l[],int n)
{
if(m==0)
return 1;
if((m>0&&n<0)||m<0)
return 0;
if(find(m-num[n],num,l,n-1))
{
l[n]=true;
return 1; // 1
}
else
return find(m,num,l,n-1);
}
int main()
{
int num[100],m,n=-1;
cin>>m;
while(cin>>num[++n]);
bool *l=new bool[n];
for(int i=0;i<n;i++)
l[i]=false;
if(find(m,num,l,n-1))
{
for(int i=0;i<n;i++)
cout<<l[i]<<" ";
cout<<endl;
}
else
cout<<"NO"<<endl;
delete []l;
return 0;
}