这是第一个题目,没找出来错误
供参考:
//第一题
#include <stdio.h>
int main()
{
int x = 1, i, j, k; //修改
while (x) //修改
{
printf(" 1--输出图形1\n"); //修改
printf(" 2--输出图形2\n"); //修改
printf(" 0--退出\n"); //修改
scanf("%d", &x);
switch (x) {
case 1:
for (i = 5; i > 0; i--) //修改
{
for (k = 1; k <= 5 - i; k++)
printf(" ");
for (j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
break; //修改
case 2:
for (i = 1; i < 5; i++) //修改
{
for (k = 1; k < 5 - i; k++)//修改
printf(" ");
for (j = 1; j <= 2 * i - 1; j++)
printf("%c", i + 'A' - 1);
printf("\n");
}
break; //修改
case 0:printf("退出"); //修改
break;
default: break;
}
}
return 0;
}
//第二题
#include <stdio.h>
int fun(int n) //素数判断
{
int i;
if (n < 4)
return n > 1;
for (i = 2; i * i <= n; i++)
if (n % i == 0) return 0;
return 1;
}
int main()
{
int k, x, i, j = 0;
printf("请输入x的值:");
scanf("%d", &x);
printf("请输入k的值:");
scanf("%d", &k);
for (i = x + 1, j = 0; j != k; i++)
{
if (fun(i)) {
j++;
printf("%5d", i);
if (j % 5 == 0 && j != k)
printf("\n");
}
}
return 0;
}
//第二题用 while()循环的写法
#include <stdio.h>
int fun(int n) //素数判断
{
int i;
if (n < 4)
return n > 1;
for (i = 2; i * i <= n; i++)
if (n % i == 0) return 0;
return 1;
}
int main()
{
int k, x, j = 0;
printf("请输入x的值:");
scanf("%d", &x);
printf("请输入k的值:");
scanf("%d", &k);
while(x++)
{
if (fun(x)) {
j++;
printf("%5d", x);
if (j % 5 == 0 && j != k)
printf("\n");
if (j == k) break;
}
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
#include <mmsystem.h>//为了加输入 成功音效
#pragma comment(lib,"winmm.lib")//为了加输入 成功音效 但还是没有成功
//数据设计
//窗口属性
const int width = 640;
const int height =500;
//游戏正确率和错误率
int right = 0;
int error = 0;
//下坠文字的结构体
struct target
{ //每一个字符串的X,Y坐标
int x;
int y;
char *str; //保存字符串
};
//用户输入的值
struct USRKEY
{
int x;
int y;
char str[20];
}userkey = {320,500-40,""};
//在指定位置输入整数
void outtextxy_int(int x, int y, char *format, int num)
{
char str[20] = "";
sprintf(str, format, num);
outtextxy(x, y, str);
}
//在指定位置输入浮点数
void outtextxy_double(int x, int y, char *format, double num)
{
char str[20] = "";
sprintf(str, format, num);
outtextxy(x, y, str);
}
//画界面分割线
void divwindow()
{
line(width - 100, 0, width - 100, height - 50);
line(0, height - 50, width + 50, height - 50);
line(width - 100, 130, width + 50, 130);
}
void inittarget(struct target words[], int n)
{
static char str[29][10] = { "main","struct","int","static","char","switch","string","include",
"true","false","conin","graphics","stdlib","time","const","width","height","right","error",
"void","public","private","protect","sizeof","continue","short","else","union","default" };
//0--28
//随机产生
words[n].str = str[rand() % 29];
//0 1 2
//判断重复 如果重复 重新生成
// 与或非 && || !
while (words[n].str == words[(n + 1) % 3].str || words[n].str == words[(n + 2) % 3].str)
{
words[n].str = str[rand() % 29];
}
words[n].x = rand() % (width - 150);
words[n].y = -20;
};
//信息输出显示
void drawScore()
{
settextcolor(LIGHTBLUE);
settextstyle(15, 0, "mingliub");
//文本信息输出
outtextxy(width - 90, 25, "我的");
outtextxy(width - 90, 25 + 25, "程序员打字游戏");
outtextxy(width - 90, 50 + 25, "c++第一个程序");
//游戏状态栏输出
outtextxy(width - 90, 225, "正确数:");
outtextxy_int(width - 90, 225 + 25, " % d ",right );
outtextxy(width - 90, 285, "错误数:");
outtextxy_int(width - 90, 285 + 25, " % d ",error);
outtextxy(width - 90, 285+285-225, "正确率");
//分类讨论
if (right + error == 0)
{
outtextxy_double(width - 90, 285 + 285 - 225 + 25, "%.2lf%%",0.0);
}
else
{
double sum = right + error;
outtextxy_double(width - 90, 285 + 285 - 225 + 25, "%.2lf%%", right / sum * 100);
}
}
//主程序
int main()
{
srand((unsigned int)time(NULL));
initgraph(width + 50, height - 20);
struct target words[3];
//产生随机掉落字符串
for (int n = 0; n < 3; n++)
{
inittarget(words, n);
words[n].y = -15-n*30; //形成不等高
}
int i = 0;
BeginBatchDraw(); //实现双缓冲 防止屏闪
while (1)
{
mciSendString("open yx.mp3 alias music", NULL, 0, NULL);
cleardevice();
divwindow();
//碰线处理
for (int n = 0; n<3;n++)
{
words[n].y += 2;
if (words[n].y > (height - 50 - textheight(words[n].str)))
{
inittarget(words, n);
}
}
//打印文字
for (int n = 0; n < 3; n++)
{
settextcolor(RED);
outtextxy(words[n].x, words[n].y, words[n].str);
}
if (_kbhit()) //检测键盘 如果有按键则返回非零 为真
{
char usertarget;//接受用户的值
if ((usertarget = _getch()) != ' ')
{
userkey.str[i++] = usertarget;
}
else
{
int flagError=0;
//干掉正确的字符
for (i=0;i<3;i++)
{
if (strcmp(userkey.str, words[i].str) == 0)
{
inittarget(words, i);
right++;
flagError = 1;
//下面这句 可以不加 我就是想实现成功的音效 但不能重复实现
//只响了一声 之后就不知道怎么重复让音效响了
mciSendString("play music ", NULL, 0, NULL);
}
mciSendString("stop music", NULL, 0, NULL);
}
if (flagError == 0)
{
error++;
}
i = 0;
userkey.x = 320;
memset(userkey.str, 0, 20);
}
}
outtextxy(userkey.x, userkey.y, userkey.str);
drawScore();
FlushBatchDraw();//实现双缓冲 防止屏闪
Sleep(60);
}
getchar();
closegraph();
return 0;
}
这是我代码的具体展示图片
对于第一个问题,代码中第11行应该改为scanf("%d",&b);
,因为scanf()
函数需要传入参数的地址来将输入的值存储到变量中。
对于第二个问题,可以使用循环嵌套来实现输出杨辉三角的效果,具体代码如下:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "请输入要打印的行数:";
cin >> n;
int a[n][n];
for(int i=0; i<n; i++) { // 初始化数组
for(int j=0; j<n; j++) {
a[i][j] = 0;
}
}
for(int i=0; i<n; i++) { // 计算杨辉三角
for(int j=0; j<=i; j++) {
if(j==0 || j==i) {
a[i][j] = 1;
} else {
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
}
for(int i=0; i<n; i++) { // 输出杨辉三角
for(int j=0; j<=i; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
对于第三个问题,可以将数组中的元素进行排序,然后遍历数组,找到第一个出现次数超过数组长度一半的元素即可。具体代码如下:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cout << "请输入数组长度:";
cin >> n;
int a[n];
cout << "请输入数组元素:";
for(int i=0; i<n; i++) {
cin >> a[i];
}
sort(a, a+n); // 对数组进行排序
int count = 0;
int num = a[0];
for(int i=0; i<n; i++) { // 找出出现次数超过数组长度一半的元素
if(a[i] == num) {
count++;
} else {
num = a[i];
count = 1;
}
if(count > n/2) {
cout << "出现次数超过数组长度一半的元素为:" << num << endl;
return 0;
}
}
cout << "数组中不存在出现次数超过数组长度一半的元素。" << endl;
return 0;
}