NUDT程序设计B测试1题解
删空格:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cctype>
char s[10001];
int main(){
fgets(s,10001,stdin);
bool flag=false;
int head=0,tail=strlen(s)-1;
for(int i=0;i<strlen(s);++i){
if(isalpha(s[i])){break;}
if(s[i]==' ')head=i;
}
for(int i=tail;i>=0;--i){
if(isalpha(s[i])){break;}
if(s[i]==' ')tail=i;
}
for(int i=head+1;i<=tail;++i){
printf("%c",s[i]);
}
printf("\n");
return 0;
} Ps:这题啥也不写仿佛也能过。。。
排序:
使用C++里的排序
#include<algorithm> std::sort(a,a+n);
找字串和数字:
#include <iostream>
#include <cstring>
using namespace std;
void subDig(char *s, int &p, int &d)
{
/**********Program**********/
for(int i=0;i<strlen(s);++i){
if(isdigit(s[i])){sscanf(s+i,"%d",&d);d*=2;break;}
}
for(int i=0;i<strlen(s)-2;++i){
bool ok=false;
if(s[i]=='x'&&s[i+1]=='y'&&s[i+2]=='z'){
p=i;
}
}
/********** End **********/
}
int main()
{
char s[]="abc123xyzabcdxyz";
int p, d;
subDig(s, p, d);
cout<<p<<" "<<d<<endl;
return 0;
}
链表删素数:
/*--------------------------------------------------
注意:部分源程序给出如下。请勿改动主函数main和其它
函数中的任何内容,不要修改或删除"program"和
"End"两行注释,仅在其中填入所编写的代码。
--------------------------------------------------*/
#include <iostream>
using namespace std;
struct ListNode
{
int data;
ListNode *next;
};
bool isPrime(int n){
if(n<=1)return false;
for(int i=2;i*i<=n;++i){
if(n%i==0)return false;
}
return true;
}
ListNode *deletesushu(ListNode *head)
{
/**********Program**********/
ListNode *tmp=head;ListNode *prev=tmp;
while(tmp!=NULL){
ListNode *de=tmp;
if(isPrime(tmp->data)){
if(tmp==head){
head=head->next;
prev=tmp=head;
delete de;
}
else{
if(tmp==prev)tmp=tmp->next;
else{
prev->next=tmp->next;
tmp=tmp->next;
delete de;
}
}
else{
prev=tmp;
tmp=tmp->next;
}
}
return head;
/********** End **********/
}
int main()
{
struct ListNode *head=NULL,*t1,*t2,*t;
int n;
cin>>n;
head = new ListNode;
cin>>head->data;
t2=head;
for(int i=0;i<n-1;i++)
{
t1=new ListNode;
cin>>t1->data;
t2->next=t1;
t2=t1;
}
t2->next = NULL;
head = deletesushu(head);
t=head;
while(t)
{
cout<<t->data<<" ";
t=t->next;
}
cout<<endl;
return 0;
} 链表插入 cnt(),add():
/*--------------------------------------------------
注意:部分源程序给出如下。请勿改动主函数main和其它
函数中的任何内容,不要修改或删除"program"和
"End"两行注释,仅在其中填入所编写的代码。
--------------------------------------------------*/
#include <iostream>
using namespace std;
struct tNode
{ int data;
tNode *next;
};
tNode *head;
void createLink(int a[], int n);
int cnt();
void add(int n);
/**********Program**********/
void add(int n){
tNode *tmp=head;
bool ok=false;
while(tmp->next!=NULL){
if(tmp->data==n){ok=true;}
tmp=tmp->next;
}
if(tmp->data==n)ok=true;
if(!ok){
tNode *tail=new tNode();
tail->next=NULL;
tail->data=n;
tmp->next=tail;
}
}
int cnt(){
int ans=0;
tNode *that=head;
while(that!=NULL){
if(that->data%2)ans++;
that=that->next;
}
return ans;
}
/********** End **********/
void createLink(int a[], int n)
{ tNode *node;
head = NULL;
for(int i=n-1; i>=0; i--)
{ node = new tNode;
node->data = a[i];
node->next = NULL;
node->next = head;
head = node;
}
}
int last()
{ tNode *p=head;
int n;
while(p)
{ n = p->data;
p = p->next;
}
return n;
}
int main()
{ int a[]={1,2,3,4,5};
createLink(a, 5);
cout<<cnt()<<" "; // 链表中有5个节点,3个奇数
int m ;
cin>>m;
add(m);
cout<<last()<<endl; // 最后一个节点的值应该是7
return 0;
} 删除链表中的重复节点
#include<cstdio>
#include<algorithm>
#include<set>
#include<iostream>
using namespace std;
set<int> s;
struct Node {
int num;
Node *next;
};
Node * dubdel(Node *head){
/*********Program*********/
s.clear();
Node *tmp=head;Node *prev=head;
while(tmp!=NULL){
if(!s.count(tmp->num)){
s.insert(tmp->num);
prev=tmp;
tmp=tmp->next;
}
else{
prev->next=tmp->next;
Node *tt=tmp;
tmp=tmp->next;
delete tt;
}
}
return head;
/********* End *********/
}
void deleteList(Node *head)
{
Node *tmp;
while (head)
{
tmp = head->next;
delete head;
head = tmp;
}
}
void printList(Node *head)
{
while(head)
{
cout<<head->num<<" ";
head = head->next;
}
cout<<endl;
}
Node * createList(int a[], int len)
{
Node *head = NULL;
if(len<1)
return head;
for(int i = len - 1; i >= 0; i--)
{
Node *tmp = new Node;
tmp->num = a[i];
tmp->next = head;
head = tmp;
}
return head;
}
int main()
{
int len;
cin>>len;
int s[len];
for(int j = 0; j < len; j++)
{
cin>>s[j];
}
Node * head = createList(s,len);
head = dubdel(head);
printList(head);
deleteList(head);
return 0;
}
查看4道真题和解析