網(wǎng)站首頁 編程語言 正文
C語言組成不重復(fù)的三位數(shù)
對于這個問題,我有兩種解決思路
- 第一種較為簡單
- 第二種較為復(fù)雜
(1)通用思路:根據(jù)數(shù)組中的數(shù)字自由組合成三位數(shù)
(2)找出最小數(shù)和最大數(shù)并以此為循環(huán)邊界(目的在于縮小循環(huán)的范圍,提高效率),之后根據(jù)不斷循環(huán),將不符合要求的數(shù)字排除,在某些題中會簡便許多
·由1,2,3,4組成的不重復(fù)的三位數(shù)
(1)通用思路
#include <stdio.h>
?
int main()
{
?? ?int a[4]={1,2,3,4};
?? ?int i,j,k,count=0;
?? ?for (i=0;i<4;i++)
?? ?{
?? ??? ?for (j=0;j<4;j++)
?? ??? ?{
?? ??? ??? ?for (k=0;k<4;k++)
?? ??? ??? ??? ?if (i!=j&&i!=k&&k!=j)
?? ??? ??? ??? ?printf("%d\n",a[i]*100+a[j]*10+a[k]);?
?? ??? ?}
?? ?}
?? ?return 0;
}?
(2)排除思路
#include <stdio.h>
?
int main()
{
?? ?int a[3],i,j,k;
?? ?for (i=123;i<=432;i++)
?? ?{
?? ??? ?int word = 1;
?? ??? ?a[0]=i%10;a[1]=i/10%10;a[2]=i/100;
?? ??? ?for (j=0;j<3;j++)
?? ??? ?{
?? ??? ??? ?if (a[j]==1||a[j]==2||a[j]==3||a[j]==4)
?? ??? ??? ?{
?? ??? ??? ?for (k=0;k<3&&j!=k;k++)
?? ??? ??? ?{
?? ??? ??? ??? ?if (a[k]==a[j])
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?word = 0;
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ? ? ?}
?? ??? ? ? else
?? ??? ? ? {
?? ??? ??? ? ? ?word = 0;
?? ??? ??? ? ? ?break;
?? ??? ? ? }
?? ??? ?
? ? ? ? if (word == 0)
?? ??? ??? ?break;
?? ?}
?
?? ??? ?if (word)
?? ??? ?printf("%d\n",i);
?? ?}
?? ?return 0;
}
·某些題(適用于排除思路)
用1,2,3,.....,9組成三位數(shù)abc,def,ghi,每個數(shù)字恰好使用一次,要求 abc:def:ghi=1:2:3.按照“abc def ghi”的格式輸出所有解,每行一個解。
#include <stdio.h>
?
int main()
{
? ? int abc,def,ghi;
? ? int i,k,a[9],word =1;
? ? for (abc=123;abc<=329;abc++)
? ? {
? ? ? ? def = abc * 2;
? ? ? ? ghi = abc * 3;
? ? ? ? a[0]=abc%10;a[1]=abc/10%10;a[2]=abc/100;
? ? ? ? a[3]=def%10;a[4]=def/10%10;a[5]=def/100;
? ? ? ? a[6]=ghi%10;a[7]=ghi/10%10;a[8]=ghi/100;
? ? ? ? for (k=0;k<9;k++)
? ? ? ? {
? ? ? ? ? ? word = 1;
? ? ? ? ? ? for (i=0;i<9&&i!=k;i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (a[k]==a[i]||a[i]==0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? word = 0;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if (word == 0)
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? if (word)
? ? ? ? printf("%d,%d,%d\n",abc,def,ghi);
? ? ? ??
? ? }
}
打印1234組成的不重復(fù)三位數(shù)
1、每位數(shù)都遍歷一次,就是個位出現(xiàn)1234,十位出現(xiàn)1234,百位出現(xiàn)1234
2、去重復(fù),個位十位百位不能相等
3、復(fù)合式遍歷,統(tǒng)計(jì)這種遍歷得到了多少回
var count = 0;
for(i=1;i<=4;i++){
for(j=1;j<=4;j++){
for(k=1;k<=4;k++){
if(i!=j && j!=k && k!=i){
count++;
console.log(i+""+j+""+k);
}
}
}
}
console.log("這樣的三位數(shù)有"+count+"個");
原文鏈接:https://blog.csdn.net/H20211009/article/details/121714186
相關(guān)推薦
- 2022-07-18 linux系統(tǒng)安全和應(yīng)用
- 2023-07-04 maven引入本地jar,打包問題
- 2022-12-23 Android開發(fā)之線程通信詳解_Android
- 2022-03-31 C#實(shí)現(xiàn)單位換算器_C#教程
- 2022-03-17 結(jié)合示例說明shell是如何被解析的_linux shell
- 2022-10-21 React實(shí)現(xiàn)動態(tài)調(diào)用的彈框組件_React
- 2022-05-22 Virtualbox?安裝?docker的流程分析_VirtualBox
- 2022-05-02 dubbo服務(wù)使用redis注冊中心的系列異常解決_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支