【nyoj 270】数的分解(统计因子模板)
生活随笔
收集整理的這篇文章主要介紹了
【nyoj 270】数的分解(统计因子模板)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題干:
數(shù)的分解
時(shí)間限制:1000 ms ?|? 內(nèi)存限制:65535 KB 難度:1 描述你的任務(wù)是找到一個(gè)最小的正整數(shù)Q,使Q的各位數(shù)的乘積等于N。
輸入一個(gè)整數(shù)N(0 ≤ N ≤ 400)。
法1:
#include<cstdio> #include<iostream>using namespace std;int main(){int n;while(scanf("%d",&n)!=EOF){int i = 0,ge,shi,bai,qi;for(;i <= 10000 && n;i++){if(i < 10){if(i == n) break;}else if(i < 100){ge = i % 10;shi = i / 10;if(ge * shi == n)break;}else if(i < 1000){ge = i % 10;shi = (i / 10)%10;bai = i / 100;if(ge * shi * bai == n)break;}else {ge = i % 10;shi = (i / 10)%10;bai = (i / 100)%10;qi = i / 1000;if(ge * shi * bai * qi== n)break;}}if(i==10001)cout<<-1<<endl;else if(n==0)cout<<10<<endl;elsecout<<i<<endl;}return 0; }法2:
#include<cstdio> #include<cstring> #include<string> #include<iostream> #include<algorithm> using namespace std; int res[10];int main() {int n;while(scanf("%d", &n) != EOF){memset(res, 0, sizeof(res));if(n == 0) {printf("10\n"); continue;} //易錯(cuò)else if(n < 10) {printf("%d\n", n); continue;}else{int tmp = n;for(int i = 9; i >= 2; ) // 統(tǒng)計(jì)2-9的因子個(gè)數(shù)(倒序是關(guān)鍵~){if(tmp % i == 0){res[i]++;tmp /= i;}elsei--;}if(tmp > 10) {printf("-1\n"); continue;}for(int i = 2; i <= 9; ++i) //從小到大輸出每個(gè)因子即可for(int j = 1; j <= res[i]; ++j)printf("%d", i);printf("\n");}}return 0; }提煉:統(tǒng)計(jì)因子的模板:
???????????int tmp = x;//用res[]數(shù)組,統(tǒng)計(jì)x的因子for(int i = 2; i <= 9; ) // 統(tǒng)計(jì)2-9的因子個(gè)數(shù)(倒序是關(guān)鍵~){if(tmp % i == 0){res[i]++;tmp /= i;}elsei++;}。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的【nyoj 270】数的分解(统计因子模板)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 语音取代按键 高速领航直达目的地!AIO
- 下一篇: 详解3D物体检测模型: Voxel Tr