•连续输入字符串,请按长度为8拆分每个输入字符串并进行输出; •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
区块链毕设网qklbishe.com为您提供问题的解答
•连续输入字符串,请按长度为8拆分每个输入字符串并进行输出;
#include <iostream> #include <string> using namespace std; int main() { string s; while(cin >> s) { s = s + "0000000"; //特别强调这里是7个0 while(s.size() >= 8) { cout << s.substr(0,8) << endl; s = s.substr(8); } } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String s = new String(sc.nextLine()); if(s.length()%8 !=0 ) s = s + "00000000"; while(s.length()>=8){ System.out.println(s.substring(0, 8)); s = s.substring(8); } } } }
#include <iostream> using namespace std; int main(){ string str; while(getline(cin,str)){ while(str.size()>8){ cout << str.substr(0,8) <<endl; str=str.substr(8); } cout << str.append(8-str.size(),'0') << endl; //不够8位的补0 } }
#include <iostream> #include <string> using namespace std; void ***(string str) { if (str == "") return; if (str.size() <= 8) { str.append(8 - str.size(), '0'); cout << str << endl; return; } cout << str.substr(0, 8) << endl; ***(str.substr(8, str.size())); } int main() { string str1, str2; cin >> str1 >> str2; ***(str1); ***(str2); return 0; }
def printStr(string): if len(string) <= 8: print(string + "0" * (8 - len(string))) else: while len(string) > 8: print(string[:8]) string = string[8:] print(string + "0" * (8 - len(string))) a=input() b=input() printStr(a) printStr(b)
Python3解法。
a=raw_input() b=raw_input() def chstr(line): left = len(line)%8 if left != 0: line += "0" * (8 - left) for i in range(len(line) / 8): print line[i*8:(i+1)*8] chstr(a) chstr(b)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ String s=sc.nextLine(); StringBuffer sb=new StringBuffer(s); if(s.length()%8!=0){ int n=8-s.length()%8; for(int i=0;i<n;i++){ sb.append("0"); } } while(sb.length()>=8){ System.out.println(sb.substring(0, 8)); sb=sb.delete(0, 8); } } } }
#include <stdio.h> #include <string.h> char* copy(char* dst, char* src, int n){ for (int i = 0; i < n; ++i){ if (src[i]) dst[i] = src[i]; else dst[i] = '0'; } return dst; } int main(){ char buf[112] = {0}; char out[9] = {0}; while(gets(buf)){ char *p = buf; while(*p){ printf("%sn", copy(out, p, 8)); p += 8; } memset(buf, 0, 112); } }
#include<iostream> #include<string> #include<vector> using namespace std; int main() { string str1; string str2; while(cin >> str1) { cin >> str2; vector<string>temp; temp.push_back(str1); temp.push_back(str2); for(int i = 0;i <2;i++) { string t = temp[i]; int n1 = t.size()/8; int n2 = t.size()%8; for(int k=0;k<8-n2 && n2>0;k++) { t +='0'; } if(n2>0) n1++; for(int j=0;j<n1;j++) { cout<<t.substr(j*8,8)<<endl; } } } }
题目要一次输入两个,就两个cin,之后判断若是某个不是8的整数倍,就在字符串后面补齐‘0’,然后利用substr 分段输出就行。蛋疼。。华为这种题目
/* 本题思路为先补全字符串到8的整数倍,然后输出 我是用数组存储字符串的 */ #include<iostream> #include<string.h> using namespace std; void Output(char a[]) { int l = strlen(a); int m = l / 8; //整除数 int n = l % 8; //余数 int i,j; //在字符串尾部添加0 for (i = 0; i < 8 - n && n>0; i++) a[l + i] = '0'; if (n > 0) m++; for (i = 1; i <= m; i++) { for (j = 0; j < 8; j++) printf("%c", a[8 * (i - 1) + j]); printf("n"); } } int main() { char a[200], b[200]; //循环输入测试用例 while (cin >> a) { cin >> b; Output(a); Output(b); } }
//主要明白substr(起始位置,截取子字符窜个数)函数即可 #include <iostream> #include <string> #define MAX_SIZE 100 using namespace std; void Output(string str); int main() { string str1; string str2; cin >> str1 >> str2; if (str1.length() > MAX_SIZE||str1.length() < 1||str2.length() > MAX_SIZE||str2.length() < 1) { cout << "input error!" << endl; } else { Output(str1); Output(str2); } return 0; } void Output(string str) { int quotient = str.length()/8; int remainder = str.length()%8; int count = 0; for (int i=0;i<quotient;i++) { string substr = str.substr(count,8); cout << substr << endl; count += 8; } if (remainder != 0) { cout << str.substr(count,remainder); for (int i=remainder;i<8;i++) { cout << "0"; } cout << endl; } }
#include<bits/stdc++.h> using namespace std; int main(){ string temp; while(cin>>temp){ if(temp.size()%8){ temp.append(8-temp.size()%8,'0'); } for(int i=0;i<temp.size();i+=8){ cout<<temp.substr(i,8)<<endl; } } return 0; }
var lines = '' while (lines = readline()) { for(let i = 0;i < lines.length;i += 8) { var str = lines.slice(i, i+8).padEnd(8, 0) console.log(str) } }
js解法挺少的,这里写一下,主要用了str的slice()和padEnd(),slice()传入开始(包含)和结束(不包含)的位置,padEnd()用于指定长度和长度未满时用什么补齐
#include <stdio.h> #include <string.h> const int N = 1000; int main() { char str[N]; int len, need; while (gets(str)) { len = strlen(str); for (int i = 0; i < len; i++) { if ((i + 1) % 8 == 0) { printf("%cn", str[i]); } else { printf("%c", str[i]); } } if (len % 8 != 0) { need = 8 - len % 8; for (int i = 0; i < need; i++) { printf("0"); } printf("n"); } } return 0; }
}通过,no bug!
import java.util.Scanner; public class Main{ public static void main(String[]args){ Scanner in = new Scanner(System.in); while(in.hasNextLine()){ String str1 = in.nextLine(); String str2 = in.nextLine(); SpliteString(str1); System.out.println(); SpliteString(str2); } } static void SpliteString(String str){ if(str.length() <= 8){ for(int i = 0;i < 8;i++) if(i < str.length()) System.out.print(str.charAt(i)); else System.out.print("0"); }else{ for(int i = 0;i < 8;i++){ System.out.print(str.charAt(i)); } System.out.println(); SpliteString(str.substring(8));//递归,再判断除去前8个字符之后的长度是否大于8 } } }
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String s = new String(sc.nextLine());
if(s.length()%8 !=0 )
s = s + "00000000";
while(s.length()>=8){
System.out.println(s.substring(0, 8));
s = s.substring(8);
}
}
}
}
#include <iostream> #include <string> using namespace std; void printStr(const string& str) { int n = str.size(); if (n <= 8) { string padding(8 - n, '0'); cout << str << padding << endl; } else { string str1 = str.substr(0, 8); string str2 = str.substr(8, n - 8); printStr(str1); printStr(str2); } } int main() { string str; while (cin >> str) { printStr(str); } return 0; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String strs = scanner.nextLine() + "00000000"; for (int i = 8; i < strs.length(); i += 8) { System.out.println(strs.substring(i - 8, i)); } } } }
/* Convert String in String array with element of 8 char length */ #include <stdio.h> int main() { char input; int i = 0; while ((input = getchar()) != EOF) { if (input != 'n') { putchar(input); i++; } else if (i != 0) { while (i < 8) { putchar('0'); i++; } } /* New line for Next Element in Array */ if (i == 8) { putchar('n'); i = 0; } } return 0; }
以上就是关于问题•连续输入字符串,请按长度为8拆分每个输入字符串并进行输出; •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。的答案
欢迎关注区块链毕设网-
专业区块链毕业设计成品源码,定制。
区块链NFT链游项目方科学家脚本开发培训