ジャガイモの春の実習問題の惨烈な教訓
4400 ワード
今日ジャガイモの春のC++実習生の募集試験問題をして、多くの間違いではない間違いがあって、ここでいくつかの重要な間違いを選んで、自分の参考に供して、後で二度と犯さないようにします.
一、プログラミングの問題は簡単ですが、間違っています.
タイトル:
配列を入力し、その逆配列を求めます.入力:1 2 3 4、出力4 3 2
一部のコードを記入するフレームワークが表示されます.
上記のコードでは、次の点に注意してください.
1.
tempの最後の要素はend()ではなくend()-1であり、end()は量の最後の要素の次の要素を指すことに注意してください.
2.戻りタイプがポインタまたは参照の場合、staticローカル変数の使用を強調する必要があります.そうしないと、通常のローカル変数を使用して、関数呼び出しが終了すると、変数が解放され、ポインタまたは参照が不確定なオブジェクトを指すようにします.
3.クラス名を使用して直接呼び出すことができる静的メンバー関数.
二、
int i=001+010+100; cout<出力結果:109
分析:
001010はいずれも0で始まる8進数で、それぞれ1,8に等しいため、上式は109に等しい
一反三を挙げる:
int i=0x001+0x010+100; cout<出力結果:117
分析:
0 X 001,0 X 010はいずれも0 xで始まる16進数で、それぞれ1,16に等しいため、上式は117に等しい
三、
プログラミングの問題:
1つの数字Aについて、その「DA(DAは1桁の整数)」の数PAはAの中のすべてのDAを綴った数である.例えば、1566のPA数は66で、それは2つの6があるからである.現在、2つの数AとBがあり、DAはそれぞれDA 1とDA 2であり、AとBのPA書PA 1+PA 2?0例えばA:1566、DA 1=6;そしてPA 1=66
B:4231554,DA2=5;するとPA 2=55、PA 1+PA 2=66+55=121
A,Bの範囲が広く,実際にはlong longの範囲を超えているため,ビッグデータ加算に属する.
四、クラスのメンバー演算子として再ロードできない演算子はどれですか?
一、プログラミングの問題は簡単ですが、間違っています.
タイトル:
配列を入力し、その逆配列を求めます.入力:1 2 3 4、出力4 3 2
一部のコードを記入するフレームワークが表示されます.
#include<iostream>
#include <vector>
#include <string>
using namespace std;
class example
{
public:
static const vector<int>& reverse1(const vector<int>& r)
{
vector<int> temp(r.begin(),r.end());
vector<int>::iterator vbegin=temp.begin();
vector<int>::iterator vend=temp.end()-1;
while(vbegin<vend)
{
swap(*vbegin++,*vend--);
}
static const vector<int> temp2(temp.begin(),temp.end());
return temp2;
}
protected:
private:
};
void main()
{
int d[]={1,2,3,4,5};
vector<int> f(d,d+5);
const vector<int> s=example::reverse1(f);
copy(s.begin(),s.end(),ostream_iterator<int>(cout," "));
}
上記のコードでは、次の点に注意してください.
1.
vector<int>::iterator vend=temp.end()-1;
tempの最後の要素はend()ではなくend()-1であり、end()は量の最後の要素の次の要素を指すことに注意してください.
2.戻りタイプがポインタまたは参照の場合、staticローカル変数の使用を強調する必要があります.そうしないと、通常のローカル変数を使用して、関数呼び出しが終了すると、変数が解放され、ポインタまたは参照が不確定なオブジェクトを指すようにします.
3.クラス名を使用して直接呼び出すことができる静的メンバー関数.
二、
int i=001+010+100; cout<出力結果:109
分析:
001010はいずれも0で始まる8進数で、それぞれ1,8に等しいため、上式は109に等しい
一反三を挙げる:
int i=0x001+0x010+100; cout<出力結果:117
分析:
0 X 001,0 X 010はいずれも0 xで始まる16進数で、それぞれ1,16に等しいため、上式は117に等しい
三、
プログラミングの問題:
1つの数字Aについて、その「DA(DAは1桁の整数)」の数PAはAの中のすべてのDAを綴った数である.例えば、1566のPA数は66で、それは2つの6があるからである.現在、2つの数AとBがあり、DAはそれぞれDA 1とDA 2であり、AとBのPA書PA 1+PA 2?0例えばA:1566、DA 1=6;そしてPA 1=66
B:4231554,DA2=5;するとPA 2=55、PA 1+PA 2=66+55=121
A,Bの範囲が広く,実際にはlong longの範囲を超えているため,ビッグデータ加算に属する.
#include<iostream>
#include <vector>
#include <string>
using namespace std;
class example
{
public:
static const string& calc(const string& stra,unsigned int a,const string& strb,unsigned int b)// a,b
{
int count_a=0;
int count_b=0;
int i=0,j=0;
// stra a
for(string::const_iterator vbegin=stra.begin();vbegin!=stra.end();++vbegin)
{
if ((*vbegin-'0')==a)
{
count_a++;
}
}
// strb b
for(string::const_iterator vbegin=strb.begin();vbegin!=strb.end();++vbegin)
{
if ((*vbegin-'0')==b)
{
count_b++;
}
}
int na=count_a;
int nb=count_b;
int length=na>nb?na:nb;
int* pa=new int[length+1];// stra a , 。
int* pb=new int[length+1];
memset(pa,0,(length+1)*sizeof(int));
memset(pb,0,(length+1)*sizeof(int));
// pa
while (count_a--)
{
pa[i++]=a;
}
// pb
while (count_b--)
{
pb[j++]=b;
}
for (int i=0;i<length;i++)
{
pa[i]=pa[i]+pb[i];
}
int ncount=0;//
for (int i=0;i<length;i++)
{
if (pa[i]>9)
{
int temp=pa[i]/10;
pa[i+1]+=temp;
pa[i]=pa[i]%10;
ncount=length;
}
else
{
ncount=length-1;
continue;
}
}
// pa , pa 。 :<a target=_blank href="http://blog.csdn.net/xiamentingtao/article/details/46362577">http://blog.csdn.net/xiamentingtao/article/details/46362577</a>
static string s1;
//
for (int i=ncount;i>=0;i--)
{
char s=pa[i]+'0';
s1.append(1u,s);//string& append (size_t n, char c);
}
delete[] pa;
pa=NULL;
delete[] pb;
pb=NULL;
return s1;
}
protected:
private:
};
void main()
{
const string vi=example::calc("12455555555555555555555555555",5,"45666666666666666666666666666666666666666666666666112",6);
}
四、クラスのメンバー演算子として再ロードできない演算子はどれですか?
1. 。 . ,.* ,:: ,? : ,sizeof,typeid ,
2. , + int 。
3. 。 。
8.4 。
8.5 。
8.6 。 + , + 。