十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
你现在不能休息,周围有 deadline 在游荡。
莲子正在赶自己的程序设计作业。除了完成程序代码的编写,对提交上去的作业进行排版以对助教留下良好印象同样重要。
而众所周知,文章里面的代码和一些特殊性质的文本是要附上行号的,然而它们的篇幅往往都很长,手动去加容易出现失误。因此,莲子决定自力更生造轮子,写一个行号生成器。
题目描述莲子希望实现这样一个功能:输入一份文本文件,并给该文件加上行号。
以下是本题中三个基本概念的定义:
以下是本题中添加行号的方法:
以下是一个例子:
#include
为了便于读者观察,这里将所有的空格换成用来表示空格的
␣
\verb!␣!
␣ 字符。
#include
输入包含若干行,为原始的文本文件。
输出格式输出包含若干行,为加上行号后的文本文件。
样例 #1 样例输入 #1#includeusing namespace std;
int main(){
int a, b;
cin >>a >>b;
cout<< a + b<< endl;
cout<< a - b<< endl;
cout<< a * b<< endl;
cout<< a / b<< endl;
return 0;
}
样例输出 #11 #include2 using namespace std;
3 int main(){
4 int a, b;
5 cin >>a >>b;
6 cout<< a + b<< endl;
7 cout<< a - b<< endl;
8 cout<< a * b<< endl;
9 cout<< a / b<< endl;
10 return 0;
11 }
样例 #2
样例输入 #2public class Main {
public static void main(String[] args) throws Exception {
int b = 0, c = 0;
for (int a = 123; a< 333; a++) {
int[] array = new int[10];
Boolean flag = true;
b = a * 2;
c = a * 3;
array[a / 100] = 1;
array[a / 10 % 10] = 1;
array[a % 10] = 1;
array[b / 100] = 1;
array[b / 10 % 10] = 1;
array[b % 10] = 1;
array[c / 100] = 1;
array[c / 10 % 10] = 1;
array[c % 10] = 1;
for (int i = 1; i< array.length; i++) {
if (array[i] != 1) {
flag = false;
break;
}
}
if (flag)
System.out.println(a + " " + b + " " + c);
}
}
}
样例输出 #21 public class Main {
2 public static void main(String[] args) throws Exception {
3 int b = 0, c = 0;
4 for (int a = 123; a< 333; a++) {
5 int[] array = new int[10];
6 Boolean flag = true;
7 b = a * 2;
8 c = a * 3;
9 array[a / 100] = 1;
10 array[a / 10 % 10] = 1;
11 array[a % 10] = 1;
12
13 array[b / 100] = 1;
14 array[b / 10 % 10] = 1;
15 array[b % 10] = 1;
16
17 array[c / 100] = 1;
18 array[c / 10 % 10] = 1;
19 array[c % 10] = 1;
20 for (int i = 1; i< array.length; i++) {
21 if (array[i] != 1) {
22 flag = false;
23 break;
24 }
25 }
26 if (flag)
27 System.out.println(a + " " + b + " " + c);
28 }
29 }
30 }
样例 #3
样例输入 #3Tao hua kai yo li hua kai, jie jie mei mei dong qi lai;
Tao hua kai yo li hua kai, jie jie mei mei tiao qi lai.
Gei ni zi ji, chun yi mai huai (yi ya yo);
Gei xin shang ren, chun se man kai.
Tao hua kai yo li hua kai, jie jie mei mei dong qi lai;
Tao hua kai yo li hua kai, jie jie mei mei tiao qi lai.
Gei tong bao men, chun yi man huai, hai hai yo;
Gei zhe da di, chun se quan kai!!!
Cong na nu jiang nan, dao mo he bei,
Dao chu dou you hua rong ming mei.
Lai hui hui shou, zai diao ge tou,
Wu xian chun guang zai wo xiong.
Cong jing cheng, dao yuan ye,
Dao chu dou you sheng de xi yue.
Lai hui hui shou, zai yan ran huang ge tou,
Wu xian CHUN GUANG XIAN ZU GUO!!!
Tao hua kai yo li hua kai, Jiong xian mei mei dong qi lai;
Tao hua kai yo li hua kai, Jie jie mei mei tiao qi lai.
Gei ni zi ji, chun yi mai huai chun yi man huai yi ya yo;
Gei xin shang ren, chun se man kai chun se man kai.
Tao hua kai yo li hua kai, Jie jie mei mei dong qi lai;
Tao hua kai yo li hua kai, Jie jie mei mei tiao qi lai.
Gei tong bao men, chun yi man huai, hai hai yo;
Gei zhe da di, chun se quan kai!!!
Cong na nu jiang nan, dao mo he bei,
Dao chu dou you hua rong ming mei.
Lai hui hui shou, zai diao ge tou,
Wu xian chun guang zai wo xiong.
Cong jing cheng, dao yuan ye,
Dao chu dou you sheng de xi yue.
Lai hui hui shou, zai yan ran huang ge tou,
Wu xian CHUN GUANG XIAN ZU GUO!!!
Lai hui hui shou, zai yan ran huang ge tou,
Wu xian CHUN GUANG XIAN ZU GUO!!!
样例输出 #31 Tao hua kai yo li hua kai, jie jie mei mei dong qi lai;
2 Tao hua kai yo li hua kai, jie jie mei mei tiao qi lai.
3 Gei ni zi ji, chun yi mai huai (yi ya yo);
4 Gei xin shang ren, chun se man kai.
5 Tao hua kai yo li hua kai, jie jie mei mei dong qi lai;
6 Tao hua kai yo li hua kai, jie jie mei mei tiao qi lai.
7 Gei tong bao men, chun yi man huai, hai hai yo;
8 Gei zhe da di, chun se quan kai!!!
9 Cong na nu jiang nan, dao mo he bei,
10 Dao chu dou you hua rong ming mei.
11 Lai hui hui shou, zai diao ge tou,
12 Wu xian chun guang zai wo xiong.
13 Cong jing cheng, dao yuan ye,
14 Dao chu dou you sheng de xi yue.
15 Lai hui hui shou, zai yan ran huang ge tou,
16 Wu xian CHUN GUANG XIAN ZU GUO!!!
17 Tao hua kai yo li hua kai, Jiong xian mei mei dong qi lai;
18 Tao hua kai yo li hua kai, Jie jie mei mei tiao qi lai.
19 Gei ni zi ji, chun yi mai huai chun yi man huai yi ya yo;
20 Gei xin shang ren, chun se man kai chun se man kai.
21 Tao hua kai yo li hua kai, Jie jie mei mei dong qi lai;
22 Tao hua kai yo li hua kai, Jie jie mei mei tiao qi lai.
23 Gei tong bao men, chun yi man huai, hai hai yo;
24 Gei zhe da di, chun se quan kai!!!
25 Cong na nu jiang nan, dao mo he bei,
26 Dao chu dou you hua rong ming mei.
27 Lai hui hui shou, zai diao ge tou,
28 Wu xian chun guang zai wo xiong.
29 Cong jing cheng, dao yuan ye,
30 Dao chu dou you sheng de xi yue.
31 Lai hui hui shou, zai yan ran huang ge tou,
32 Wu xian CHUN GUANG XIAN ZU GUO!!!
33 Lai hui hui shou, zai yan ran huang ge tou,
34 Wu xian CHUN GUANG XIAN ZU GUO!!!
提示
数据范围及约定对于全部数据,保证输入的文本文件的字符总数(包括换行符在内),不超过 2 × 1 0 4 2\times 10^4 2×104。同时,保证输入数据合法。
评测时,会忽略选手输出文件的行末空格与文末换行。因此选手可以自行决定行尾是否要输出多余空格。请使用 Java 或 Python 语言作答的选手注意输入输出时的效率。
题解读入题。暴风吸入输入数据里给定的所有字符,存到数组里,统计有多少个换行符,确定输入文件的总行数m。由此计算出最后一个行号的长度 s=⌊lgm+1⌋(数学库里可以直接调用 lg,当然你也可以随便用什么途径算出每个数的长度)。
然后就是模拟了。对于第 i*i 行,
时间复杂度为O(∣S∣),其中∣S∣ 是输入的所有字符的个数。
参考代码#include#define up(l, r, i) for(int i = l, END##i = r;i<= END##i;++ i)
#define dn(r, l, i) for(int i = r, END##i = l;i >= END##i;-- i)
using namespace std;
typedef long long i64;
const int INF = 2147483647;
const int MAXN= 2e4 + 3;
char S[MAXN], c; int l, m;
int main(){
m = count(S + 1 , S + 1 + fread(S + 1, 1, MAXN, stdin), '\n');
int s = log10(m) + 1 + 1e-9, p = 0;
up(1, m, i){
int t = log10(i) + 1 + 1e-9;
for(int j = 1;j<= s - t;++ j) putchar( ' '); printf("%d ", i);
for(p = p + 1;S[p] != 10;++ p) putchar(S[p]); putchar('\n');
}
return 0;
}
#include#include#includeusing namespace std;
char buf[200050];
vectors[200050];
int cnt;
int get_digit(int x)
{
int digit=1,ret=1;
while (ret<=x)
{
digit++;
ret*=10;
}
return digit;
}
int main()
{
while(fgets(buf,200000,stdin)!=NULL)
{
cnt++;
for (int i=0;buf[i]!='\n';i++)
s[cnt].push_back(buf[i]);
}
int cnt_digit=get_digit(cnt);
for (int i=1;i<=cnt;i++)
{
for (int j=1;j<=cnt_digit-get_digit(i);j++)
putchar(' ');
cout<< i<< ' ';
for (int j=0;j
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Main {public static Listlist = new ArrayList<>();
public static int getBit(int x) {int cnt = 0;
while (x >0) {x /= 10;
++cnt;
}
return cnt;
}
public static void main(String[] args) {Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {list.add(scanner.nextLine());
}
int size = list.size();
int len = getBit(size);
for (int i = 0; i< size; ++i) {System.out.printf("%" + len + "d %s\n", i + 1, list.get(i));
}
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧