要求:使用者输入行数然后print出相应结果
java版本的
如果问题解决,麻烦点下我回答右边的采纳,谢谢。
// Q701108.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int table[] = {1,5,8,9,10,11,10,8,5,1,-3,-6,-8,-9,-8,-6,-3};
int N = 17;
void out(int n)
{
if (n > 0)
{
for (int j = 0; j < 10; j++) printf(" ");
for (int j = 0; j < n; j++) printf("*");
printf("\n");
}
else
{
for (int j = 0; j < 9 + n; j++) printf(" ");
for (int j = 0; j < 2 - n; j++) printf("*");
printf("\n");
}
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("input the # of lines:");
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
out(table[i % N]);
}
return 0;
}
#include <iostream>
#include <math.h>
using namespace std;
int main(){
float pi = 3.1415926;
int wave = 10;
int lines = 17;
for( int i = 0; i < lines; i++ ){
int bound = round( sin( pi / 8 * i ) * wave );
for( int i = -wave; i <= wave; i++ ){
cout << ( ( bound > 0 ) ?
( ( 0 <= i && i <= bound ) ? "*" : " "):
( ( bound <= i && i <= 0 ) ? "*" : " ")
);
}
cout << endl;
}
}
修改了一下,用了一个判断区间的小技巧,楼主可以思考一下
#include <iostream>
#include <math.h>
using namespace std;
int main(){
float pi = 3.1415926;
int wave = 10;
int lines;
cout << "Input the lines:"; cin >> lines;
for( int i = 0; i < lines; i++ ){
int bound = round( sin( pi / 8 * i ) * wave );
for( int j = -wave; j <= wave; j++ )
cout << ( (0-j)*(j-bound) >= 0 ? "*" : " " );
cout << endl;
}
}
Java版本
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double pi = 3.1415926;
int wave = 10;
int lines;
System.out.println("Input the lines:");
lines = scan.nextInt();
for( int i = 0; i < lines; i++ ){
int bound = (int)Math.round( Math.sin( pi / 8 * i ) * wave );
for( int j = -wave; j <= wave; j++ )
System.out.print( ( (0-j)*(j-bound) >= 0 ? "*" : " " ) );
System.out.println();
}
}