1,有一个数组a[1000]存放0--1000;要求每隔二个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。
usingnamespacestd;
structNode
{
intvalue;
boolisDeleted;
};
intmain()
{
Nodea[1001];
inti,n=12;
for(i=0;i<n;++i)
{
a[i].value=i;
a[i].isDeleted=false;
}
intcur=0,count,num=n;
while(num!=1)
{
count=0;
while(count!=2)
{
cur=(cur+1)%n;
if(a[cur].isDeleted==false)
{
count++;
}
}
a[cur].isDeleted=true;
while(a[cur].isDeleted!=false)
{
cur=(cur+1)%n;
}
--num;
}
cout<<cur<<endl;
return0;
}
2,有一个整数数组,现要求实现这个整数数组的循环右移。如:1,2,3,4,5 则循环右移两位后结果是:4,5,1,2,3。
usingnamespacestd;
voidshiftRight(inta[],intn,intm)
{//循环右移m位
intpre=0,cur=(pre+m)%n,curNum=a[pre],tmp,count=0;
if(n%2==0)
{//偶数
//移动偶数位
while(count!=n/2)
{
tmp=a[cur];
a[cur]=curNum;
curNum=tmp;
pre=cur;
cur=(cur+m)%n;
++count;
}
//移动奇数位
count=0;
pre=++pre;
cur=(pre+m)%n;
curNum=a[pre];
while(count!=n/2)
{
tmp=a[cur];
a[cur]=curNum;
curNum=tmp;
pre=cur;
cur=(cur+m)%n;
++count;
}
}
else
{//奇数
while(count!=n)
{
tmp=a[cur];
a[cur]=curNum;
curNum=tmp;
pre=cur;
cur=(cur+m)%n;
++count;
}
}
}
intmain()
{
inta[]={1,2,3,4,5,6,7,8};
shiftRight(a,8,2);
return0;
}
3, 以单词为最小单位翻转字符串
#include<stack>
#include<string>
usingnamespacestd;
stringreverse_string_word_by_word(stringinput)
{
stack<string>s;
charchSplit='';
size_tpos=input.find_first_of(chSplit);
size_tlastPos=input.find_last_of(chSplit);
size_tnBegin=0;
size_tlen=0;
stringtmpStr;
size_tnWhiteSpace=0;
while(true)
{
++nWhiteSpace;
if(pos==input.npos)
{
len=input.length()-nBegin;
tmpStr=input.substr(nBegin,len);
s.push(tmpStr);
break;
}
len=pos-nBegin;
tmpStr=input.substr(nBegin,len);
s.push(tmpStr);
nBegin=pos+1;
pos=input.find(chSplit,nBegin);
}
stringresult="";
while(!s.empty())
{
stringtmp=s.top();
result.append(tmp.c_str());
if(--nWhiteSpace)
{
result.append("");
}
s.pop();
}
returnresult;
}
intmain()
{
stringstrData="thehouseisblue";
stringresult=reverse_string_word_by_word(strData);
cout<<result<<endl;
return0;
}
4,题目描述:
设有n个正整数,将它们联接成一排,组成一个最大的多位整数。
例如:
n=2时,2个整数32,321连接成的最大整数为:32321,
n=4时,4个整数55,31,312, 33 联接成的最小整数为:553332131312
#include<vector>
#include<algorithm>
usingnamespacestd;
classMyCompator
{
public:
booloperator()(char*lhsStr,char*rhsStr)
{
if(lhsStr==NULL)
returnfalse;
if(rhsStr==NULL)
returntrue;
constchar*temps1=lhsStr;
constchar*temps2=rhsStr;
while(!((*temps1==0)&&(*temps2==0)))
{
//字符串相加的效果
if(*temps1==0)
{
temps1=rhsStr;
}
if(*temps2==0)
{
temps2=lhsStr;
}
//比较
if(*temps1!=*temps2)
{
return*temps1>*temps2;
}
else
{
temps1++;
temps2++;
}
}
//两个相等
returntrue;
}
};
intmain()
{
char*ps[]={"55","31","312","33","321"};
vector<char*>v(ps,ps+sizeof(ps)/sizeof(*ps));
sort(v.begin(),v.end(),MyCompator());
copy(v.begin(),v.end(),ostream_iterator<char*>(cout,""));
cout<<endl;
return0;
}
5、编程题
输入:N(整数)
输入:数据文件A.txt,不超过6条记录,字符串长度不超过15个字节
文件格式如下:
字符串"t数字"n
说明:
每行为1条记录;字符串中不含有"t。
数字描述的是该字符串的出现概率,小于等于100的整数。
多条记录的出现概率之和为100,如果A.txt不满足该条件,程序则退出;
如果文件格式错误,程序也退出。
要求:
编写一个程序,输入为N(正整数),读入文件A.txt,按照字符串出现概率随机地输出字符串,输出N条记录
例如:
输入文件A.txt
abc"t20
a"t30
de"t50
输入为:10
即 abc有20%的概率输出,a有30%的概率输出,de有50%的概率输出,输出10条记录
以下为一次输出的结果,多次输出的结果可能不相同。
abc
a
de
de
abc
de
a
de
a
de
importjava.io.FileInputStream;
importjava.io.InputStreamReader;
importjava.util.Random;
importjava.util.Scanner;
importjava.util.Vector;
//记录类
classRecord
{
privateStringname=null;
privateintnum=0;
publicRecord(Stringname,intnum)
{
this.name=name;
this.num=num;
}
publicStringgetName()
{
returnname;
}
publicvoidsetName(Stringname)
{
this.name=name;
}
publicintgetNum()
{
returnnum;
}
publicvoidsetNum(intnum)
{
this.num=num;
}
}
publicclasstest
{
publicstaticvoidmain(String[]args)throwsException
{
intN,i;
Scannerscanner=newjava.util.Scanner(System.in);
N=scanner.nextInt();
FileInputStreaminput=null;
InputStreamReaderbufferInput=null;
BufferedReaderreader=null;
Vector<Record>records=newjava.util.Vector<Record>();
try
{
input=newFileInputStream("D://data1.txt");
bufferInput=newInputStreamReader(input);
reader=newBufferedReader(bufferInput);
Stringline="";
StringstrError="//t";
StringstrSpliter="/t";//分隔符
inttotal=0;
while((line=reader.readLine())!=null)
{
if(line.indexOf(strError)!=-1)
{//含有/t
System.exit(1);
}
String[]record=line.split(strSpliter);
if(record.length!=2)
{//记录格式错误
System.exit(1);
}
Stringname="";
intnum=0;
try
{
name=record[0];
num=Integer.parseInt(record[1]);
if(num>100)
{//单个记录超过100
System.exit(1);
}
total+=num;
}
catch(java.lang.Exceptionex)
{//解析错误
System.exit(1);
}
Recordnode=newRecord(name,num*N/100);
records.add(node);
}
if(total!=100)
{//概率和不为100
System.exit(1);
}
intsize=records.size();
intpos=-1,count=0;
for(i=0;count!=N;++i)
{
Randomrandom=newjava.util.Random();
//在记录集合中随机取一条记录来检验
pos=random.nextInt(size);
Recordtemp=records.elementAt(pos);
if(temp.getNum()!=0)
{//若此记录可以被输出(次数还没减到0)
//输出记录并将其次数减去1
System.out.println(temp.getName());
count++;
temp.setNum(temp.getNum()-1);
}
}
}
catch(java.lang.Exceptionex)
{
System.exit(1);
}
finally
{//关闭文件
input.close();
bufferInput.close();
reader.close();
}
}
}