C/C++ 棘手程式
這裡有10個棘手的程式,可以測試你的程式設計基礎。
1. 在C++中列印“ ”的程式
在C++程式語言中,我們使用引號來表示要列印文字的開始和結束。因此,列印引號“ ”需要一個特殊的轉義序列。所以,我們將使用\”來在c++中列印引號。
示例
#include<iostream>
using namespace std;
int main() {
cout<<"\"Tutorials Point \"";
return 0;
}輸出
"Tutorials Point "
2. 使用迴圈或goto語句列印從1到10的數字的程式
在程式設計中,為了多次迭代相同的程式碼塊,有幾種方法。它們是:
- 使用迴圈
- 使用goto語句
- 使用遞迴函式
由於你不能使用迴圈或goto語句,唯一有效的方法是使用遞迴函式。讓我們看看如何使用遞迴呼叫來列印從1到10的數字。
示例
#include <stdio.h>
void printNumber(int count){
printf("%d\n", count );
count+=1;
if(count<=10)
printNumber(count);
}
int main(){
printNumber(1);
return 0;
}輸出
1 2 3 4 5 6 7 8 9 10
3. 在不使用算術或比較運算子的情況下檢查兩個數字是否相等
為了檢查兩個數字是否相等,我們可以使用按位異或運算子(^)。如果兩個數字相等,則這兩個數字的按位異或結果為0。現在,讓我們在一個程式中實現這個概念。
示例
#include<iostream>
using namespace std;
int main(){
int a = 132;
int b = 132;
if ( (a ^ b) )
cout<<"a is not equal to b";
else
cout<<"a is else to b";
return 0;
}輸出
a is equal to b
4. 在c/c++中不使用;列印“Hello”
在c/c++程式語言中,有一些方法可以在不使用分號的情況下列印一些內容。我們可以透過使用輸出方法printf的返回型別來做到這一點。c++中的printf方法返回列印到輸出螢幕的字元數。我們可以使用一個可以在不使用分號的情況下執行的條件語句。
示例
#include <stdio.h>
int main(){
if(printf("Hello "))
return 0;
}輸出
Hello
5. 在不使用比較運算子的情況下查詢兩個數字的最大值和最小值的程式。
為了找到兩個定義的數字的最大值和最小值,而不使用比較運算子,我們將使用abs方法,並將兩個數字的差值傳遞給它。它將返回數字之間的正差,我們將使用這個絕對差來找到兩個給定數字的最大值和最小值。
示例
#include<iostream>
using namespace std;
int main (){
int x = 15, y = 20;
cout<<"The numbers are x = "<<x<<"and y = "<<y<<endl;
cout<<"The max of the numbers is "<<((x + y) + abs(x - y)) / 2<<endl;
cout<<"The min of the numbers is "<<((x + y) - abs(x - y)) / 2<<endl;
return 0;
}輸出
The numbers are x = 15and y = 20 The max of the numbers is 20 The min of the numbers is 15
6. 列印程式的原始碼和輸出
將程式的原始碼作為同一程式的輸出打印出來是一個有點棘手的問題,需要對程式語言有很好的理解才能做到。
在這個程式中,我們將使用檔案處理的概念,開啟我們用來編寫程式碼的相同檔案,然後列印檔案的內容。
示例
#include <stdio.h>
int main(void){
FILE *program;
char ch;
program = fopen(__FILE__, "r");
do{
ch=fgetc(program);
printf("%c", ch);
}
while(ch!=EOF);
fclose(program);
return 0;
}7. 在不使用+運算子的情況下找到兩個數字之和的程式
可以透過在程式碼中多次使用-運算子來找到兩個數字之和,而不使用+運算子。下面的程式展示瞭如何做到這一點。
示例
#include<iostream>
using namespace std;
int main(){
int x = 5;
int y = 5;
int sum = x - (-y);
cout<<"The numbers are x = "<<x<<" y = "<<y<<endl;
cout<<"Their sum = "<<sum;
return 0;
}輸出
The numbers are x = 5 y = 5 Their sum = 10
8. 在不使用算術或關係運算符的情況下檢查給定數字是否為偶數。
為了檢查給定數字是否為偶數,我們可以使用按位運算子。按位&運算子與**0x01**一起將檢查數字中第0位上的位。如果第0位上的位為1,則該數字為奇數,否則為偶數。
示例
#include<iostream>
using namespace std;
int main(){
int a = 154;
if(a & 0x01) {
cout<<a<<" is an odd number";
} else{
cout<<a<<" is an even number";
}
printf("\n");
return 0;
}輸出
154 is an even number
9. 在不使用/運算子的情況下將數字除以4的程式。
為了在不使用除法運算子的情況下將數字除以4,我們可以使用右移運算子>>,它會將最後一位移位。
示例
#include<iostream>
using namespace std;
int main(){
int n = 128;
cout<<n<<"divided by 4 = ";
n = n >> 2;
cout<< n;
return 0;
}輸出
128 divided by 4 = 32
10. C++程式遞迴計算數字的各位之和,直到它變成一位數。
透過將數字的所有位相加來計算遞迴和,然後檢視它是否是一位數,如果是則停止,否則重新計算和,直到和變成一位數。
示例
#include <iostream>
using namespace std;
int main() {
int a = 534;
int sum;
if(a)
sum = a % 9 == 0 ? 9 : a % 9 ;
else
sum = 0;
cout<<"The final sum is "<<sum;
return 0;
}輸出
The final sum is 3
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP