题目一:
许多简单的交互式程序都是基于菜单的:它们向用户显示可供选择的命令列表;一旦用户选择了某条命令,程序就执行相应的操作,然后提示用户输入下一条命令;这个过程一直会持续到用户选择 "退出" 或 "停止" 命令。
这类程序的核心显然是循环。循环内将有语句提示用户输入命令,读命令,然后确定执行的操作
for (; ;) {
提示用户输入命令;
读入命令;
执行命令;
}
执行命令将用到 switch 语句 (或者级联式 if 语句):
for (; ;) {
提示用户输入命令;
读入命令;
switch (命令) {
case 命令1: 执行操作; break;
case 命令2: 执行操作; break;
...
case 命令n: 执行操作; break;
default: 显示错误信息; break;
}
}
现在请开发一个记账程序。程序将为用户提供选择菜单:清空账户余额,往账户上存钱,从账户上取钱,显示当前余额,退出程序。选择项分别用整数 0、1、2、3 和 4 表示。程序的会话类似这样:
*** checkbook-balancing program ***
Commands: 0=clear, 1=credit, 2=debit, 3=balance, 4=exit
Enter command: 1
Enter amount of credit: 1000.0
Enter command: 2
Enter amount of debit: 100.0
Enter command: 3
Current balance: $900.00
Enter command: 9
Commands: 0=clear, 1=credit, 2=debit, 3=balance, 4=exit
Enter command: 0
Enter command: 3
Current balance: $0.00
Enter command: 4
代码一/解答:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
int command=0; float credit = 0.0f; float add_or_del;
printf("*** checkbook-balancing program ***\nCommands: 0 = clear, 1 = credit, 2 = debit, 3 = balance, 4 = exit\n\n");
for (;command!=4;) {
printf("Enter command: ");
scanf("%d", &command);
switch (command)
{
case 0:
{
credit =0.0;
break;
}
case 1:
{
printf("Enter amount of credit: ");
scanf("%f", &add_or_del);
credit += add_or_del;
break;
}
case 2:
{
printf("Enter amount of debit: ");
scanf("%f", &add_or_del);
credit -= add_or_del;
break;
}
case 3:
{
printf("Current balance: $%.2f\n",credit);
break;
}
default:
{
printf("Commands: 0 = clear, 1 = credit, 2 = debit, 3 = balance, 4 = exit\n\n");
break;
}
case 4:break;
}
}
return 0;
}
答案:
发现一个蛮好玩的事情,1.2的时候,不知道怎么回事
exit(0);是老师退出循环的语句,for循环里面就没有放任何表达式
其他就是,我的长的丑哭死
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main(void) { printf("*** checkbook-balancing program ***\n"); printf("Commands: 0=clear, 1=credit, 2=debit, 3=balance, 4=exit\n\n"); double balance = 0.0; double amount = 0.0; for (;;) { int cmd; printf("Enter command: "); scanf("%d", &cmd); switch (cmd) { case 0: balance = 0; break; case 1: printf("Enter amount of credit: "); scanf("%lf", &amount); balance += amount; break; case 2: printf("Enter amount of debit: "); scanf("%lf", &amount); balance -= amount; break; case 3: printf("Current balance: $%.2lf\n", balance); break; case 4: exit(0); default: printf("Commands: 0=clear, 1=credit, 2=debit, 3=balance, 4=exit\n\n"); break; } } return 0; }
题目二:
(1) 给你一个 非空整数数组 nums,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
输入:nums = [1,4,2,1,2]
输出:4
(2) 给你一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。你可以按任意顺序返回答案.
输入:nums = [1,2,1,3,2,5]
输出:[3, 5]
解释:[5, 3] 也是有效的答案
代码二/解答:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define SIZE(a) (sizeof(a)/sizeof(a[0]))
int main(void) {
//ONE SINGLE_DOG
int nums[] = { 1,4,2,1,2 };
int single_dog = 0; int lenght = 0;
for (lenght = 0; lenght < SIZE(nums); lenght++) {
single_dog = single_dog ^ nums[lenght];
}
printf("nums=[1,4,2,1,2]\nsingle_dog is %d\n", single_dog);
//two single_dog
int nums2[] = {1, 2, 1, 7, 2, 5};
single_dog = 0; lenght = 0;
for (lenght = 0; lenght < SIZE(nums2); lenght++) {
single_dog = single_dog ^ nums2[lenght];
}
//printf("%d\n", single_dog);
//找出single_dog最低有效位
int lsb = 1; int count=0;
while ((single_dog & lsb) == 0) {
lsb <<=1; //single_dog>>=1;
count++;
}
//printf("%d\n", count);
//分组 -LSB的最低有效位
int a=0; int b=0; lenght = 0;
for (lenght = 0; lenght < SIZE(nums2); lenght++) {
if (nums2[lenght] & lsb) {
a ^= nums2[lenght]; //在LSB位上是1 多次异或,除了SINGLE_DOG,其他的都会异或两次!!!妈的他不说
}
else {
b^= nums2[lenght]; //在LSB位上是0
}
//nums2[length] & lsb 检查数组中的每个元素在 lsb 位上是1还是0。如果是1,则将其与 a 异或。如果是0,则将其与 b 异或。
}
printf("nums = [1,2,1,7,2,5]\ntwo single_dog is %d and %d\n", a,b);
return 0;
}
答案:
(1)
#include <stdio.h> int main(void) { int nums[] = { 1,2,1,3,2}; int xor = 0; for (int i = 0; i < 5; i++) { xor ^= nums[i]; } printf("xor = %d\n", xor); return 0; }
(2)
老师的找出最低有效位:
int lsb = xor &(-xor);
解释:
-xor
的作用:
- 负号操作符
-
对xor
进行取反操作(按位取反),并且将结果加 1,这是因为-xor
在计算机中表示为补码形式,即~xor + 1
。这个操作的结果是一个整数,其二进制表示仍然是原整数xor
的最低有效位(LSB)的位置。- 这个结果确保了
-xor
保留了xor
中最低有效位的1,并且将其右边的所有位都置为0,而它左边的所有位都变成了原xor
的补码形式(即原来的0变成1,1变成0,但不影响最低有效位)。按位与操作
&
:
xor & (-xor)
执行按位与操作。在按位与操作中,只有当两个操作数的对应位都为 1 时,结果的对应位才为 1,否则为 0。结果
lsb
:
- 结果
lsb
是一个整数,其二进制表示只有xor
中的最低有效位为 1,其余位都为 0。这是因为-xor
操作使得它只保留了xor
中最低有效位的信息,其他位上的信息都被置为了 0。示例:
假设
xor
的值为0b101010
,即十进制的 42。
xor = 0b101010
(42)-xor = ~xor + 1 = 0b010110 + 1 = 0b010111
(-42 的补码表示)xor & (-xor) = 0b101010 & 0b010110 = 0b000010
(结果为 2)在这个示例中,
lsb
的值为 2,对应于二进制数0b000010
,它是xor
中最低有效位的位置。我的:
int lsb = 1; int count=0;
while ((single_dog & lsb) == 0) {
lsb <<=1; //single_dog>>=1;
count++;
}#include <stdio.h> int main(void) { int nums[] = { 1,2,1,3,2,5 }; int xor = 0; for (int i = 0; i < 6; i++) { xor ^= nums[i]; } // xor = a ^ b; (xor肯定不等于0, 至少有一位为1, a和b在那一位上的值不一样) int lsb = xor &(-xor); int a = 0, b = 0; for (int i = 0; i < 6; i++) { if (nums[i] & lsb) { a ^= nums[i]; } else { b ^= nums[i]; } } printf("a = %d, b = %d\n", a, b); return 0; }
题目三:
用户输入初始金额,利率和投资年数,程序将打印一张表格。表格将显示输入的利率以及紧随其后 4 个更高利率下的总金额。程序的会话如下:
Enter initial balance: 100
Enter interest rate: 6
Enter number of years: 5
Years 6% 7% 8% 9% 10%
1 106.00 107.00 108.00 109.00 110.00
2 112.36 114.49 116.64 118.81 121.00
3 119.10 122.50 125.97 129.50 133.10
4 126.25 131.08 136.05 141.16 146.41
5 133.82 140.26 146.93 153.86 161.05
代码三/解答:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
int balance, rate, years;
printf("Enter initial balance: ");
scanf("%d", &balance);
printf("Enter interest rate:");
scanf("%d", &rate);
printf("Enter number of years: ");
scanf("%d", &years);
int i; int temp_rate = rate;
double money[5];
int rate_arr[5];
printf("\nYears ");
for (i = 0; i < 5; i++) {
money[i] = balance;
rate_arr[i] = temp_rate;
printf(" %d%% ", temp_rate++);
//printf("%d\t", rate_arr[i]);
}
printf("\n");
i = 0;
for(i = 0; i < years; i++) {
printf(" %d ", i + 1);
int j;
for (j = 0; j < years; j++) {
money[j] =money[j]* (1.0 + rate_arr[j]/100.0);
printf("%-8.2f", money[j]);
}
printf("\n");
}
return 0;
}
答案:
int main(void) { double init_balance; printf("Enter initial balance: "); scanf("%lf", &init_balance); int rate; printf("Enter interest rate: "); scanf("%d", &rate); int years; printf("Enter number of years: "); scanf("%d", &years); double balance[5]; printf("\nYears "); for (int i = 0; i < 5; i++) { printf("%4d%% ", rate + i); balance[i] = init_balance; } printf("\n"); for (int i = 1; i <= years; i++) { printf("%3d ", i); for (int i = 0; i < 5; i++) { balance[i] += balance[i] * ((rate + i) / 100.0);//同时实现利率的递增 printf("%7.2lf", balance[i]); } printf("\n"); } return 0; }
题目四:
写一个随机发牌的程序。用户指定发几张牌,程序打印手牌。程序的会话如下:
Enter number of cards in hand: 5
Your hand: 9c 7d 3c 5d kd
代码四/解答:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define SIZE(a) (sizeof(a)/sizeof(a[0]))
//int random_number = min + rand() % (max - min + 1);
int main(void) {
srand(time(NULL));
const char rank[] = { '1','2','3','4','5','6','7','8','9','t','j','q','k','a' };
const char suit[] = { 'd','c','h','s' };
bool is_hand[13][4] = { false }; //注意不能TRUE
int num;
printf("Enter number of cards in hand:");
scanf("%d", &num);
printf("Your hand: ");
int i;
for (i = 0; i < num;i++) {
int random_number =rand()%13;
int random_number2 = rand() % 3;
if (is_hand[random_number][random_number2] == false) {
is_hand[random_number][random_number2] = true;
printf("%c%c ", rank[random_number], suit[random_number2]);
}
else {
i--;
}
}
printf("\n");
return 0;
}
/*
写一个随机发牌的程序。用户指定发几张牌,程序打印手牌。程序的会话如下:
Enter number of cards in hand: 5
Your hand: 9c 7d 3c 5d kd
*/
答案:
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <time.h> int main(void) { const char suits[4] = { 's', 'h', 'c', 'd' }; const char ranks[13] = { '2', '3','4', '5','6','7','8','9','t','j','q','k','a' }; bool in_hand[4][13] = { false }; int cards; printf("Enter number of cards in hand: "); scanf("%d", &cards); printf("Your hand: "); srand(time(NULL)); // 设置随机种子 while (cards) { int i = rand() % 4; int j = rand() % 13; // (i, j) if (!in_hand[i][j]) { in_hand[i][j] = true; cards--; printf("%c%c ", ranks[j], suits[i]); } } printf("\n"); return 0; }