sicily 1036 Crypto Columns

本文介绍了一个使用C++实现的程序,该程序读取输入的字符串,并按字符顺序对字符串中的字符进行排序。随后,将排序后的字符按照指定规则填充到矩阵中。此外,文章还展示了如何使用结构体和指针来管理排序过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;

typedef struct node{
	char c;
	int i;
	struct node* next; 
}Node;

#define N 10
char a[N][N];

int main()
{
 	string s,str;
	Node* headerPtr=NULL;
	Node *cur,*pre;
	while(cin>>s&&s!="THEEND")
	{
	 	int size=s.length();
	 	for(int i=0;i<size;i++)
		{
		 	Node* temp=new Node();
			temp->c=s[i];
			temp->i=i+1;
			temp->next=NULL;
			pre=cur=headerPtr;
			while(cur!=NULL)
			{
			 	if(s[i]>=cur->c)
				{
				 	pre=cur;
				 	cur=cur->next;
				}
				else
				{
				 	break;
				} 
			}
			if(cur==headerPtr)
			{
			 	temp->next=headerPtr;
				headerPtr=temp;
				
			}
			else
			{
			 	pre->next=temp;
				temp->next=cur;
			}
		}
		cin>>str;
		int sizeStr=str.length();
		int h=sizeStr/size;
		int l=0;
		while(headerPtr!=NULL)
		{
		 	//cout<<headerPtr->c<<"***"<<headerPtr->i<<endl;
			int z=headerPtr->i;
			for(int k=0;k<h;k++)
			{
			 	a[k][z-1]=str[l];
				l++;
			}
			Node* temp=headerPtr;
			headerPtr=headerPtr->next;
			delete temp;
		}
		for(int i=0;i<h;i++)
		{
		 	for(int j=0;j<size;j++)
			{
			 	cout<<a[i][j];
			}
		}
		cout<<endl;
	}
	return 0; 
}

 

转载于:https://www.cnblogs.com/growing/archive/2011/12/22/2298368.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值