首页 > 试题广场 >

Base64

[编程题]Base64
  • 热度指数:424 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
Base64是一种基于64个可打印字符来表示二进制数据的表示方法,它Base64规定每76个字符后需要加上一个回车换行。例如:
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNp
需要输出成
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
IHNp
现在给你一串加密后的字符串,请你在每76个字符后面加上一个换行符。

输入描述:
输入有多组数据。

每组数据占一行,由字母、数字等非空白符号组成的字符串,长度不超过50000。


输出描述:
对应每一组输入,输出相应的转换后的文本。

每一组数据之后输出一个空行作为间隔。
示例1

输入

TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHN
IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGh

输出

TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
IHN

IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
dGh
思路:使用string的insert成员函数。
定义循环变量i,初始化i = 76,如果 i < str.length(),在i位置插入'\n',下一个插入的位置应该是i += 77(因为相比于原来的字符串多了一个换行符,所以下一个插入的位置应该在i之后的第77个字符)。
PS:(题意如果源字符串的长度正好是76的整数倍,需不需要在最后加上一个换行,运行结果显示加上会有有错误>_<)。
循环结束后,按照提议连续输出两个换行。
代码如下所示:
// write your code here cpp
#include <iostream>
#include <string>
using namespace std;
string change(string str)
    {
    int i = 76;
    for(i = 76; i < str.length(); i += 77)
        str.insert(i, 1, '\n');
    return str;
}
int main()
{
    string s;
    int len;
    while(cin >> s)
    {
        len = s.length();    //记录源字符串的长度
        cout << change(s) << "\n\n";
        //如果源字符串长度是76的整数倍,在s后面补一个换行符
//        if(len % 76 == 0)
//            cout << endl;
     }
    return 0;
}
 
编辑于 2015-08-19 11:47:51 回复(0)
更多回答

python解法,应该是最简单的了吧:

while True:
    try:
        a = input()
        while len(a) > 76:
            print(a[:76])
            a = a[76:]
        print(a)
        print()
    except:
        break
编辑于 2017-09-12 10:04:41 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            String str=sc.next();
            int len=str.length();
            int index=0;
            while(len>index*76){
                String subStr="";
                if(len>(index+1)*76)
                    subStr=str.substring(index*76,(index+1)*76);
                else
                    subStr=str.substring(index*76);
                System.out.println(subStr);
                index++;
            }
            System.out.println();
        }
    }
}

发表于 2018-09-20 18:09:39 回复(0)
#include <cstdio>
#include <limits.h>
#include <vector>
#include <functional>
#include <string>
#include <iostream>
using namespace std;
int main()
{
 //freopen("in.txt", "r", stdin);
 string s;
 while (cin >> s)
 {
  int count = 0;
  for (auto& c : s)
  {
   ++count;
   if (count == 77) { count = 1; cout << endl; }
   cout << c;
  }
  cout << endl << endl;
 }
 return 0;
}

发表于 2017-07-27 16:54:56 回复(0)
import java.io.*;
public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String s = null;
		while ((s = bf.readLine()) != null) {
			int i;
			for (i = 0; i < s.length() - 76; i += 76)
				System.out.println(s.substring(i, i + 76));
			System.out.println(s.substring(i));
			System.out.println();
		}
	}
}

发表于 2016-10-13 01:23:46 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    while(getline(cin, str))
    {
        int i = 0;
        while(str[i] != '\0')
        {
            if(i%76 == 0 && i != 0)
                cout << endl;
            cout << str[i++];
        }
        cout << endl <<endl;
    }
    return 0;
}
发表于 2019-07-21 21:31:55 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s;
	while (getline(cin, s))
	{
		string res;
		int i = 1;
		for (; i <= s.size(); i++)
		{
			if (i % 76 != 0)
				res += s[i - 1];		
			else
			{			
				res += s[i - 1];
				res += "\n";
			}
		}
		if (i % 76 != 1)
			res += "\n";
		cout << res << endl;
	}
	return 0;
}

发表于 2017-04-23 18:38:38 回复(1)
try:
    while 1:
        s = raw_input()
        a = len(s)
        if a <= 76:
            print s
        else:
            n = a / 76
            m = a % 76
            for i in xrange(n):
                print s[76 * i:76 * (i + 1)]
            if m != 0:
                print s[-m:]
        print ''
except:
    pass

发表于 2017-01-01 21:01:08 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner cin =new Scanner(System.in);
        while(cin.hasNext()){
            String s=cin.next();
            int lines=s.length()/76;
            for(int i=0;i<lines;i++){
                System.out.println(s.substring(i*76,(i+1)*76));
            }
            System.out.println(s.substring(s.length()-s.length()%76,s.length()));
            System.out.println();
        }
    }
}
格式错误:您的程序输出的格式不符合要求(比如空格和换行与要求不一致)
不是道格式哪里出错了
发表于 2016-07-04 20:52:16 回复(0)
// write your code here
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);		
		while(sc.hasNext()){
			String str = sc.nextLine();
			Main.getStr(str);	
			
			
		}
		
	}
	public static void getStr(String str){
		int length = str.length();
		for(int i = 0; i < length; i += 76){
			if(length - i >= 76)
				System.out.println(str.substring(i, i+76));
			else{
				System.out.println(str.substring(i));
			}
		}
        System.out.println();
	}
}

发表于 2016-07-04 19:13:34 回复(0)
#include <iostream>
#include <string>
using namespace std;
int main() {
    string str;
    while(getline(cin, str)) {
    	unsigned int len = str.size();
      	for(unsigned int i = 1; i <= len; i++) {
      		cout<<str[i-1];
      		if( i % 76 == 0)
      			cout<<endl;
      		else if (i== len)
      			cout<<endl;
        }
        cout<<endl;
    }
}

发表于 2016-07-04 12:52:37 回复(0)
很难比这再短了吧。。。核心代码4行。。。
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str;
	while (cin >> str)
	{
		for (int i = 0; i <= str.size() / 76; ++i)
			cout << str.substr(i * 76, 76) << endl;
		if (str.size() / 76 * 76 != str.size())
			cout << endl;
	}
	return 0;
}

发表于 2015-12-19 15:49:43 回复(0)
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
using namespace std;

int  main(){

char a;
int num = 0;
while (scanf("%c",&a)!=EOF){
if (a == '\n'){
num = 0;
printf("\n\n");
}
else{
if (num == 76)
{
num = 0;
printf("\n");
}
printf("%c",a);
num++;
}

}
return 0;
}
发表于 2015-10-24 17:21:27 回复(0)
// write your code here cpp

#include<stdio.h>

char base64[50004];

int main()
{
    while(~scanf("%s", base64)){
        char *h = base64, tmp;
        for(int i=0; ; ++i){
            if(h[i] == '\0'){
                printf("%s\n\n", h);
                break;
            }
            if(i == 76){
                tmp = h[76];
                h[76] = '\0';
                printf("%s\n", h);
                h += 76;
                h[0] = tmp;
                i = 0;
                continue;
            }
        }
    }
    return 0;
}

发表于 2015-09-16 14:48:01 回复(0)
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
cin>>str;
int len=str.length();
for(int i=1;i<=len;i++)
{
printf("%c",str[i-1]);
if(i%76==0)
printf("\n\n");
}
}
//这个为啥不通过呢!?
发表于 2015-08-26 09:10:38 回复(1)

问题信息

难度:
15条回答 11420浏览

热门推荐

通过挑战的用户

查看代码