笔试题练习(九)

来源:互联网 发布:飞扬软件维护费 编辑:程序博客网 时间:2024/05/18 04:32

1,有一个数组a[1000]存放0--1000;要求每隔二个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。

#include <iostream>
using namespace std;

struct Node
{
    
int value;
    
bool isDeleted;
};
int main()
{
    Node a[
1001];
    
int i,n = 12;
    
for (i = 0; i < n; ++i)
    {
        a[i].value 
= i;
        a[i].isDeleted 
= false;
        
    }
    
int cur = 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;
    
return 0;
}

2,有一个整数数组,现要求实现这个整数数组的循环右移。如:12345 则循环右移两位后结果是:45123

#include <iostream>
using namespace std;

void shiftRight(int a[], int n,int m)
{
//循环右移m位
    int pre = 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;
        }
    }
}
int main()
{
    
int a[] = {1,2,3,4,5,6,7,8};
    shiftRight(a,
8,2);
    
return 0;
}

 3, 以单词为最小单位翻转字符串

#include <iostream>
#include 
<stack>
#include 
<string>
using namespace std;

string reverse_string_word_by_word(string input)
{
    stack
<string> s;
    
char chSplit = ' ';
    size_t pos 
= input.find_first_of(chSplit);
    size_t lastPos 
= input.find_last_of(chSplit);
    size_t nBegin 
= 0;
    size_t len 
= 0;
    
string tmpStr;
    size_t nWhiteSpace 
= 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);
    }
    
string result = "";
    
while (!s.empty())
    {
        
string tmp = s.top();
        result.append(tmp.c_str());
        
if (--nWhiteSpace)
        {
            result.append(
" ");
        }
        s.pop();
    }
    
return result;
}
int main()
{
    
string strData = "the house is blue";
    
string result = reverse_string_word_by_word(strData);
    cout 
<< result << endl;
    
return 0;
}

4,题目描述:

设有n个正整数,将它们联接成一排,组成一个最大的多位整数。

例如:

 

n=2时,2个整数32321连接成的最大整数为:32321

n=4时,4个整数5531312, 33 联接成的最小整数为:553332131312

#include <iostream>
#include 
<vector>
#include 
<algorithm>
using namespace std;

class MyCompator
{
public:
    
bool operator()(char* lhsStr,char* rhsStr)
    {
        
if (lhsStr == NULL) 
            
return false
        
if (rhsStr == NULL) 
            
return true;
        
const char* temps1 = lhsStr;
        
const char* 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
++;
            }
        }
        
// 两个相等
        return true;
    }
};
int main() 

    
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; 
    
return 0;
}

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

 

abc20%的概率输出,a30%的概率输出,de50%的概率输出,输出10条记录

以下为一次输出的结果,多次输出的结果可能不相同。

abc

a

de

de

abc

de

a

de

a

de

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Scanner;
import java.util.Vector;

//记录类
class Record
{
    
private String name = null;
    
private int num = 0;
    
public Record(String name, int num)
    {
        
this.name = name;
        
this.num = num;
    }
    
public String getName() 
    {
        
return name;
    }
    
public void setName(String name) 
    {
        
this.name = name;
    }
    
public int getNum() 
    {
        
return num;
    }
    
public void setNum(int num)
    {
        
this.num = num;
    }
}
public class test 
{

    
public static void main(String[] args) throws Exception
    {
        
int N,i;
        Scanner scanner 
= new java.util.Scanner(System.in);
        N 
= scanner.nextInt();
        FileInputStream input 
= null;
        InputStreamReader bufferInput 
= null;
        BufferedReader reader 
= null;
        Vector
<Record> records = new java.util.Vector<Record>();
        
try
        {
            input 
= new FileInputStream("D://data1.txt");
            bufferInput 
= new InputStreamReader(input);
            reader 
= new BufferedReader(bufferInput);
            String line 
= "";
            String strError 
= "//t";
            String strSpliter 
= "/t";//分隔符
            int total = 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);
                }
                String name 
= "";
                
int num = 0;
                
try
                {
                    name 
= record[0];
                    num 
= Integer.parseInt(record[1]);
                    
if (num > 100)
                    {
//单个记录超过100
                        System.exit(1);
                    }
                    total 
+= num;
                }
                
catch(java.lang.Exception ex)
                {
//解析错误
                    System.exit(1);
                }
                Record node 
= new Record(name,num*N/100);
                records.add(node);
            }
            
if (total != 100)
            {
//概率和不为100
                System.exit(1);
            }
            
int size = records.size();
            
int pos = -1,count = 0;
                
            
for (i = 0; count != N; ++i)
            {
                Random random 
= new java.util.Random();
                
//在记录集合中随机取一条记录来检验
                pos = random.nextInt(size);
                Record temp 
= records.elementAt(pos);
                
if (temp.getNum() != 0)
                {
//若此记录可以被输出(次数还没减到0)
                    
//输出记录并将其次数减去1
                    System.out.println(temp.getName());
                    count
++;
                    temp.setNum(temp.getNum()
-1);
                }
            
            }
            
        }
        
catch(java.lang.Exception ex)
        {
            System.exit(
1);
        }
        
finally
        {
//关闭文件
            input.close();
            bufferInput.close();
            reader.close();
        }
    }

}

原创粉丝点击