字符串的输入输出处理
时间: 1ms 内存:64M
描述:
字符串的输入输出处理。
输入:
第一行是一个正整数N,最大为100。之后是多行字符串(行数大于N), 每一行字符串可能含有空格,字符数不超过1000。
输出:
先将输入中的前N行字符串(可能含有空格)原样输出,再将余下的字符串(不含有空格)以空格或回车分割依次按行输出。每行输出之间输出一个空行。
示例输入:
2
www.njupt.edu.cn NUPT
A C M
N U P Ter
示例输出:
www.njupt.edu.cn NUPT
A C M
N
U
P
Ter
提示:
参考答案(内存最优[748]):
#include <stdio.h>
int F(int i);
int main()
{
int a,b,i,temp=0,temp1,temp2,a1,a2;
while(scanf("%d %d",&a,&b)==2)
{
a1=a;
a2=b;
if(a>b)
{
temp2=a;
a=b;
b=temp2;
}
for(i=a;i<=b;i++)
{
temp1=F(i);
//printf("%d\n",temp1);
if(temp<temp1)
temp=temp1;
}
printf("%d %d %d\n",a1,a2,temp);
temp=0;
}
}
int F(int i)
{
int temp=i,j=1;
while(1)
{
if(temp==1)
return j;
if(temp%2==0)
{
temp=temp/2;
}
else
{
temp=temp*3+1;
}
j++;
}
}
参考答案(时间最优[0]):
#include <stdio.h>
int F(int i);
int main()
{
int a,b,i,temp=0,temp1,temp2,a1,a2;
while(scanf("%d %d",&a,&b)==2)
{
a1=a;
a2=b;
if(a>b)
{
temp2=a;
a=b;
b=temp2;
}
for(i=a;i<=b;i++)
{
temp1=F(i);
//printf("%d\n",temp1);
if(temp<temp1)
temp=temp1;
}
printf("%d %d %d\n",a1,a2,temp);
temp=0;
}
}
int F(int i)
{
int temp=i,j=1;
while(1)
{
if(temp==1)
return j;
if(temp%2==0)
{
temp=temp/2;
}
else
{
temp=temp*3+1;
}
j++;
}
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。