PAT基础编程题目-7-20 打印九九口诀表
PAT基础编程题目-7-20 打印九九口诀表
题目详情
解答
C语言版
#include<stdio.h>
int main() {
int N;
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
printf("%d*%d=%-4d", j, i, i * j);
}
if (i<N)
printf("\n");
}
return 0;
}
C++版
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
int N;
cin >> N;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
cout << j << "*" << i << "=" << setw(4) << setiosflags(ios::left) << i * j;
}
if (i < N)
cout << endl;
}
return 0;
}
Java版
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int N = 0;
Scanner scanner = new Scanner(System.in);
if (scanner.hasNext()) {
N = scanner.nextInt();
}
scanner.close();
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+String.format("%1$-4d", i*j));
}
if (i < N)
System.out.println();
}
}
}
创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!