本专栏分享Java解法(主),c/c++,python解法一同记录。
目录
11. MT1011 字符和整数
(1)题目描述
输出’X’’、65的字符、十进制数据形式。
格式
输入格式: 无
输出格式: 输出字符、十进制整数,空格分隔
样例1
输入格式: 无
输出格式:
X 88
A 65
(2)参考代码
Java
import java.util.Scanner;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// code here
System.out.printf("%c %d\n", 'X', (int)'X');
System.out.printf("%c %d", (char)65, 65);
input.close();
}
}
c++
#include<bits/stdc++.h>
using namespace std;
int main( )
{
printf("X %d\nA %d",(int)'X',(int)'A');
return 0;
}
12. MT1012 各种类型长
(1)题目描述
请编写一个简单程序,输出int、float、double和char的大小。
格式
输入格式: 无
输出格式: 输出分4行,分别输出int、float、double和char的大小
样例1
输入格式: 无
输出格式:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
(2)参考代码
Java
import java.util.Scanner;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// code here
int A= Integer.SIZE;//16bit
int B =Float.SIZE;//16bit
int C =Double.SIZE;//64bit
int D =Character.SIZE;//8bit
A= A/8;B=B/8;C=C/8;D=D/16;
System.out.printf("Size of int: %d bytes \n",A);
System.out.printf("Size of float: %d bytes \n",B);
System.out.printf("Size of double: %d bytes \n",C);
System.out.printf("Size of char: %d byte ",D);
input.close();
}
}
c++
#include<bits/stdc++.h>
using namespace std;
int main( )
{
printf("Size of int: %d bytes\n",sizeof(int));
printf("Size of float: %d bytes\n",sizeof(float));
printf("Size of double: %d bytes\n",sizeof(double));
printf("Size of char: %d byte\n",sizeof(char));
return 0;
}
13. MT1013 关键字long
(1)题目描述
请编写一个简单程序,输出int、long int、long long int、double和longdouble的大小。
格式
输入格式: 无
输出格式: 输出分5行,分别输出int、long int、 long long int、double和long double的大小
样例1
输入格式: 无
输出格式:
Size of int = 4 bytes
Size of long int = 8 bytes
Size of long long int = 8 bytesSize of double = 8 bytes
Size of long double = 16 bytes
(2)参考代码
Java
import java.util.Scanner;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// code here
System.out.println("Size of int = "+Integer.SIZE/8+" bytes");
System.out.println("Size of long int = "+Long.SIZE/8+" bytes");
System.out.println("Size of long long int = "+Long.SIZE/8+" bytes");
System.out.println("Size of double = "+Double.SIZE/8+" bytes");
System.out.println("Size of long double = "+Double.SIZE*2/8+" bytes");
input.close();
}
}
c++
#include<bits/stdc++.h>
using namespace std;
int main( )
{
printf("Size of int = %d bytes\n",sizeof(int));
printf("Size of long int = %d bytes\n",sizeof(long int));
printf("Size of long long int = %d bytes\n",sizeof(long long int));
printf("Size of double = %d bytes\n",sizeof(double));
printf("Size of long double = %d bytes\n",sizeof(long double));
return 0;
}
15. MT1015 输入分隔符
(1)题目描述
输入“a=22,b=b,c=14,d=d”给变量a、b、c、d,然后再输出他们。
格式
输入格式: a=22,b=b,c=14,d=d
输出格式: 空格分隔
样例1
输入格式: a=22,b=b,c=14,d=d
输出格式: 22 b 14 d
(2)参考代码
Java
import java.util.Scanner;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// code here
String[] src = input.next().split(",");
for (int i=0;i<src.length;i++){
String[] z =src[i].split("=");
System.out.print(z[1]+" ");
}
input.close();
}
}
c++
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a,c;
char b,d;
scanf("a=%d,b=%c,c=%d,d=%c",&a,&b,&c,&d);
printf("%d %c %d %c",a,b,c,d);
return 0;
}
16. MT1016 宽度与对齐
(1)题目描述
输出455、-123、987654,宽度为5,分别左对齐和右对齐
格式
输入格式: 无
输出格式: 输出为整型,空格分隔。每个数的输出占一行
样例1
输入格式: 无
输出格式:
455 455
-123 -123
987654 987654
(2)参考代码
Java
import java.util.Scanner;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// code here
int a=455;
int b=-123;
int c=987654;
System.out.printf("%-5d %5d\n",a,a);
System.out.printf("%-5d %5d\n",b,b);
System.out.printf("%-5d %5d\n",c,c);
input.close();
}
}
c++
#include<bits/stdc++.h>
#include<iomanip>
using namespace std;
int main( )
{
cout<<setw(5)<<left<<455<<" "<<setw(5)<<right<<455<<endl;
cout<<setw(5)<<left<<-123<<" "<<setw(5)<<right<<-123<<endl;
cout<<setw(5)<<left<<987654<<" "<<setw(5)<<right<<987654<<endl;
return 0;
}
17. MT1017 左右对齐
(1)题目描述
输出3.1415926、22.3456,宽度为14,精度为6,分别左对齐和右对齐。
格式
输入格式: 无
输出格式: 输出为实型,空格分隔。每个数的输出占一行。
样例1
输入格式: 无
输出格式:
3.141593 3.141593
22.345600 22.345600
(2)参考代码
Java
import java.util.Scanner;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// code here
double a=3.1415926;
double b=22.3456;
System.out.printf("%-14.6f %14.6f\n",a,a);
System.out.printf("%-14.6f %14.6f",b,b);
input.close();
}
}
c++
#include<bits/stdc++.h>
using namespace std;
int main( )
{
printf("%-14.6f %14.6f\n",3.141593,3.141593);
printf("%-14.6f %14.6f\n",22.345600,22.345600);
return 0;
}
18. MT1018 输入宽度
(1)题目描述
输入123456789给变量a1、a2、a3,宽度为3,然后输出a1、a2、a3,空格分隔。
格式
输入格式: 123456789
输出格式: 输出为整型,空格分隔。
样例1
输入格式: 123456789
输出格式: 123 456 789
(2)参考代码
Java
import java.util.Scanner;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// code here
int sum=123456789;
int a1=sum/1000000;
int a2=(sum%1000000)/1000;
int a3=sum%1000;
System.out.printf("%d %d %d",a1,a2,a3);
input.close();
}
}
c++
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int a1,a2,a3;
scanf("%3d%3d%3d",&a1,&a2,&a3);
printf("%3d %3d %3d",a1,a2,a3);
return 0;
}
20. MT1020 %s格式符
(1)题目描述
输入字符串,然后输出前3个字符,要求占6列,右对齐。
格式
输入格式: 输入字符串
输出格式: 输出字符串
样例1
输入格式: Wendy
输出格式: Wen
(2)参考代码
Java
import java.util.Scanner;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// code here
String s = input.nextLine().substring(0, 3);
System.out.printf("%6s", s);
input.close();
}
}
c++
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char str[20];
cin.getline(str,4);
cout<<setw(6)<<right<<str<<endl;
return 0;
}